P
Poster Matt
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.
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.
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.
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.