CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Posts
    62

    Need some socket programming source code explained

    Hi,

    I need some socket programming source code explained please. Here's the source that I'm currently using in a class that I got from the web - my question is below the source.

    Code:
    IPHostEntry hostadd = Dns.GetHostEntry(server);
    IPEndPoint EPhost = new IPEndPoint(hostadd.AddressList[0], port);
    
    Socket socket = new Socket(EPhost.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, timeoutSendingToServer);
    socket.SendTo(messageSend, EPhost);
    
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint tempRemoteEP = (EndPoint)sender;
    
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeoutReceivingFromServer);
    
    int numBytesReceived = socket.ReceiveFrom(messageReceive, ref tempRemoteEP);
    The code works perfectly there's just one element I don't understand. Firstly I know Socket should be in a 'using' clause, I've removed that and error checking so that the code I've posted is just the barebones.

    So first of all the server's IP address and port are combined into an IPEndPoint - no problem.

    Then the Socket is created using that IPEndPoint, a timeout added and the data is sent using SendTo - no problem.

    Now comes the code that I don't understand:

    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint tempRemoteEP = (EndPoint)sender;

    A new IPEndPoint is created with any IP address and port 0 and then an EndPoint is created by casting the new IPEndPoint as an EndPoint. Then, after setting the receive timeout (no problem), the Socket ReceiveFrom method is called using the new EndPoint, to get the server's response.

    When the Socket was created it was set with the server's address and port, why is a new IPEndPoint created to receive? Why is it set to any IP address and port 0? To my mind this says get any data at all coming in from the net to this computer, how does it even distinguish between the response from the server and any random incoming internet traffic?

    I'm thoroughly confused by this. Any help will be appreciated. Thanks.

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Need some socket programming source code explained

    As I understand it, the protocal just needs a second socket object to do the receiving.

    The second socket is being used in a call to the main socket's method ReceiveFrom, so presumably that is how it is associated with the server you are talking to.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Sep 2009
    Posts
    62

    Re: Need some socket programming source code explained

    Thanks for your reply.

    Quote Originally Posted by Zaccheus View Post
    As I understand it, the protocal just needs a second socket object to do the receiving.
    No second socked object is created in the code. Did you mean a new IPEndPoint? If so then I should mention that since posting yesterday I've discovered that if you create the EndPoint which will be sent to ReceiveFrom from the original IPEndPoint then it still works. (Code below).


    Quote Originally Posted by Zaccheus View Post
    The second socket is being used in a call to the main socket's method ReceiveFrom, so presumably that is how it is associated with the server you are talking to.
    If you mean ReceiveFrom uses the Socket that has already been created with the original IPEndPoint, then yes good point.

    Altered code:
    Code:
    IPHostEntry ipHostEntry = Dns.GetHostEntry(server);
    IPEndPoint ipEndPoint = new IPEndPoint(ipHostEntry.AddressList[0], port);
    EndPoint endPoint = (EndPoint)ipEndPoint;
    
    Socket socket = new Socket(ipEndPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp;
    
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, timeoutSendingToServer);
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeoutReceivingFromServer);
    
    socket.SendTo(messageSend, endPoint);
    
    int numBytesReceived = socket.ReceiveFrom(messageReceive, ref endPoint);
    Can anyone shed light on why IPAddress.Any is used, as in my original question and shown below, I'd be most grateful.

    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint tempRemoteEP = (EndPoint)sender;
    int numBytesReceived = socket.ReceiveFrom(messageReceive, ref tempRemoteEP);

    By the way I've also noticed that's the way MSDN does it in the example code on ReceiveFrom's method page. See URL below:

    http://msdn.microsoft.com/en-us/library/wdfskwcy.aspx

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Need some socket programming source code explained

    Sorry for the confusion ... I saw the end-point as representing a socket, or rather representing what the 2nd socket (which I'm assuming ReceiveFrom uses internally) is connecting to:

    Quote Originally Posted by MSDN
    The ReceiveFrom method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent.
    Have you checked what tempRemoteEP is after ReceiveFrom is called in the original code?
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Sep 2009
    Posts
    62

    Re: Need some socket programming source code explained

    Ok I got some help in the newsgroup microsoft.public.dotnet.languages.csharp and thought I'd expain what's going on as it might help others here.

    Here's the original code with the bit I didn't understand:

    Code:
    IPHostEntry hostadd = Dns.GetHostEntry(server);
    IPEndPoint EPhost = new IPEndPoint(hostadd.AddressList[0], port);
    
    Socket socket = new Socket(EPhost.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
    
    socket.SendTo(messageSend, EPhost);
    
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint tempRemoteEP = (EndPoint)sender;
    
    socket.ReceiveFrom(messageReceive, ref tempRemoteEP);
    The EndPoint by being set to any IP address and any port (specified by passing it 0) tells ReceiveFrom() to accept traffic over the socket from any IP address and any port, and after RecieveFrom() has returned the referenced EndPoint will hold the details of the IP address and port that it did in fact receive the data from.

    Had I wanted to limit the IP address and port ReceiveFrom() could receive from then I could set that in the EndPoint prior to calling ReceiveFrom().

    Not too hard once you know.

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