0

TCP is stream to communicate and it has varying length. So in the application, how I can know whether the TCP ends or not?

In the transfom layer, The TCP packet header doesn't have a length field and its length is varying, how can the TCP layer know where is the end.

Kangyabing
  • 61
  • 1
  • 2
    What language or technology are you talking about? – Brian S Jul 29 '14 at 12:28
  • if in C#, following links may provide some help http://www.codeproject.com/Questions/669142/Retrieve-Size-of-Data-Available-Csharp-TCP-Network, http://stackoverflow.com/questions/6522869/c-sharp-tcpclient-detecting-end-of-stream – Atur Jul 29 '14 at 12:30
  • Your question is too vague, you need to add more details. What language are you using? What have you tried? Show us code. – Mizipzor Jul 29 '14 at 12:34

4 Answers4

0

You design a protocol that runs on top of TCP that includes a length field (or a message terminator). You can look at existing protocols layered on top of TCP (such as DNS, HTTP, IRC, SMTP, SMB, and so on) to see how they do this.

To avoid pain, it helps to have a thorough understanding of several different protocols layered on top of TCP before attempting to design your own. There's a lot of subtle details you can easily get wrong.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

If you look at this post, it gives a good answer.

Depending on what you are communicating with, or how you are communicating there will need to be some sort of character sequence that you look for to know that an individual message or transmission is done if you plan to leave the socket open.

Community
  • 1
  • 1
Brian S
  • 3,096
  • 37
  • 55
0

When a party of a TCP connection receives a FIN or RST signal it knows that the other side has stopped sending. At an API level you can call shutdown to give that signal. The other side will then get a zero length read and knows that nothing more will be coming.

usr
  • 168,620
  • 35
  • 240
  • 369
0

n the transfom layer

I assume you mean 'transport layer'?

The TCP packet header doesn't have a length field and its length is varying

The IP header has a length field. Another one in the TCP header would be redundant.

how can the TCP layer know where is the end.

From the IP header length word, less the IP and TCP header sizes.

user207421
  • 305,947
  • 44
  • 307
  • 483