c - Residual characters printed on stdout with socket communication -


in socket tcp communication, have server , client. both can read , write socket.

the code written in c , uses linux system calls recv , write.

recv saves received string in:

char message_array[2000]; 

another array same dimensions used source write process.

after both reading , writing process following operation performed, clear array elements:

memset(&message_array, 0, sizeof(message_array)); 

moreover, fflush performed on stdin , stdout @ every write , every read process.

the server prints on stdout writes , receives.

if send small messages both terminals ("hello", "hi") several times (18-20), appears work correctly. if try send longer messages (longer 5 characters, shorter 2000!), server side has strange behaviour: prints message received client, inserts random number of trailing characters of previous messages. example have:

client message: hello1 server message: hello2 client message: hello3 server message: hello4  client message: other characters5 server message: hello6 client message: long phrase 7 ters5 

after several messages, 5 characters ters5 previous message appear trailing characters after actual message, a long phrase 7. continuing "conversation", other unexpected newline , "old" characters appears on stdout of server.

the code of read , write routine following:

void *connection_handler_read(void *socket_read_desc) {     int read_sock = *(int*)socket_read_desc;     int read_size;     char remote_message[max_string_len];      while( (read_size = recv(read_sock, remote_message, max_string_len, 0)) > 0 )     {         printf(remote_message);         fflush(stdout);     }      free(socket_read_desc);     connection_active = 0;     return 0; }  void *connection_handler_write(void *socket_write_desc) {     int write_sock = *(int*)socket_write_desc;     char local_message[max_string_len];      while( connection_active != 0 )     {         scanf ("%[^\n]%*c", local_message);         write(write_sock, local_message, strlen(local_message));         memset(&local_message, 0, sizeof(local_message));         fflush(stdin);     }      free(socket_write_desc);     return 0; } 

these 2 functions invoked in main threads pthread_create after new socket connection succesfully created (so, after successful accept). operate on same socket.

my questions are:

1) software problem or socket problem?

2) trick should have followed, in addition memset , fflush?

  1. the read side not 0 buffer used reading, before reusing it. char remote_message[max_string_len] = {0}; @ declaration time , memset(remote_message, 0, sizeof(remote_message); after reading.

  2. memset(&local_message, 0, sizeof(local_message)); --> memset(local_message, 0, sizeof(local_message));

  3. bear in mind tcp (are using tcp?) not message oriented protocol. read() can (and will) return amount of data between 1 byte , amount sent writer far. if want line oriented protocol/output, scan read data '\n' , not remove in scanf().

other:

  • do call close() on socket descriptors somewhere?
  • why free int * parameters? allocated heap malloc?

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -