TCP Client and Concurrent Server in C

Posted By : Pushpandra Kumar | 15-Dec-2017

Implement TCP client and server (concurrent) where the client gets input from the user and sends it to the server. The server displays it on the screen. The server then gets another input from the user and sends it to the client. The client displays it on the screen. The process continues till server or client sends “bye” to the other party. 

The server processes the multiple client parallel.

tcpEchoClient.c

#include
#include
#include
#include
#include
#include
#include
#include

void str_cli(FILE *,int );

int main(int argc, char *argv[])
{
int sock;
struct sockaddr_in server;
char buffers[1024];
char data[1024];

if(argc!=3)
{
perror("Enter the IP Address and port number of the server!!\n");
exit(EXIT_FAILURE);
}

sock=socket(AF_INET, SOCK_STREAM, 0);
if(sock<0)
{
printf("FAil to create the Socket\n");
exit(EXIT_FAILURE);
}

bzero(&server, sizeof(server));
bzero(buffers, 1024);
server.sin_family=AF_INET;
server.sin_port=htons(atoi(argv[2]));

if((inet_pton(AF_INET, argv[1], &server.sin_addr)) <= 0)
{
perror("Ip Address format error\n");
close(sock);
exit(EXIT_FAILURE);
}

if(connect(sock, (struct sockaddr *) &server, sizeof(server))<0)
{
perror("COnnection Error");
close(sock);
exit(EXIT_FAILURE);
}
printf("\nEnter the Message to be sent to Server\n");
str_cli(stdin, sock);
exit(0);
}

void str_cli( FILE *fp, int sockfd)
{
char buffer[1024];
bzero(buffer, 1024);

while(fgets(buffer, 1024, fp) != NULL)
{
send(sockfd, buffer, strlen(buffer), 0);
bzero(buffer, 1024);
if(recv(sockfd, buffer, 1024, 0) < 0)
{
perror("\n Client:::Server Terminated permanently\n");
exit(EXIT_FAILURE);
}
else
{
printf("Received Message from the Server is\n");
fputs(buffer,stdout);
}
}
}
      

tcpEchoServer.c

#include
#include
#include
#include
#include
#include

void str_echo(int );
void ms(FILE *,int );

int main(int argc, char *argv[])
{
int sock, new_conn;
struct sockaddr_in server,client;
int len;

if(argc!=2)
{
perror("Enter a Port Number!!\n");
exit(EXIT_FAILURE);
}

sock=socket(AF_INET, SOCK_STREAM, 0);
if(sock<0)
{
printf("Fail to create socket!!");
exit(EXIT_FAILURE);
}

bzero(&server, sizeof(server));
bzero(&client, sizeof(client));
server.sin_family=AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=htons(atoi(argv[1]));

if(bind(sock, (struct sockaddr *)&server, sizeof(server)))
{
perror("Binding Failed");
close(sock);
exit(EXIT_FAILURE);
}

listen(sock,5);

while(1)
{
len=sizeof(client);
new_conn=accept(sock, (struct sockaddr *)&client, &len);

if(new_conn == -1)
{
perror("Acceptance Failed");
close(sock);
exit(EXIT_FAILURE);
}

str_echo(new_conn);
close(new_conn);
}
}

void str_echo(int sockfd)
{
int data_len,i;
char buffers[1024];

while(1)
{
bzero(buffers,1024);
data_len=recv(sockfd, buffers, 1024, 0);
if(data_len>0)
{
if((i=strcmp(buffers, "bye"))==0)
{
printf("\nTerminating");
return;
}
else
{
printf("\n  Message Received from Client is: %s",buffers);
printf("\nEnter the Message to be sent to the client\n");
ms(stdin, sockfd);
}
}
else{}
}
}

void ms(FILE *fp, int sockfd)
{
char buf[1024];
bzero(buf, 1024);

while(fgets(buf, 1024, fp) != NULL)
{
send(sockfd, buf, strlen(buf), 0);
}
}
      

For Running the programs first you have to compile using GNU Compiler. Steps for Starting Server, type the commands in Terminal:
1. gcc tcpEchoServer.c -o server
2. ./server port_number
Specify the port number on which you want to bind the server. Steps for Client, type the commands in another Terminal:
1. gcc tcpEchoClient.c -o server
2. ./server ip_address port_number
Specify IP address and Port number of the server, incase you are running your server on the same machine then IP address is 127.0.0.1

 

About Author

Author Image
Pushpandra Kumar

Pushpender has experience in Core Java, C & C++. His hobbies are learning new technologies and listening music.

Request for Proposal

Name is required

Comment is required

Sending message..