Client Server Programming C Code
Client Server Programming C Code
Example: Write two C programs (say to generate first ten factorials or Fibonacci series) and
create the executables after compilation
Solution
The steps in setting up a client and server are as shown in figure:
In our program, the server creates a socket and binds it to port 8080. Two programs, one for
getting first ten factorials, and one for Fibonacci series are written and their executable files are
included in the directory of the server. After the server creates the socket, and binds it to port
8080, it creates a listening queue for the clients. Since it is connected oriented, we are using
SOCK_STREAM as the socket type (i.e, TCP protocol). For making the server iterative, we use the
following algorithm:
create a socket and bind to the well-known address of the service being offered.
put socket into passive mode, making it ready for use by a server
loop
accept the next connection request from the socket, and obtain a new socket
for the connection
repeat
Read a request from the client
Generate a reply
Send the reply back to the client
until finished with the client
close connection
end
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
char *answer;
if((server_fd=socket(AF_INET,SOCK_STREAM,0))==0)
exit(EXIT_FAILURE);
perror("Setsockopt error");
exit(EXIT_FAILURE);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
perror("Bind failed");
exit(EXIT_FAILURE);
if(listen(server_fd,3)<0)
perror("Listen failed");
exit(EXIT_FAILURE);
}
for(i=1;i<=5;i++)
perror("Accept failed");
exit(EXIT_FAILURE);
if(strcmp(prg1,buffer)==0)
buffer[0]='\0';
printf("Question received\n");
else if(strcmp(prg2,buffer)==0)
buffer[0]='\0';
printf("Question received\n");
else
outp_buffer[0]='\0';
fgets(outp_buffer, 1024, f);
outp_buffer[0]='\0';
printf("Answer sent.\n\n");
return 0;
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
struct sockaddr_in serv_addr; //address of server to which the client needs to connect
if(argc<2)
exit(0);
if((sock = socket(AF_INET,SOCK_STREAM,0))<0)
return -1;
if((sock = socket(AF_INET,SOCK_STREAM,0))<0)
return -1;
memset(&serv_addr,'0',sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET,"127.0.0.1",&serv_addr.sin_addr)<=0)
return -1;
return -1;
question[0]='\0';
val_read = read(sock,buffer,1024);
printf("%s\n",buffer);
buffer[0]='\0';
return 0;
}
The server is started and clients at two different locations are also started together. The clients
are served one by one (next client after the previous client has finished being served).
The run-time screenshots are attached below:
4. Next, the second client asks for first ten factorials and the server provides that too.
5. Further client request are made and served accordingly:
6. After 5 requests the server closes the socket and no more requests from clients are served.