Hello,

I'm using the UdpClient (.net 3.5) class to comunicate 3 devices with the pc. The pc asks and the devices send the answer to pc by UDP protocol.

My problem is that PC receives mixed packages. That is to say, the PC receives a datagram from one ip that has sent another ip.

Summarizing, the "bytesReceived" and the "sRemoteHost" on the "callback function" does not correspond to the packages seen in a sniffer.

Any help?

Thanks.

Here is a code snippet:

private UdpClient m_udpClient;


public bool Open(int iLocalPort)
{
try
{
m_udpClient = new UdpClient(iLocalPort, AddressFamily.InterNetwork);
}
catch { m_bOpened = false; }

return m_bOpened;
}



public bool SendAndWaitResponse(bool bWait, Dictionary<string, CItemData> colIpsToWait, int iMaxRetries)
{
bool bIsOk;
int iRetries = 0, iIpsToWait;

do
{
iIpsToWait = 0;

foreach (EthernetHost host in m_colIps.Values)
{
if (colIpsToWait.ContainsKey(host.IP) && host.BytesSend != null &&
(host.BytesSend = null) && host.BytesSend.Length > 0)
{
//aņadir Ip to wait
iIpsToWait++;

try
{
m_udpClient.Send(host.BytesSend, host.BytesSend.Length, host.IP, host.Port);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
host.Error = EthernetError.UdpConnectionError;
}
}
}

m_udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);

//WaitResponse() is waiting for all devices to answer
bIsOk = WaitResponse(colIpsToWait, iIpsToWait);

iRetries++;
} while (!bIsOk && iRetries < iMaxRetries);

return bIsOk;
}



private void ReceiveCallback(IAsyncResult ar)
{
IPEndPoint remoteHost = new IPEndPoint(IPAddress.Any, 1000);
string sRemoteHost;
byte[] bytesReceived;

bytesReceived = null;

bytesReceived = m_udpClient.EndReceive(ar, ref remoteHost);

sRemoteHost = remoteHost.Address.ToString();

//to receiving the new packet...
m_udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}