LeDuc
LeDuc

Reputation: 243

Does incoming message auto truncated in .NET UdpClient/UDPServer?

When working in VB6, i used Winsock to send and receive message over internet. The popular problem in VB6 Winsock is when i send a large string about 7000 character, the client received many part truncated to 2500->3000 character and i need to join truncated packet to have the original message like this:

Public FullMessage as string
Private Sub wskConnect_DataArrival(ByVal bytesTotal As Long)
    Dim sBuff As String
    wskConnect.GetData sBuff, vbString
    FullMessage = FullMessage & sBuff
End sub

This problem is confirmed for VB6 winsock in this link (VB6 Asynchronous Tcp Client truncates incoming messages)

Now i'm going to migrate my code to .NET Framework. Do i need to do the same method when working with .Net UDPClient/TCPClient ? Does .Net auto-resolve this problem for me or the message is still auto-truncated and i need to join it myself ? Does this problem come for both UDP protocol and TCP protocol or only for TCP protocol ?

Upvotes: 1

Views: 180

Answers (2)

jgauffin
jgauffin

Reputation: 101166

Now i'm going to migrate my code to .NET Framework. Do i need to do the same method when working with .Net UDPClient/TCPClient ? Does .Net auto-resolve this problem for me or the message is still auto-truncated and i need to join it myself ? Does this problem come for both UDP protocol and TCP protocol or only for TCP protocol ?

TCP is a stream based protocol. In it there are no notation of messages (as in application level messages). It sees everything sent as bytes of data.

UDP on the other hand do promise that what you send is what you will receive.

However, there is another major difference. TCP guarantees that it will do the best o deliver your message (or die trying). UDP do not guarantee that. It just promises that if it would deliver your message it would be complete. But the message can get lost too.

So if you use TCP you'll get your data, but you have to assemble your messages by yourself. If you use UDP you'll get the messages the way you sent them. But the messages can be lost too.

imho it's much easier to assemble messages in TCP than writing a reliable protocol on top of UDP.

Upvotes: 2

nvoigt
nvoigt

Reputation: 77354

There is no message truncation and there never was. The highest voted answer on your link even says so. This is how sockets work. They don't know anything about messages. They know that bytes are comming in. It's your job to handle messages.

.NET sockets work like sockets in other languages because that is how they were defined. However, if you want messages to be exchanged and don't care for the low level work of sockets, you can use mechanisms like WCF to have the .NET Framework do the heavy lifting and only deliver complete messages to you. This means that you have to give some control over how messages look to the .NET Framework. For WCF for example, that would be the SOAP specification or maybe even REST.

Upvotes: 1

Related Questions