0% found this document useful (0 votes)
49 views6 pages

Client-Server Communication With One Client and One Server

This document describes client-server communication between one client and one server using TCP sockets in C. It includes code snippets for both the client and server. The client code opens a socket, connects to the server, sends a message read from stdin, receives a response and prints it. The server code opens a socket, binds it to a port, accepts the client connection, receives the message, prints and responds to the client.

Uploaded by

Rishabh Shrivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

Client-Server Communication With One Client and One Server

This document describes client-server communication between one client and one server using TCP sockets in C. It includes code snippets for both the client and server. The client code opens a socket, connects to the server, sends a message read from stdin, receives a response and prints it. The server code opens a socket, binds it to a port, accepts the client connection, receives the message, prints and responds to the client.

Uploaded by

Rishabh Shrivas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Client-Server communication with one client and one

server :

Client:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<netdb.h>
#include<unistd.h>
#include <string.h>
#include<stdlib.h>

int main(int argc,char *argv[]){


int socket_fd;int port_num = 2017;
char buffer[18];
struct sockaddr_in server_addr;

socket_fd = socket(AF_INET,SOCK_STREAM,0);
if(socket_fd<0)
printf("\nSocket error");

bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_num);
inet_pton(AF_INET,argv[0],&server_addr.sin_addr);

if(connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr)


<0)){
printf("Connection error\n");
exit(0);
}

bzero(buffer,18);
fgets(buffer,18,stdin);

if(write(socket_fd,buffer,18) <0)
printf("\nCouldn't send to server\n");

bzero(buffer,18);
if(read(socket_fd,buffer,18) <0)
printf("\nerror reading from the server\n");

printf("%s\n",buffer);

return 0;

}
Server:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<netdb.h>
#include<unistd.h>
#include <string.h>
#include<stdlib.h>

int main(int argc,char *argv[]){


int socket_fd,newsocket_fd;int port_num = 2017,clilen;
char buffer[18];
struct sockaddr_in server_addr,client_addr;

socket_fd = socket(AF_INET,SOCK_STREAM,0);
if(socket_fd<0)
printf("\nSocket error");

bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_num);
server_addr.sin_addr.s_addr = INADDR_ANY;

if (bind(socket_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)


{
perror("ERROR on binding");
exit(1);
}

listen(socket_fd,3002);
newsocket_fd = accept(socket_fd, (struct sockaddr *)&client_addr, &clilen);
if (newsocket_fd < 0) {
perror("ERROR on accept");
exit(1);
}

bzero(buffer,18);

if (read( newsocket_fd,buffer,18 ) < 0) {


perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s\n",buffer);

if (write(newsocket_fd,"I got your message",18)< 0) {


perror("ERROR writing to socket");
exit(1);
}

return 0;

}
OUTPUT:

Client - Server communication with multiple clients and a


single server:
Client:

#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<time.h>
#include<netdb.h>
#include<unistd.h>
#include <strings.h>
#include<stdlib.h>

int main(int argc,char *argv[]){


int socket_fd;int port_num = 2017,p;
char buffer[18];
struct sockaddr_in server_addr;

socket_fd = socket(AF_INET,SOCK_STREAM,0);
if(socket_fd<0)
printf("\nSocket error");

bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_num);

server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");

if(connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(server_addr))


<0){
printf("Connection error\n");
exit(0);
}

bzero(buffer,18);
fgets(buffer,18,stdin);

if(write(socket_fd,buffer,18) <0)
printf("\nCouldn't send to server\n");

bzero(buffer,10);
if(read(socket_fd,buffer,18) <0)
printf("\nerror reading from the server\n");

printf("%s\n",buffer);
return 0;
}

Server:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<netdb.h>
#include<unistd.h>
#include <string.h>
#include<stdlib.h>
int main(int argc,char *argv[]){
int socket_fd,newsocket_fd;int port_num = 2017,clilen;
pid_t child;
char buffer[10],cladr[100];
struct sockaddr_in server_addr,client_addr;

socket_fd = socket(AF_INET,SOCK_STREAM,0);
if(socket_fd<0)
printf("\nSocket error");

bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port_num);

inet_pton(AF_INET,"127.0.0.1",&server_addr.sin_addr.s_addr);

if (bind(socket_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)


{
perror("ERROR on binding");
exit(1);
}

listen(socket_fd,5);
for(;;){

bzero(&client_addr,sizeof(client_addr));
newsocket_fd = accept(socket_fd, (struct sockaddr *)&client_addr, &clilen);
inet_ntop(AF_INET,&(client_addr.sin_addr),cladr,100);

if (newsocket_fd < 0) {
perror("ERROR on accept");
exit(1);
}

if((child = fork()) == 0){


close(socket_fd);
bzero(buffer,10);

if (read( newsocket_fd,buffer,10 ) < 0) {


perror("ERROR reading from socket");
exit(1);
}
printf("\nReceived from %s",cladr);

printf("Here is the message: %s\n",buffer);


if (write(newsocket_fd,"I got your message",18)< 0) {
perror("ERROR writing to socket");
exit(1);
}
}
close(newsocket_fd);
}
return 0;

OUTPUT:

You might also like