Thursday, March 13, 2008

UNIX NETWORK PROGRAMS

MEANT FOR MCA II YEAR TELANGANA UNIVERSITY NIZAMABAD


//iterative unix client
#include "files.h"
int main(int argc,char *argv[])
{
int sockfd,servlen;
struct sockaddr_un serv_addr;
bzero((char *) &serv_addr,sizeof(serv_addr));
serv_addr.sun_family=AF_UNIX;
strcpy(serv_addr.sun_path,UNIXSTR_PATH);
servlen=strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);

/* Open a socket */

if(sockfd=(socket(AF_UNIX,SOCK_STREAM,0))<0)
perror("Client: Can't open socket");
/* Connect to the server */

if(connect(sockfd,(struct sockaddr *) &serv_addr,servlen)<0)
perror("Client:Can't connect to server");
str_cli(stdin,sockfd);
exit(0);
}

/* Example of Iterative server using UNIX domain stream protocol.*/

#include "files.h"
main(int argc,char *argv[])
{
int sockfd,newsockfd,clilen,childpid,servlen;
struct sockaddr_un cli_addr,serv_addr;
pname=argv[0];
if((sockfd=socket(AF_UNIX,SOCK_STREAM,0))<0)
perror("server: cant open stream socket");
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sun_family=AF_UNIX;
strcpy(serv_addr.sun_path,UNIXSTR_PATH);
servlen=strlen(serv_addr.sun_path)+sizeof(serv_addr.sun_family);
if((bind(sockfd,(struct sockaddr*)&serv_addr,servlen))<0)
{
perror("server:cant bind local address");
exit(1);
}
listen(sockfd,5);
for( ; ;)
{
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.....");
}

No comments: