EX3:Write a program
to Echo from server to client 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.
4.
Terminate the connections using close() system call.
Server Program:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<errno.h>
#include<time.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
for(;;)
{
bzero(buff,MAX);
read(sockfd,buff,sizeof(buff));
printf("from client: %s\n",buff);
write(sockfd,buff,sizeof(buff));
if(strncmp("exit",buff,4)==0)
{
printf("server exit...\n");
break;
}
}
}
int main()
{
int sockfd,connfd,len;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("socket successfully created...\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr,sizeof(servaddr)))!=0)
{
printf("socket bind failed...\n");
exit(0);
}
else
printf("socket successfully binded...\n");
if((listen(sockfd,5))!=0)
{
printf("listen failed...\n");
exit(0);
}
else
printf("server listening...\n");
len=sizeof(cli);
connfd=accept(sockfd,(SA*)&cli,&len);
if(connfd<0)
{
printf("server accept failed...\n");
exit(0);
}
else
printf("server accept the client...\n");
func(connfd);
close(sockfd);
}
Client Program:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<errno.h>
#include<time.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,sizeof(buff));
printf("enter the string:");
n=0;
while((buff[ n++ ]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
printf("from server: %s", buff);
if ((strncmp(buff,"exit",4)) == 0)
{
printf("client exit...\n");
break;
}
}
}
int main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("socket successfully created" );
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))!=0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server... \n");
func(sockfd);
close(sockfd);
}
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<errno.h>
#include<time.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
for(;;)
{
bzero(buff,MAX);
read(sockfd,buff,sizeof(buff));
printf("from client: %s\n",buff);
write(sockfd,buff,sizeof(buff));
if(strncmp("exit",buff,4)==0)
{
printf("server exit...\n");
break;
}
}
}
int main()
{
int sockfd,connfd,len;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("socket successfully created...\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr,sizeof(servaddr)))!=0)
{
printf("socket bind failed...\n");
exit(0);
}
else
printf("socket successfully binded...\n");
if((listen(sockfd,5))!=0)
{
printf("listen failed...\n");
exit(0);
}
else
printf("server listening...\n");
len=sizeof(cli);
connfd=accept(sockfd,(SA*)&cli,&len);
if(connfd<0)
{
printf("server accept failed...\n");
exit(0);
}
else
printf("server accept the client...\n");
func(connfd);
close(sockfd);
}
Client Program:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<errno.h>
#include<time.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,sizeof(buff));
printf("enter the string:");
n=0;
while((buff[ n++ ]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
printf("from server: %s", buff);
if ((strncmp(buff,"exit",4)) == 0)
{
printf("client exit...\n");
break;
}
}
}
int main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("socket successfully created" );
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))!=0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server... \n");
func(sockfd);
close(sockfd);
}
OUTPUT:
SERVER:
[tec@tecnetserver anand]$
cc serecho.c
[tec@tecnetserver anand]$
cc serecho.c -o ser
[tec@tecnetserver anand]$
./ser
Socket successfully created..
socket successfully binded..
Server listening..
server accept the client...
From Client:Hello
From Client:Hai
From Client:exit
server exit....
[tec@tecnetserver anand]$
CLIENT:
[tec@tecnetserver anand]$
cc cliecho.c
[tec@tecnetserver anand]$
cc cliecho.c -o cli
[tec@tecnetserver anand]$
./cli
socket successfully
createdconnected to server..
Enter the String:Hello
From Server:Hello
Enter the String:Hai
From Server:Hai
Enter the String:exit
From Server:exit
client exit.
[tec@tecnetserver anand]$
No comments:
Post a Comment