Hi!
I have to make a connection (client application) to 8 different socket. And then receive data from all socket. What I'd tried to do is make an array of socket and the connect them. This is working fine. Afterwards I trie to asynchronically receive data from all socket using methods bellow.
The method works fine if I receive data only on one socket (it doesn't meter witch), but after I receive some data on any other socket, method OnDataReceived won't fire. What am I doing wrong? Or what would be a right approach?
I have to make a connection (client application) to 8 different socket. And then receive data from all socket. What I'd tried to do is make an array of socket and the connect them. This is working fine. Afterwards I trie to asynchronically receive data from all socket using methods bellow.
#region Global variables
Socket[] m_socClient = new Socket[8];
IAsyncResult[] m_asynResult = new IAsyncResult[8];
AsyncCallback[] pfnCallBack = new AsyncCallback[8];
byte[][] m_DataBuffer = new byte[8][];
int timeOut = 500;
string IPstring = "10.0.0.15";
int[] PortNo = new int[8];
string bar;
#endregion
#region Constructor
public BAR()
{
IPAddress ip = IPAddress.Parse(IPstring);
for (int i = 0; i < 8; i++)
{
m_socClient[i] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
if (m_socClient[i].Connected == false)
{
PortNo[i] = (2001 + (i * 10));
IPEndPoint ipEnd = new IPEndPoint(ip, PortNo[i]);
m_socClient[i].Connect(ipEnd);
//Čakaj dokler povezava ni vzpostavljena
while (m_socClient[i].Connected != true) ;
}
}
WaitForData();
}
#endregion
#region Ethernet received
private void WaitForData()
{
for (int i = 0; i < 8; i++)
{
if(pfnCallBack[i] == null)
{
pfnCallBack[i] = new AsyncCallback(OnDataReceived);
}
m_DataBuffer[i] = null;
m_DataBuffer[i] = new byte[1024];
m_asynResult[i] = m_socClient[i].BeginReceive(m_DataBuffer[i], 0, 1024, SocketFlags.None, pfnCallBack[i], null);
}
}
private void OnDataReceived(IAsyncResult asyn)
{
//end receive...
for (int i = 0; i < 8; i++)
{
if (m_DataBuffer[i][0] != 0)
{
m_socClient[i].EndReceive(asyn);
bar = ByteArrayToStr(m_DataBuffer[i]);
}
}
WaitForData();
}
#endregion
The method works fine if I receive data only on one socket (it doesn't meter witch), but after I receive some data on any other socket, method OnDataReceived won't fire. What am I doing wrong? Or what would be a right approach?