In: Computer Science
I am needing to initialize a server socket and wait for incoming connections in C programming. I am also required to create a thread to handle my client and able to pass messages between them asynchronously. I would ideally like to start my server with pthreads to allow for multiple connections.
I have 2 functions started:
int serverRun(){
this will be the meat of the server code with sockets
}
int serverStart()
{
This is where I want to initiate the server
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
// initialise functions and variables
void *connection_handler(void *);
int socket_desc , client_sock , c;
struct sockaddr_in server , client;
pthread_t thread_id;
int serverRun()
{
listen(socket_desc , 3);
//Accept and incoming connection
printf("Waiting for incoming connections...\n");
c = sizeof(struct sockaddr_in);
//Accept and incoming connection
printf("Waiting for incoming connections...\n");
c = sizeof(struct sockaddr_in);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
printf("Connection accepted\n");
if( pthread_create( &thread_id , NULL , connection_handler , (void*) &client_sock) < 0)
{
printf("Could not create thread\n");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( thread_id , NULL);
printf("Handler assigned\n");
}
if (client_sock < 0)
{
printf("accept failed\n");
return 1;
}
}
int serverStart()
{
//Create server socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket\n");
return 0;
}
printf("Server Socket Created\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8000); // You can use any port here
//Bind the port to the server socket to be active listner
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
printf("bind failed\n");
return 0;
}
printf("bind done\n");
return 1;
}
int main(int argc , char *argv[])
{
int serverStatus = serverStart();
if(serverStatus = 1)
{
printf("Server Created\n");
}
else
{
printf("Server Creation failed\n");
}
int a = serverRun();
return 0;
}
/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
//Send some messages to the client
message = "Greetings! I am your connection handler\n";
write(sock , message , strlen(message));
message = "Now type something and i shall repeat what you type \n";
write(sock , message , strlen(message));
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//end of string marker
client_message[read_size] = '\0';
//Send the message back to client
write(sock , client_message , strlen(client_message));
//clear the message buffer
memset(client_message, 0, 2000);
}
if(read_size == 0)
{
printf("Client disconnected\n");
fflush(stdout);
}
else if(read_size == -1)
{
printf("recv failed\n");
}
return 0;
}
Here is the required c c0de for server