Connection oriented Program
// TCP SERVER PROGRAM
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>
int main()
{
char buff[2000];
int sd,connfd,len;
struct sockaddr_in servaddr,cliaddr;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd==-1)
{
printf(" socket not created in server\n");
exit(0);
}
else
{
printf("socket created in server\n");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(7802);
if ( bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0 )
printf("Not binded\n");
else
printf("Binded\n");
if(listen(sd,1)!=0)
printf("Not listened\n");
else
printf("Listened\n");
len=sizeof(cliaddr);
connfd=accept(sd,(struct sockaddr *) &cliaddr, &len);
if(connfd!=0)
printf("Server accepted client \n");
else
printf("Server not accepted client\n");
recv(connfd,buff,1024,0);
printf("Serever received msg from client %s",buff);
close(sd);
return(0);
}
// TCP CLIENT PROGRAM
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>
int main()
{
char buff[2000];
int sockfd,connfd,len;
struct sockaddr_in servaddr,cliaddr;
// create socket in client side
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf(" socket not created in client\n");
exit(0);
}
else
{
printf("socket created in client\n");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // ANY address or use specific address
servaddr.sin_port = htons(7802); // Port address
// client connected to server using server address
if(connect(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr) )!=0)
{
printf("client not connected to server\n");
exit(0);
}
else
{
printf("client connected to server\n");
}
printf("Type ur client message\n");
scanf("%s",buff);
// send msg to server
send(sockfd,buff,1024,0);
//close client side connection
close(sockfd);
return(0);
}
Steps:
1) Compile and Run Server Program cc tcpserver.c ./a.out
2) Compile and Run Client Program cc tcpclient.c ./a.out
3) Enter Message in Client
// TCP SERVER PROGRAM
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>
int main()
{
char buff[2000];
int sd,connfd,len;
struct sockaddr_in servaddr,cliaddr;
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd==-1)
{
printf(" socket not created in server\n");
exit(0);
}
else
{
printf("socket created in server\n");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(7802);
if ( bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0 )
printf("Not binded\n");
else
printf("Binded\n");
if(listen(sd,1)!=0)
printf("Not listened\n");
else
printf("Listened\n");
len=sizeof(cliaddr);
connfd=accept(sd,(struct sockaddr *) &cliaddr, &len);
if(connfd!=0)
printf("Server accepted client \n");
else
printf("Server not accepted client\n");
recv(connfd,buff,1024,0);
printf("Serever received msg from client %s",buff);
close(sd);
return(0);
}
// TCP CLIENT PROGRAM
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>
int main()
{
char buff[2000];
int sockfd,connfd,len;
struct sockaddr_in servaddr,cliaddr;
// create socket in client side
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf(" socket not created in client\n");
exit(0);
}
else
{
printf("socket created in client\n");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // ANY address or use specific address
servaddr.sin_port = htons(7802); // Port address
// client connected to server using server address
if(connect(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr) )!=0)
{
printf("client not connected to server\n");
exit(0);
}
else
{
printf("client connected to server\n");
}
printf("Type ur client message\n");
scanf("%s",buff);
// send msg to server
send(sockfd,buff,1024,0);
//close client side connection
close(sockfd);
return(0);
}
Steps:
1) Compile and Run Server Program cc tcpserver.c ./a.out
2) Compile and Run Client Program cc tcpclient.c ./a.out
3) Enter Message in Client
No comments:
Post a Comment