meant for MCA II year students Telangana university Nizamabad
/* Iterative TCP client */
#include "files.h"
main(int argc,char *argv[])
{
int sockfd;
struct sockaddr_in serv_addr;
pname=argv[0];
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDRESS);
serv_addr.sin_port=htons(atoi(argv[1]));
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
perror("client:socket error");
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
{
perror("client:cant connect to error");
exit(1);
}
str_cli(stdin,sockfd);
close(sockfd);
exit(0);
}
/* Iterative TCP server program */
#include "file.h"
main(int argc,char *argv[])
{
int sockfd,newsockfd,clilen,childpid,i;
struct sockaddr_in cli_addr,serv_addr,peer;
pname=argv[0];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
perror("server:cant open stream socket");
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("172.16.0.1");
// serv_addr.sin_port=htons(SERV_TCP_PORT);
serv_addr.sin_port=htons(atoi(argv[1]));
if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
perror("server:cant bind local address");
exit(1);
}
listen(sockfd,1);
// getsockname(sockfd,(struct sockaddr *) &peer,sizeof(peer));
// printf("\nIP= %s",inet_ntoa(peer.sin_addr.s_addr));
fflush(stdout);
for( i=0;i<6;++i)
{
printf("\nServer is waiting for a connection request::\n");
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if(newsockfd<0)
{
perror("server:accept error");
exit(1);
}
str_echo(newsockfd);
close(newsockfd);
}
printf("\nServer is exiting.....");
}
Thursday, March 13, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment