tcp服务器问题
服务器如何知道他接收到什么样的数据包? (strcut,数组...) ?
PS:我知道愚蠢的问题
How does a server knows what kind of packets his receiving ? (strcut,array...) ?
PS: i know dumb question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不是 - 它只是一堆位/字节。应用程序使用应用程序级协议决定如何解释这些位/字节。
就像内存是一堆位/字节一样 - 可以强制指向结构的指针指向任何位置,并且可以使用该结构来读取内存,但数据可能是无意义的。您的应用程序逻辑确保(希望)结构指针仅应用于包含有效结构数据的内存。同样,您的网络应用程序必须决定如何解释数据包或 TCP 流中的位的含义。
应用程序可以使用众所周知的协议来决定如何解释这些字节。例如,HTTP 协议指示客户端应传输什么内容,并且服务器知道如何根据 HTTP 规范解释来自客户端的数据。无论客户端发送什么(例如,如果游戏客户端意外地将二进制流发送到 HTTP 服务器),HTTP 服务器仍然会尝试将这些位解释为 HTTP 客户端请求。
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.
TCP 服务器接收字节流。除此之外的任何解释都取决于应用程序逻辑。
TCP servers receive a stream of bytes. Any interpretation beyond this is up to the application logic.