Thursday, March 13, 2008

UNIX NETWORK PROGRAMS

/* Some of the files and definitions to be used in the socket programs*/

#include
#include
#include
#include
#include
#include
#include
#define SERV_UDP_PORT 6600
#define SERV_TCP_PORT 5000
#define SERV_HOST_ADDRESS "172.16.0.1"
#define MAXLINE 80
#define UNIXSTR_PATH "unix_sock"
#define UNIXDG_PATH "./unix_srv_dg"
#define UNIXDG_TMP "./dg.xxxxxx"
#define MAXMESG 2048
#define MAX 512
char *pname;

int readn(register int fd,register char *ptr,register int nbytes)
{
int nleft,nread;
nleft=nbytes;
while(nleft>0)
{
nread=read(fd,ptr,nleft);
if(nread<0)
return (nread);
else
if(nread==0)
break;
nleft-=nread;
ptr+=nread;
}
return (nbytes-nleft);
}


int writen(register int fd,register char *ptr,register int nbytes)
{
int nleft,nwritten;
nleft=nbytes;
while(nleft>0)
{
nwritten=write(fd,ptr,nleft);
if(nwritten<0)
return (nwritten);
nleft-=nwritten;;
ptr+=nwritten;
}
return (nbytes-nleft);
}


int readline(register int fd,register char *ptr, register int maxlen)
{
int n,rc;char c;
for(n=1;n {if((rc=read(fd,&c,1))==1){*ptr++=c;if(c=='\n') break;
}
else if(rc==0) {
if(n==1)return 0;
else break;}
else
return -1;}*ptr='\0';return (n);}
str_cli(register FILE *fp,register int sockfd)
{
int n;
char sendline[MAXLINE],recvline[MAXLINE+1];
printf("\nEnter a message: ");
fflush(stdout);
while(1)
{
fgets(sendline,MAXLINE,fp);
if(strlen(sendline)==1)
break;
n=strlen(sendline);
printf("\nClient is sending to server: %s",sendline);
if(writen(sockfd,sendline,n)!=n)
perror("str_cli:writen error from socket");
n=readline(sockfd,recvline,MAXLINE);
printf("Client received from server: %s",recvline);
printf("\nEnter a message to continue or press ENTER to terminate: ");
fflush(stdout);
if(n<0)
perror("str_cli:readline error");
// recvline[n]='\0';
}
if(ferror(fp))
perror("str_cli:error reading file");
}


str_echo(int sockfd)
{
int n;
char line[MAXLINE];
for(; ;)
{
n=readline(sockfd,line,MAXLINE);
printf("\nServer received from client: %s",line);
if(n==0)
return;
else
if(n<0)
perror("str_echo: readline error");
printf("Server echoing back to client:%s",line);
fflush(stdout);
if(writen(sockfd,line,n)!=n)
perror("str_echo:writen error");
}
}

dg_cli(FILE *fp,int sockfd,struct sockaddr *pserv_addr,int servlen)
{
int n;
char sendline[MAXLINE],recvline[MAXLINE+1];
printf("\nEnter a message to send to server: ");
while(fgets(sendline,MAXLINE,fp))
{
if((n=strlen(sendline))==1) exit(0);
printf("\nUDP client is sending to server: %s",sendline);
if(sendto(sockfd,sendline,n,0,pserv_addr,servlen)!=n)
perror("dg_cli:sendto error");
n=recvfrom(sockfd,recvline,MAXLINE,0,(struct sockaddr *)0,(int *)0);
if(n<0)
perror("dg_cli:recvfrom error");
recvline[n]='\0';
printf("UDP client received from: %s",recvline);
fflush(stdout);
printf("\nEnter thenext message to send or ENTER to terminate:");
}
if(ferror(fp))
perror("dg_cli:error reading file");
}


dg_echo(int sockfd,struct sockaddr * pcli_addr,int maxclilen)
{
int n,clilen;
char mesg[MAXMESG];
for( ; ;)
{
clilen=maxclilen;
n=recvfrom(sockfd,mesg,MAXMESG,0,pcli_addr,&clilen);
if(n<0)
perror("dg_echo:recvfrom error");
mesg[n]='\0';
printf("\nUDP server received from client and echoing: %s",mesg);
fflush(stdout);
if(sendto(sockfd,mesg,n,0,pcli_addr,clilen)!=n)
perror("dg_echo:sendto error");

}
}

1 comment:

Anonymous said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Celular, I hope you enjoy. The address is http://telefone-celular-brasil.blogspot.com. A hug.