Data transfer over sockets[TCP] how to pack multiple integer in c/c++ and transfer the data with send() recv()?

前端 未结 4 923
感情败类
感情败类 2021-01-21 19:17

I\'m making a small client/server based game, on linux in c/c++ and I need to send the player turn to the server.

Here is my problem.

I want to send two integer

4条回答
  •  深忆病人
    2021-01-21 19:38

    The issue is that TCP is a continuous stream, with no concept of the start or end of a ”message” because it is not message-based.

    Most times, people use a very simple ”framing protocol” whereby you always send a 4-byte header on every transfer which tells the recipient how many bytes to read, then you send that many bytes as your message.

    Use htonl() to send the 4-byte header in network byte order then you will be interoperable. There is a very similar example here.

提交回复
热议问题