Re: Socket programming source, help please.
- From: Poster Matt <postermatt@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 10 Dec 2009 12:30:06 GMT
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
.
- Follow-Ups:
- Re: Socket programming source, help please.
- From: Peter Duniho
- Re: Socket programming source, help please.
- References:
- Socket programming source, help please.
- From: Poster Matt
- Re: Socket programming source, help please.
- From: Peter Duniho
- Re: Socket programming source, help please.
- From: Poster Matt
- Re: Socket programming source, help please.
- From: Peter Duniho
- Re: Socket programming source, help please.
- From: Poster Matt
- Re: Socket programming source, help please.
- From: Peter Duniho
- Re: Socket programming source, help please.
- From: Poster Matt
- Re: Socket programming source, help please.
- From: Peter Duniho
- Socket programming source, help please.
- Prev by Date: Re: Print any file to a specific printer (silently)
- Next by Date: Read/Write binary file data to/from string
- Previous by thread: Re: Socket programming source, help please.
- Next by thread: Re: Socket programming source, help please.
- Index(es):
Relevant Pages
|