patterncMinor
Multithreaded Client/ Server communication
Viewed 0 times
serverclientcommunicationmultithreaded
Problem
This is my first network programming codes writing for a client who has the following requirement:
As of now, they are not able to identify whether client side software has really crashed or is still running. The first time client gets registered in the server succesfully, they are not able to track about its active execution state on later stages. So I gave it a thought and somehow managed to use threads to satisfy their requirement partially (code still under development). But is this really a fool-proof solution? Will this code be able to handle 100's of clients and know about their statuses? I tried with around 10-20 terminals of client and it worked perfectly.
Can anyone review my code let me know the errors/ any modifications.
Here's my Server code:
```
#include
#include //strlen
#include //strlen
#include
#include //inet_addr
#include //write
#include //for threading , link with lpthread
//the thread function
void connection_handler(void );
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
- My Server has to run 247365 for multiple clients at the same time (concurrency).
- Their Client (Software running on client machine) has some bugs which makes it to crash sometimes.
- When the software crashes, Server should get information and the connection should be terminated, freeing any other id's associated with each connected client.
As of now, they are not able to identify whether client side software has really crashed or is still running. The first time client gets registered in the server succesfully, they are not able to track about its active execution state on later stages. So I gave it a thought and somehow managed to use threads to satisfy their requirement partially (code still under development). But is this really a fool-proof solution? Will this code be able to handle 100's of clients and know about their statuses? I tried with around 10-20 terminals of client and it worked perfectly.
Can anyone review my code let me know the errors/ any modifications.
Here's my Server code:
```
#include
#include //strlen
#include //strlen
#include
#include //inet_addr
#include //write
#include //for threading , link with lpthread
//the thread function
void connection_handler(void );
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
Solution
Should not assume null termination
This code in the server assumes that the client will send null terminated data:
There are several problems with that:
To fix this,you should use
This code in the server assumes that the client will send null terminated data:
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
printf("%s\n",client_message);
}There are several problems with that:
- The client could not send null terminated data.
- The client could send a string longer than 2000 bytes, which would look unterminated.
- Even if the client were well behaved, sockets don't necessarily send/receive all of their data in one chunk. So the client might send a 1000 byte string but your
recv()call might only read 500 bytes (with no null termination).
To fix this,you should use
read_size to terminate the string yourself (and make your buffer bigger by one character). Or pick a new message protocol that sends a length followed by a string of that length, so you can better determine where each message ends.Code Snippets
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
printf("%s\n",client_message);
}Context
StackExchange Code Review Q#143286, answer score: 4
Revisions (0)
No revisions yet.