unclepauly 6 Light Poster

hello,

ive got some code that works fine, except when the UdpClient sends to the local host. the code is below:

//some duff data to test with
byte[] m_RecBuffer = new byte[3]{0x00, 0x01, 0x02};           

//new client object           
UdpClient udpClient = new UdpClient(34567);

//the remote address
IPEndPoint ipRemote = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);

 //start receiving!
UDPData data = new UDPData();
data.client = udpClient;
data.remote = ipRemote;    
udpClient.BeginReceive(new AsyncCallback(recvCallback), data);

//send to remote address             
udpClient.Send(m_RecBuffer, m_RecBuffer.Length, ipRemote);   

******************

static public void recvCallback(IAsyncResult ar)
{           
            UDPData data = (UDPData)(ar.AsyncState);
            UdpClient client = data.client;
            IPEndPoint remote = data.remote;
            byte[] receiveBytes = client.EndReceive(ar, ref remote);
}

so thats the code. as you can see i am creating a UDP socket having an address of 127.0.0.1, and sending to another UDP socket, also having an address of 127.0.0.1. the problem is that when the Send() method is called, the recvCallback is fired. this should not happen because the UdpClients are created with different ports (so one UdpClient is definitely not sending to itself).

and then when EndReceive is called in the recvCallback, an exception is raised (remote socket has shutdown).

the problem does not happen when the local UdpClient sends to a non-127.0.0.1 UdpClient. for example, if the send was changed to 84.33.45.67:1234 (just a random address).

anyone know why this problem is occuring ?

thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.