-1

I am sending some request from the server to my client but I have some problem.

When I'm sending messages to the client, if I send many messages, I'll receive all with socket.recv()

Is there a way to get the messages one by one ?

Thanks

user3666197
  • 1
  • 6
  • 50
  • 92
AnonBird
  • 570
  • 13
  • 27
  • In recv did u to tried passing the size parameter . that tells the size of message to be received in bytes – coder3521 Jun 25 '15 at 18:32
  • @csharpcoder this does *not* help, the stream-nature of sockets might still chop up incoming data in different sizes. – deets Jun 25 '15 at 18:38
  • Yes of course i can, but i don't know in advance how many bytes i'll have to receive ;) – AnonBird Jun 25 '15 at 18:39
  • You may want to **read whathaveyoutried.com & show some respect** to the StackOverflow Community, which strongly encourages to post high quality questions, altogether with a **MCVE ( a Minimum-Complete-Verifiable-Example of code ) showing what-you-have-tried so far**. You may want to update your post, so as to meet this minimum reasonable level of quality & to show your will to respect other StackOverflow contributing members. They are professionals who love to answer good questions on MCVE-related issues. **Enjoy being StackOverflow Contributing Member & do support this Community Netiquette** – user3666197 Jun 25 '15 at 20:12

3 Answers3

1

You need to use some kind of protocol over otherwise bare sockets.

See python twisted or use something like nanomsg or ZeroMQ if you want a simple drop-in replacement which is message-oriented.

It is not transport-agnostic though, meaning they will only work if they are used on both ends.

user3666197
  • 1
  • 6
  • 50
  • 92
deets
  • 6,285
  • 29
  • 28
0

No. TCP is a byte stream. There are no messages larger than one byte.

Martin James
  • 24,453
  • 3
  • 36
  • 60
0

I assume that you are using TCP. TCP is a streaming protocol, not a datagram protocol. This means that the data are not a series of messages, but instead a single data stream without any message boundaries. If you need something like this either switch the protocol (UDP is datagram, but has other problems) or make your own protocol on top of TCP which knows about messages. Typical message based protocols on top of TCP either use some message delimiter (often newline) or prefix each message with its size.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • thanks for your answer. So, online game are, for example, using UDP instead of TCP ? – AnonBird Jun 25 '15 at 18:38
  • moreover, what the others problem you are writing for ? – AnonBird Jun 25 '15 at 18:38
  • UDP is used for information for which the the nature of the messages allows for loss and re-ordering (e.g. permanent controller update messages with a timestamp), or by using a layer on top of it to ensure messages arrive. So - often games do use it, but not always. It depends no the usecase. – deets Jun 25 '15 at 18:44
  • Ok. For example, i'm trying to do a chat so do you think UDP is the good choice ? – AnonBird Jun 25 '15 at 18:50
  • @Xavier59: if you are OK that chat messages get lost or get duplicated then you might use UDP. – Steffen Ullrich Jun 25 '15 at 19:08