Reputation: 1217
Practicing C network programming: I am writing a simple TCP client-server application that is supposed to send a menu (in a separate thread for each client) as a string from the server to the client and print the menu on the client side (latter on this will become a console shop app). Client should then send a int (the selected option from the menu) and the server should receive it but the server doesn't even print an error. Server side:
// Returning message to client
if ( write (tdL.cl, &size_firstLoginMenu, sizeof(int)) <= 0 ) {
printf("[Thread %d] ",tdL.idThread);
perror ("[Thread] Error at write(): size_firstLoginMenu to client.\n");
}
if (write (tdL.cl, firstLoginMenu, size_firstLoginMenu) <= 0) {
printf("[Thread %d] ",tdL.idThread);
perror ("[Thread] Error at write(): firstLoginMenu to client.\n");
} else {
printf ("[Thread %d] Message firstLoginMenu was sent succesfully.\n",tdL.idThread);
}
// Reading the client's selected option
int selectedOption;
if (read(tdL.cl, &selectedOption, sizeof(int)) <= 0) {
printf("[Thread %d]\n",tdL.idThread);
perror ("Error at read() from client.\n");
}
printf("Message from clinet, selectedOption: %d", selectedOption);
Unfortunately neither of the last two prints show in the server console, I am sending selectedOption in the last part of the client:
// Reading size of first mesage: lenWelcomeMsg
if (read(sd, &lenWelcomeMsg, sizeof(int)) < 0) {
perror ("[client] Error reading len welcome message from server.\n");
}
// Reading initial message from server: welcomeMessageFromServer
if (readAll(sd, welcomeMessageFromServer, lenWelcomeMsg) < 0) {
perror ("[client] Error reading welcome message from server.\n");
return errno;
}
// Initial menu - Login Sign Up - message
char initialMenu[512];
int len_initialMenu;
// Reading size of first mesage: lenWelcomeMsg
if (read(sd, &len_initialMenu, sizeof(int)) < 0) {
perror ("[client] Error reading len_initialMenu from server.\n");
}
// Reading initial menu - Login Sign Up - message from server: initialMenu
if (readAll(sd, initialMenu, len_initialMenu) < 0) {
perror ("[client] Error reading initialMenu message from server.\n");
return errno;
}
printf("%s",welcomeMessageFromServer);
printf("\n");
printf("%s",initialMenu);
printf("\n\nSelect your option...\n");
// Sending message containing client selected option to the server
if (write (sd, &selectedOption,sizeof(int)) <= 0) {
perror ("[client] Error at write() selectedOption from client to server.\n");
return errno;
}
Clinet side output:
Welcome to Console Shopper!
1. Login.
2. Sign Up.
Select your option... 1
Sever side output:
[Thread 0] Sending Welcome message to client:
Welcome to Console Shopper!
[Thread 0] Message initMsgToClient was sent succesfully.
[Thread 0] Sending first menu message to client:
1. Login.
2. Sign Up.
[Thread 0] Message firstLoginMenu was sent succesfully.
I was expecting for at least an error as I am treating the if (read() <= 0)
but nothing bellow int selectedOption;
gets printed.
UPDATE: Found a solution: adding a \n
inside last printf
on server fixes the issue, I am still confused why would a simple \n
cause the server to stop printing, and would appreciate if anyone would take the time to explain me why is this so. I am referring to:
printf("Message from clinet, selectedOption: %d\n", selectedOption);
-------------------------^
Upvotes: 0
Views: 734
Reputation: 136525
adding a
\n
inside lastprintf
on server fixes the issue, I am still confused why would a simple\n
cause the server to stop printing, and would appreciate if anyone would take the time to explain me why is this so.
What you observe is stdout
buffering in the standard C library, see man stdout
:
The stream
stderr
is unbuffered. The streamstdout
is line-buffered when it points to a terminal. Partial lines will not appear untilfflush(3)
orexit(3)
is called, or a newline is printed.
Upvotes: 1