/* udp simple server program to reverse a string */
# include "unp.h"
/* UDP echo server dg_echo function: */
void dg_echo(int sockfd,SA*pcliaddr,socklen_t clilen)
{
int n,i,j,swp;
socklen_t len;
char mesg[MAXLINE];
for(;;){
len=clilen;
n=Recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);
printf("From Client : %s reversing ..",mesg);
for(i=0,j=n-1;i<n/2;i++,j--)
{
swp=mesg[i];
mesg[i]=mesg[j];
mesg[j]=swp;
}
Sendto(sockfd,mesg,n,0,pcliaddr,len);
}
}
/*UDP echo server main function:*/
#include"unp.h"
int main(int argc,char **argv[])
{
int sockfd;
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(SERV_PORT);
Bind(sockfd,(SA*)&servaddr,sizeof(servaddr));
dg_echo(sockfd,(SA*)&cliaddr,sizeof(cliaddr));
}
/*=================================================*/
/*udp client program to reverse a string*/
# include "unp.h"
/* UDP ECHO Client: dg_cli function*/
void dg_cli(FILE *fp,int sockfd,const SA* pservaddr,socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE+1];
while ( Fgets(sendline,MAXLINE,fp)!=NULL)
{
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr,servlen);
n= Recvfrom(sockfd,recvline, MAXLINE,0,NULL,NULL);
recvline[n]=0;
Fputs(recvline, stdout);
}
}
/* UDP client main function: */
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc!=2)
err_quit("usage : udpcli <Ipaddress>");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family= AF_INET;
servaddr.sin_port = htons(SERV_PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA*) &servaddr, sizeof(servaddr));
exit(0);
}
/* use wrapsock.c in compilation */
# include "unp.h"
/* UDP echo server dg_echo function: */
void dg_echo(int sockfd,SA*pcliaddr,socklen_t clilen)
{
int n,i,j,swp;
socklen_t len;
char mesg[MAXLINE];
for(;;){
len=clilen;
n=Recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);
printf("From Client : %s reversing ..",mesg);
for(i=0,j=n-1;i<n/2;i++,j--)
{
swp=mesg[i];
mesg[i]=mesg[j];
mesg[j]=swp;
}
Sendto(sockfd,mesg,n,0,pcliaddr,len);
}
}
/*UDP echo server main function:*/
#include"unp.h"
int main(int argc,char **argv[])
{
int sockfd;
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(SERV_PORT);
Bind(sockfd,(SA*)&servaddr,sizeof(servaddr));
dg_echo(sockfd,(SA*)&cliaddr,sizeof(cliaddr));
}
/*=================================================*/
/*udp client program to reverse a string*/
# include "unp.h"
/* UDP ECHO Client: dg_cli function*/
void dg_cli(FILE *fp,int sockfd,const SA* pservaddr,socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE+1];
while ( Fgets(sendline,MAXLINE,fp)!=NULL)
{
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr,servlen);
n= Recvfrom(sockfd,recvline, MAXLINE,0,NULL,NULL);
recvline[n]=0;
Fputs(recvline, stdout);
}
}
/* UDP client main function: */
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc!=2)
err_quit("usage : udpcli <Ipaddress>");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family= AF_INET;
servaddr.sin_port = htons(SERV_PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA*) &servaddr, sizeof(servaddr));
exit(0);
}
/* use wrapsock.c in compilation */
No comments:
Post a Comment