Adrian
Adrian

Reputation: 20078

tcp server question

How does a server knows what kind of packets his receiving ? (strcut,array...) ?

PS: i know dumb question

Upvotes: 0

Views: 75

Answers (2)

Bert F
Bert F

Reputation: 87603

It doesn't - its just a bunch of bits/bytes. The application, using application level protocols, decides how to interpret those bits/bytes.

Just like memory is a bunch of bits/bytes - a pointer to a struct can be forced to point anywhere and the struct can used to read the memory, but the data may be nonsensical. Your application logic ensures (hopefully) that the struct pointer is only applied to memory that contains valid struct data. Similarly, your network application must decide how to interpret what the bits in the packets or TCP stream mean.

An application may use a well-known protocol to decide how to interpret those bytes. For example, the HTTP protocol indicates what the client should transmit and the server knows to interpret that data from the client according to how the HTTP spec. Regardless of what the client sends (e.g. if a gaming client accidentally sent a binary stream to the HTTP server), the HTTP server will nevertheless try to interpret the bits as a HTTP client request.

Upvotes: 1

D.Shawley
D.Shawley

Reputation: 59623

TCP servers receive a stream of bytes. Any interpretation beyond this is up to the application logic.

Upvotes: 1

Related Questions