c - Getting two messages from receive when only one is sent -


i wrote server should wait messages client after opening connection it:

while(1){   if(recv(mysocket, buffer, 1000, 0) < 1){      continue;   }      printf("message received: %s", buffer); } 

i checked wireshark packets sent server, every packet sent there 2 printf outputs.

my question did additional message from.

(the additional message random bytes. every time same.)

your apparent expectations behavior of recv() not justified. @karolyhorvath observed in comments, stream sockets (among tcp-based sockets fall) have no sense whatever of "messages". in particular, network packets not correspond messages on stream socket. posix has behavior of recv(), in fact:

for stream-based sockets, [...] message boundaries shall ignored.

although that's more have effect of combining multiple "messages", can mean single message (as dispatched single send() call) split on multiple recv() calls. will mean if buffer length specify recv() less number of bytes received on socket, there other circumstances in result obtained, too.

on success, recv() returns number of bytes copied receive buffer. if genuinely trying implement sort of "message" exchange, can use split incoming data on message boundaries. recognize, however, that constitutes implementing message-passing protocol on top of stream, sender , receiver need cooperate, @ least implicitly, work.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -