I'm doing a client/server program with a multithreaded server and using sockets to communicate.
Everything works fine for the reading/sending part, but while doing some testing, I saw that the server exits when the read() function has nothing to read, instead of just waiting indefinitely.
It's not just the thread that stops, it's the whole program. Is it the normal behavior ? Shouldn't the read() function be blocking ?
Trying to do a minimal view of what I'm doing :
Server :
void admin_process_server(int sockcomm) // executed in a thread
{
char buff[256];
bzero(buff, sizeof(buff));
while(1){
// definir le nombre de joueurs
write_buff("NUMBER_OF_USERS", &buff);
write(sockcomm, buff, sizeof(buff));
read_client(&buff, sockcomm);
printf("Message recu - Socket %d : %s, %ld\n", sockcomm, buff, strlen(buff));
for (int p = 1; p <= GAME.nb_players; ++p)
{
write_buff("SET_USERNAME", &buff);
write(sockcomm, buff, sizeof(buff));
read_client(&buff, sockcomm);
printf("Message recu - Socket %d : %s, %ld\n", sockcomm, buff, strlen(buff)); //exits here for some reasons ?
set_player_username(p,strdup(buff));
}
}
}
void read_client(char (*buff)[256], int sockfd)
{
bzero(*buff, sizeof(*buff));
int recu = read(sockfd, *buff, sizeof(*buff));
(*buff)[recu] = '\0';
}
void set_player_username(int p, char * u)
{
pthread_mutex_lock(&verrou_player);
GAME.players[p].username = u;
pthread_mutex_unlock(&verrou_player);
}
And basically, at the read() it exits, the client doesn't send nothing
Valgrind gives me this :
==4759==
==4759== Process terminating with default action of signal 13 (SIGPIPE)
==4759== at 0x4E4D2B7: write (write.c:27)
==4759== by 0x10962C: admin_process_server (in /home/seitnom/Documents/ESGI/ProgSysRes/_______PROJECT/Server/server)
==4759== by 0x10927C: th_action (in /home/seitnom/Documents/ESGI/ProgSysRes/_______PROJECT/Server/server)
==4759== by 0x4E436DA: start_thread (pthread_create.c:463)
==4759== by 0x517C88E: clone (clone.S:95)
==4759==
==4759== HEAP SUMMARY:
==4759== in use at exit: 389 bytes in 5 blocks
==4759== total heap usage: 6 allocs, 1 frees, 1,413 bytes allocated
==4759==
==4759== LEAK SUMMARY:
==4759== definitely lost: 0 bytes in 0 blocks
==4759== indirectly lost: 0 bytes in 0 blocks
==4759== possibly lost: 285 bytes in 2 blocks
==4759== still reachable: 104 bytes in 3 blocks
==4759== suppressed: 0 bytes in 0 blocks
==4759== Rerun with --leak-check=full to see details of leaked memory
==4759==
==4759== For counts of detected and suppressed errors, rerun with: -v
==4759== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
After some deeper looking, the read() returns 0 (no idea why, there's nothing to read), and the program exits at the next send from the server.