#include
#include
#include
#include
#include
#define max 80
main(int argc,char *argv[])
{
int sfd,s,i,n,sl;
char buff1[max],buff2[max];
struct sockaddr_un sa;
//pname =argv[0];
sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0)
{
printf("CLIENT:SOCKET ERROR");
exit(0);
}
//perror("server:cannot open stream socket");
bzero((char*)&sa,sizeof(sa));
sa.sun_family=AF_UNIX;
strcpy(sa.sun_path,argv[1]);
sl=strlen(sa.sun_path)+sizeof(sa.sun_family);
s=connect(sfd,(struct sockaddr *)&sa,sl);
if(s<0)
{
printf("CLIENT:CANNOT CONNECT");
exit(0);
}
for(i=0;i<10;i++)
{
write(1,"\nENTER MESSAGE:",15);
n=read(0,buff1,20);
write(1,"\nCLIENT HAS SENT:",17);
write(1,buff1,n);
send(sfd,buff1,n,0);
n=recv(sfd,buff2,20,0);
write(1,"\nCLIENT HAS RECEIVED FROM SERVER:",33);
write(1,buff2,n);
}
close(sfd);
exit(0);
}
/* CONNECTION ORIENTED(UNIX) CONCURRENT SERVER*/
#include
#include
#include
#include
#include
#define max 80
main(int argc,char *argv[])
{
int sfd,nsfd,pid,n,i,sl,cl;
char buff[max];
struct sockaddr_un ca,sa;
sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0)
{
printf("SERVER:SOCKET ERROR");
exit(0);
}
bzero((char *)&sa,sizeof(sa));
sa.sun_family=AF_UNIX;
strcpy(sa.sun_path,argv[1]);
sl=strlen(sa.sun_path)+sizeof(sa.sun_family);
if(bind(sfd,(struct sockaddr *)&sa,sl)<0)
{
printf("SERVER:BIND FAILURE");
exit(0);
}
listen(sfd,5);
for(;;)
{
write(1,"\nSERVER:WAITING....",19);
fflush(stdout);
cl=sizeof(ca);
nsfd=accept(sfd,(struct sockaddr*)&ca,&cl);
if(nsfd<0)
{
printf("SERVER:ACCEPT ERROR");
exit(0);
}
pid=fork();
if(pid==0)
{
close(sfd);
for(i=0;i<10;i++)
{
n=recv(nsfd,buff,max,0);
buff[n]='\0';
write(1,"\nMESSAGE RECEIVED FROM CLIENT:",32);
write(1,buff,n);
send(nsfd,buff,n,0);
}
}
close(nsfd);
exit(0);
}
}
/* Concurrent TCP client program */
#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(SERV_TCP_PORT);
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);
}
for(;;)
{
str_cli(stdin,sockfd);
close(sockfd);
exit(0);
}
/* Concurrent TCP server program */
#include "files.h"
main(int argc,char *argv[])
{
int sockfd,newsockfd,clilen,childpid;
struct sockaddr_in cli_addr,serv_addr;
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=htonl(INADDR_ANY);
serv_addr.sin_port=htons(SERV_TCP_PORT);
if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))<0)
{
perror("server:cant bind local address");
exit(1);
}
listen(sockfd,5);
for( ; ;)
{
clilen=sizeof(cli_addr);
printf("\nServer is waiting for connection requests: ");
fflush(stdout);
newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if(newsockfd<0)
{
perror("server:accept error");
exit(1);
}
printf("\nConnection established and communicating with client.....");
fflush(stdout);
if((childpid=fork())<0)
perror("server:fork error");
else if(childpid==0)
{
close(sockfd);
str_echo(newsockfd);
exit(0);
}
close(newsockfd);
}
}
/*Concorent UDP client program */
#include "files.h"
main(int argc,char *argv[])
{
int sockfd;
struct sockaddr_in serv_addr,cli_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(SERV_UDP_PORT);
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
perror("client:cant open datagram socket");
bzero((char *)&cli_addr,sizeof(cli_addr));
cli_addr.sin_family=AF_INET;
cli_addr.sin_addr.s_addr=htonl(INADDR_ANY);
cli_addr.sin_port=htons(0);
if(bind(sockfd,(struct sockaddr*)&cli_addr,sizeof(cli_addr))<0)
{
perror("client:cant bind local address");
exit(1);
}
dg_cli(stdin,sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
close(sockfd);
exit(0);
}
/* Concurent UDP Server program */
#include "files.h"
main(int argc,char *argv[])
{
int sockfd;
struct sockaddr_in serv_addr,cli_addr;
pname=argv[0];
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("server:cant open datargram socket");
exit(1);
}
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(SERV_UDP_PORT);
if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
{
perror("server:cant bind local address");
exit(1);
}
printf("\nUDP server is waiting........");
fflush(stdout);
dg_echo(sockfd,(struct sockaddr *)&cli_addr,sizeof(cli_addr));
}
No comments:
Post a Comment