Re: Socket programming source, help please.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Peter Duniho wrote:
Poster Matt wrote:

If you're going to hard-code the socket type by, for example, using the IPEndPoint type, you might as well hard-code the AddressFamily value too:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

I didn't realize that it could be coded like that, I should have looked more closely.


However you can re-code it like this:

IPHostEntry ipHostEntry = Dns.GetHostEntry(server);
IPAddress[] ipAddresses = ipHostEntry.AddressList;
IPAddress ipAddress = ipAddresses[0];
EndPoint endPoint = new IPEndPoint(ipAddress, port);

Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
socket.SendTo(message, endPoint);
socket.ReceiveFrom(message, ref endPoint);

How's that? Does it get your seal of approval? :)

Well, no...I wouldn't bother with the "ipAddress" variable.

That said, it's not my code...if you feel the code is superior when written that way, you should write it that way. My point is simply that from a language point of view, there's no _need_ to write it that way.

Yes, I know the 'ipAddress' is not necessary, but my personal coding style tends to put the extra variable in to make things clear and readable.

Since this thread has gone on way further than necessary, I don't feel I can let it end without you finally saying "Yes, I approve your socket code". :) I hope this does the trick:

IPHostEntry ipHostEntry = Dns.GetHostEntry(server);
EndPoint endPoint = new IPEndPoint(ipHostEntry.AddressList[0], port);

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SendTo(messageOut, endPoint);
socket.ReceiveFrom(messageIn, ref endPoint);

What do you think?

Regards,

Matt
.



Relevant Pages

  • Re: Socket programming source, help please.
    ... 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. ... Then the Socket is created using that IPEndPoint, a timeout added and the data is sent using SendTo - no problem. ... It works fine with RecieveFromso I'll skip the SendTotimeout and just keep the ReceiveFrom() one. ... No new instance of EndPoint is created. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Possible bug in .Net 2.0 udp sockets?
    ... You have multiple listeners and only one socket and one endpoint. ... R> in the received buffer are always equal, ...
    (microsoft.public.dotnet.framework)
  • Re: Possible bug in .Net 2.0 udp sockets?
    ... I called BeginReceiveFrom() several times on purpose, ... BeginReceiveFrom is not active on the socket. ... each with a separate buffer to hold received packets. ... You have multiple listeners and only one socket and one endpoint. ...
    (microsoft.public.dotnet.framework)
  • Possible bug in .Net 2.0 udp sockets?
    ... server sends the requested data to the client. ... each time with a separate buffer. ... "EndReceiveFrom" to get the data and the endpoint, ... One socket listening for incoming udp-packets, ...
    (microsoft.public.dotnet.framework)
  • Re: Slow network?
    ... There is an object called 'endpoint' on both sides, ... outgoing message queues. ... reading/writing messages from/into the socket object, ... raise SystemExit ...
    (comp.lang.python)