0

I'm stuck at the concept of TCP as a stream-oriented protocol.

While using Sockets or anything that uses that protocol there is no way to know how much data will be received in a single receive; so for example if we send 1024 bytes, it can be received in 3 Receive methods give or take.

So we basically need to keep calling our receive methods until the buffer hits the size sent.

I'm still struggling on network, I'm trying to get a bit comfortable using Client/Server applications. I found that the easiest way to do so is to create packets and send them over the network.

So my question is: When serializing a Class as a packet that has - lets say a typeof Normal.Message - and has a string that holds the Message...

[Serializable]
Class Message
{
   String Msg;
   public Message(String msg)
   {
       This.Msg = msg;
   }
}

...if we sent this packet, is it undetermined that it will be received in one Receive Method? And if not, what's the easiest way to ensure that it will?

Enfyve
  • 916
  • 10
  • 22
cruny
  • 13
  • 5
  • 1
    you could first send the payload size as e.g. one byte (if the packets are smaller than < 256 bytes), read this amount of data from the socket and then deserialize the packet – Nico Oct 27 '16 at 13:10
  • Its what i thought actually , i need to send the payload Size then receive the actual payload but what if there will be alot of payloads like alot of paylods sent won't that break the application ? – cruny Oct 27 '16 at 13:12
  • you mean many packets in row? Why should it break the application? If someone sends you garbage, that would be a problem. – Nico Oct 27 '16 at 13:14
  • I meant it won't loss data ? if there is alot of packets sent by many users in the same time while reading their size then reading the actual Packet ? – cruny Oct 27 '16 at 13:16
  • you mean parallel writes to the socket? that would generally a bad idea... you would ne a queue for the packets – Nico Oct 27 '16 at 13:17
  • @heinzbeinz you got any useful tutorial sir ? – cruny Oct 27 '16 at 13:20
  • sorry, not really – Nico Oct 27 '16 at 13:33
  • Have a look at [**message framing**](http://stackoverflow.com/a/37352525/3740093). You still won't receive everything in one call, but with this method you know exactly how many bytes you data is. – Visual Vincent Oct 31 '16 at 11:21

0 Answers0