I am new to socket programming in C. I have written a server-side program and a client-side program. Their functions are described below:
Server side : The server sends integers from 1 to 9 to the client side, and after sending 9, it sends 0. Once 0 has been sent successfully to the client, the server is supposed to print (and NOT send) "SENT DATA SUCCESSFULLY" on the terminal window running server-side, which is a confirmation of the fact that the server has sent 1 to 9 and then 0 successfully to the client.
Client side: The client side is supposed to display all the integers sent by the server. Since we know that we are going to receive integers from 1 to 9 and then 0, the expected output at client side should be :
1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 0
I try to achieve this using below code:
Code for client side:
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
int main()
{
int client_socket;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(8080);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect(client_socket, (struct sockaddr *) & server_address, sizeof(server_address));
char data_recv; //'data_recv' variable stores the data received from server
recv(client_socket, &data_recv , sizeof(data_recv), 0); //receive data from server
while(data_recv - 48) //receive 1 to 9. Once 0 is received, the loop breaks.
{
printf("%c --> ", data_recv);
recv(client_socket, &data_recv , sizeof(data_recv), 0);
}
printf("%c\n", data_recv); //print 0
close(client_socket); //close client socket
return 0;
}
Code for server side :
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
int main()
{
int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(8080);
server_address.sin_addr.s_addr = INADDR_ANY;
bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
listen(server_socket, 5);
while(1)
{
int client_socket;
client_socket = accept(server_socket, NULL, NULL);
for(int i = 1; i < 10 ; i++) //first sending integers from 1 to 9
{
char c = i + 48; //performing integer to character conversion
send(client_socket, &c, sizeof(c), 0); //sending data to client
}
char c = 48; //store 0 in c (ASCII value of 0 is 48)
send(client_socket, &c, sizeof(c), 0); //send 0 to client
printf("SENT DATA SUCCESSFULLY"); //print the message
}
close(server_socket); //close the server socket
}
The problem : On the terminal window which runs the client side of this program, I get the following output :
1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 0
So, on the client-side, we can clearly see that 0 has been received. This means that the server has sent all the data successfully to the client.
However, the problem comes in the terminal window running server-side of the program. Since we know that the server has already send 0 (from the output on the terminal window of client-side), the terminal window on the server-side should display "SENT DATA SUCCESSFULLY". However, it is not displaying this message ("SENT DATA SUCCESSFULLY"). The terminal of server-side remains blank, without displaying the above message, even though 0 has been successfully sent to the client.
I wanted to know :
- Why does the terminal window on server-side does not display the message "SENT DATA SUCCESSFULLY" ?
- How can this be fixed ?
Thanks for helping me !