CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Threaded View

  1. #1
    Join Date
    Jul 2002
    Posts
    788

    UdpClient socket issues, pls help!

    Hi,

    I am creating an application that talks to 2 other applications, A and B. So I am creating a 2 udp sockets bind to two different ports, udpClient1 socket receive from port 12345 from application A. Upon receiving this data, i do something and use udpClient2 socket (bind to 6789) to send new data to port 54321 to application B. In the main thread, I am also listening for data that comes in from B, to the port that i am sending it data.

    Code:
     IPEndPoint RemoteEp2 = new IPEndPoint(myPcIp, 54321);
    void Test()
    {
    
       udpClient1 = new UdpClient(12345);
      udpClient1.BeginReceive(receiveCallback1 , udpState);
    
      udpClient2 = new UdpClient(6789);
      udpClient2.BeginReceive(receiveCallback2 , udpState);
    
    }
     
    void receiveCallback1 (IAsyncResult asyn)
    {
    //receive from application A from port 12345
    udpClient1.EndReceive(asyn, ref RemoteEp1); 
    .....
    
    //send to application B at port 54321 
    udpClient2.Send(data,length,RemoteEp2); //this may send to unbound socket in the remote
    
    //restart to receive again
      udpClient1.BeginReceive(receiveCallback1 , udpState);
    }
    
      void receiveCallback2(IAsyncResult asyn)
    {
    
    //receive from application B send to port 6789 (where udpClient2 bind to)
    udpClient2.EndReceive(asyn, ref RemoteEp); //remote host closed exception here!
    .....
    //restart to receive again
    udpClient2.BeginReceive(receiveCallback2 , udpState);
    }
    I found out that the exception is due to udpclient2 sending to an unbound remote ip endpoint in the Test () method. And this ended up an socket error/exception which caused the receiveCallback to be called, as the remote end point is actually my local PC.
    My question is how to handle this?? I want to use asynchronous socket and i need to used the same socket for sending and receive.
    Last edited by mce; November 10th, 2010 at 03:13 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured