Client server system that uses the socket interface to send messages over TCP connection
SERVER
· Start the program.
· Inside the main function, create an instance of server.
· Create a socket using socket function. Pass the domain AF_INET to indicate IPV4 protocol and the type as SOCK_STREAM to indicate stream socket. On success, the socket function returns a small non-negative int value called socket descriptor that is stored on integer variable.
· Set the values of address structure as zero using bzero(). Initialize family as AF_INET, the IP address using htonl() function and port using htons(). Use bind() function to bind the socket to server address.
· Use the listen() function to define how many connections can be pending on socket.
· Use the accept() function which is called by a TCP server to return the next completed connection from the front of completed connection queue. The client address and address returns the protocol address of the connected peer processes.
· Issue a write protocol to send the message to the client.
CLIENT
· Check if the number of arguments is equal to 3.
· Create a socket using socket function with family as AF_INET and type as SOCK_STREAM.
· Set the values of address structure as zero using bzero().
· Initialize family as AF_INET and the port number that is passed as an argument.
· Use the connect() function to set up connection with server. The address parameter refers to the remote address.
· Obtain the message entered by the user with the help of scanf() function. Write this using write() function.
· Read() function is then used to read the message from the server and print it.
· Stop the program.
PROGRAM:
SERVER
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/types.h>
int main()
{
int socketmain,socketclient,child,port=5050,len=81;
struct sockaddr_in serv,clientaddr;
socklen_t clientlen;
if((socketmain=socket(AF_INET, SOCK_STREAM,0))<0)
{
printf("Server cannot open socket \n");
exit(0);
}
bzero(&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(port);
if((bind(socketmain,(struct sockaddr*)&serv, sizeof(serv)))<0)
{
printf("Server bind failed \n");
exit(1);
}
listen(socketmain,5);
if((socketclient=accept(socketmain, (struct sockaddr *)&clientaddr, &clientlen))<0)
{
printf("Client is bad \n");
exit(0);
}
char buf[len];
bzero(buf,len);
int n=read(socketclient,buf,100);
printf("Message is %s \n",buf);
strcpy(buf,"bye");
write(socketclient,buf,100);
}
CLIENT
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<netdb.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/types.h>
int main(int argc,char **argv)
{
int sockfd,n,port=4500;
struct sockaddr_in serv;
char buf[100],msg[100];
if(argc!=3)
{
printf("Invalid format");
exit(0);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n Socket cannot be opened");
exit(0);
}
bzero(&serv, sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_port=htons(atoi(argv[2]));
if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0)
{
printf("\n Connection failed");
exit(0);
}
printf("Connected \n");
printf("Enter message to server");
scanf("%s",buf);
write(sockfd,buf,100);
n=read(sockfd,msg,100);
printf("Server message is %s \n",msg);
}
OUTPUT:
./server
Message is hi
bye
./client 127.0.0.1 5050
Connected
Enter message to server hi
Server message is bye
No comments:
Post a Comment