0% found this document useful (0 votes)
34 views

Client Server Program

This document contains code for a server program and client program written in C. The server program creates a socket, binds it to a port, listens for connections, accepts connections, reads messages from clients, and prints them. The client program creates a socket, connects to the server, scans user input, writes it to the server, and closes the socket.

Uploaded by

Rohit Lal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Client Server Program

This document contains code for a server program and client program written in C. The server program creates a socket, binds it to a port, listens for connections, accepts connections, reads messages from clients, and prints them. The client program creates a socket, connects to the server, scans user input, writes it to the server, and closes the socket.

Uploaded by

Rohit Lal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/* This is a server program: server.

c
* compile with: cc -o server server.c */

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#define TRUE 1

main(){
int soc,len;
char buf[1024];

struct sockaddr_in server,client;


int msgsock,addrlen=sizeof(client);

soc=socket(AF_INET, SOCK_STREAM,0);

if(soc<0){
perror("\n Error in openig socket..."); exit(1);
}

server.sin_family=AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=0;

if(bind(soc,(struct sockaddr *)&server,sizeof(server))){


perror("\n Error in bind..."); exit(2);
}

len=sizeof(server);

if(getsockname(soc,(struct sockaddr *)&server,&len)){


perror("\n Error in getting port..."); exit(3);
}

printf("\n Socket has port# %hd\n", ntohs(server.sin_port));

listen(soc,5);

do{
msgsock=accept(soc,(struct sockaddr *)&client,&addrlen);
if(msgsock==-1){
perror("\n Error in accept");exit(0);
}

else
read(msgsock,buf,1024);

printf("\n message from client::\n%s\n", buf);

}while(TRUE);

}
/* This is a client program: client.c
* compile with: cc -o client client.c */

#include<sys/types.h>/* contain data types such as u_short */


#include<sys/socket.h>/*all socket sys call */
#include<netinet/in.h>/* it contain socket address stuct */
#include<netdb.h>/* the function getsockname()*/

#include<stdio.h>
#include<string.h>
#include<arpa/inet.h>
/* convert IP add from decimal to 32 bits memory word */

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


int soc, addrlen;
char buf[1024];

struct sockaddr_in server;

soc=socket(AF_INET, SOCK_STREAM,0);

if(soc<0){
perror("\n Error in openig socket...");
exit(1);
}

server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr(argv[1]);
server.sin_port=htons(atoi(argv[2]));

if(connect(soc,(struct sockaddr *)&server,sizeof(server))<0){


perror("\n Error in connection..."); exit(2);
}

addrlen=sizeof(server);

scanf("%[^\n]",buf);
write(soc, buf,1024);

close(soc);
}

You might also like