UDP Socket Client in .NET

Posted by Betamoo on Stack Overflow See other posts from Stack Overflow or by Betamoo
Published on 2010-03-31T22:18:09Z Indexed on 2010/03/31 22:23 UTC
Read the original article Hit count: 432

Filed under:
|
|
|
|

I use UDP Sokckts in my client application. Here are some code snippets:

SendIP = new IPEndPoint(IPAddress.Parse(IP), port);
ReceiveIP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
socket = new Socket(
    AddressFamily.InterNetwork,
    SocketType.Dgram,
    ProtocolType.Udp);
socket.Bind(ReceiveIP);

And to Receive (while(true)):

byte[] data = new byte[BUFFERSIZE];
int receivedDataLength = socket.ReceiveFrom(data, ref ReceiveIP);
string s= Encoding.ASCII.GetString(data, 0, receivedDataLength);

I am doing an infinite while on the receive, there are other things to be done in the while, even if nothing is received..

  • I want to check if there are actually available data then receive else do not wait.. (Note the current receive method waits until the server sends a message)

© Stack Overflow or respective owner

Related posts about c#

Related posts about client