EX1: Write a program
to transfer a simple message from client to server using TCP socket
Algorithm:
Server:
1.
Create a socket with the socket() system call.
2.
Bind the socket to an address using the bind()
system call. For a server socket on the Internet, an address consists of a port
number on the host machine.
3.
Listen for connections with the listen() system
call.
4.
Accept a connection with the accept() system
call. This call typically blocks until a client connects with the server.
5.
Send and receive data using the read() and write()
system calls.
6.
Terminate the connections using close() system call.
Client:
1. Create a socket
with the socket() system call.
2. Connect the
socket to the address of the server using the connect() system call.
3. Send and receive
data using the read() and write() system calls.
Server Program:
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<errno.h>
#include<time.h>
#define MAX 100
#define len 81
int cwork(int);
int main()
{
int
smain,sclient,child,port=5678;
struct sockaddr_in serv;
char str[100];
if((smain=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\nserver cannot
open main socket");
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(smain,(struct sockaddr
*)&serv, sizeof(serv))<0)
{
printf("\nServer bin
fail");
exit(0);
}
listen(smain,15);
for(;;)
{
if((sclient=accept(smain,0,0))<0)
{
printf("\nClient is
bad");
exit(0);
}
if((child=fork())<0)
{
printf("\nFailed to
creat child");
exit(1);
}
else if(child==0)
{
close(smain);
cwork(sclient);
close(sclient);
exit(0);
}
close(sclient);
}
}
int cwork(int sclient)
{
char buf[len];
int msglen;
bzero(buf,len);
if((msglen=recv(sclient,buf,len,0))<0)
{
printf("\nerror during
child reception");
exit(1);
}
printf("\nScoket is used
%d",sclient);
buf[msglen]='\0';
printf("\nMessage
%s",buf);
strcpy(buf,"message recd
bye");
send(sclient,buf,strlen(buf),0);
}
Client Program:
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<errno.h>
#include<time.h>
#define MAX 100
#define len 81
int cwork(int);
int main(int argc,char **argv)
{
int sockfd,n;
struct sockaddr_in serv;
char str[100];
if(argc!=3)
{
printf("\n
error message");
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("\n
socket not created");
exit(0);
}
bzero(&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_port=htons(atoi(argv[2]));
if(inet_pton(AF_INET,argv[1],&serv.sin_addr) <=0 )
{
printf("\n
conversion error");
exit(0);
}
if(connect(sockfd,
(struct sockaddr *)&serv, sizeof(serv)) <0)
{
printf("\n
connect fail");
exit(0);
}
printf("\n connected");
printf("\n enter the
message to server");
scanf("%s",str);
write(sockfd,str,sizeof(str));
n=read(sockfd,str,100);
str[n]='\0';
printf("\n from
server %s", str);
}
OUTPUT:
SERVER:
[3019@tecnetserver anand]$ cc
tcpserver.c -o server
[3019@tecnetserver anand]$
./server 127.0.0.1 3019
Socket is used 4
Message Hello
CLIENT:
[3019@tecnetserver anand]$ cc
tcpclient.c -o client
[3019@tecnetserver anand]$
./client 127.0.0.1 3019
Connected
Enter the message to server Hello
From server Message read bye
No comments:
Post a Comment