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

前端 未结 4 928
感情败类
感情败类 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:21

    One possible solution could be defining a format for the message that client send to server. For example you could define a protocol as follow:

    [4 bytes length of your message][2 bytes for first player][2 bytes for second one] and in server side you should at first in rcv function get 4 bytes and extract the length of the arrived message and based on the receiving length(L) call again the rcv function with size L after that you should parse received messaged and extract the turn of each players.

提交回复
热议问题