UDP Sending and Receiving

onepieceking

Well-known member
Joined
Sep 20, 2006
Messages
64
Programming Experience
1-3
Hi,

I need to send and receive structures using UDPClient. How do I go about doing it?

I have done it using TCPClient and TCPListener. UDPClient does not have networkstream and I can't use the serialize or deserialize function of binaryformatter.
 
UDP doesn't have a stream because it is connectionless, only packet per packet is sent/received. UDPClient have Send/Receive methods for sending a packet (datagram) of bytes each transmission. The bytes can be serialized according to your design, same as with Tcp. There are some code example in class documentation. Read all about it: http://msdn2.microsoft.com/en-us/system.net.sockets.udpclient.aspx
 
I still have a problem.

Client code:
VB.NET:
[COLOR=blue][COLOR=blue]Try[/COLOR]
Dim[/COLOR] udpClient [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] UdpClient([COLOR=maroon]"xxx"[/COLOR], 11000)
Dim senddata as New Structuring.Operation
senddata.var1 = "test"
Dim streaming as new memorystream
dim format as new binaryformatter
format.serialize(streaming, senddata)
[COLOR=blue]Dim[/COLOR] sendBytes [COLOR=blue]As[/COLOR] [Byte]() = streaming.toarray
udpClient.Send(sendBytes, sendBytes.Length)
[COLOR=blue]Catch[/COLOR] e [COLOR=blue]As[/COLOR] Exception 
e.ToString()
[COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR]

Server Code:
VB.NET:
[/COLOR]
[COLOR=blue][COLOR=blue]Dim[/COLOR] receivingUdpClient [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] UdpClient(11000)[/COLOR]
[COLOR=blue][COLOR=blue]Dim[/COLOR] RemoteIpEndPoint [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] IPEndPoint(IPAddress.Any, 0)[/COLOR]
[COLOR=blue][COLOR=blue]Try[/COLOR][/COLOR]
[COLOR=blue][COLOR=blue]Dim[/COLOR] receiveBytes [COLOR=blue]As[/COLOR] [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)[/COLOR]
[COLOR=blue]dim streaming as new memorystream[/COLOR]
[COLOR=blue]streaming.write(receivebytes, 0, receivebytes.length - 1) [/COLOR]
[COLOR=blue]dim format as new binaryformatter[/COLOR]
[COLOR=blue]dim result as new Structuring.Operation[/COLOR]
[COLOR=blue]result = ctype(format.deserialize(streaming), structuring.operation)            [/COLOR]
[COLOR=blue][COLOR=blue]Catch[/COLOR] e [COLOR=blue]As[/COLOR] Exception      [/COLOR]
[COLOR=blue]e.ToString()[/COLOR]
[COLOR=blue][COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR][/COLOR]
[COLOR=blue]

the codes are not full but i think is enough. I am having error at the "deserialize" part. It says end of stream reached before parse. what is this error?
 
You missed writing the last byte to the memorystream at deserialize, also you have seek memstream to start after writing to it and before deserializing from it.
VB.NET:
[SIZE=2]
streaming.Write(receiveBytes, 0, receiveBytes.Length)
streaming.Seek(0, IO.SeekOrigin.Begin)
[/SIZE]
 
Hi John,

Thanks for your reply. I change my code abit.

Original
VB.NET:
Dim streaming as new memorystream

Changed:
VB.NET:
Dim streaming as new memorystream(receivebytearray)

With the changed code, i did not use write and it works well.
 
True, that is easier when you already have the full byte array ready, and you also don't have to seek the stream to start when initializing and filling with the contructor.
 
Back
Top