UNIX网络编程学习(18)--UDP回射(服务器+客户端)程序

时间:2021-10-02 17:51:06

服务器端:

 

#include"unp.h"

int
main(int argc, char **argv)
{
intsockfd;

struct sockaddr_incliaddr, servaddr;

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));

}

#include"unp.h"voiddg_echo(int sockfd ,SA *pcliaddr,socklen_t clilen){    int     n;    socklen_t   len;    char    mesg[MAXLINE];    for( ; ;)    {        len = clilen;        n = Recvfrom(sockfd, mesg, MAXLINE ,0 ,pcliaddr,&len);        Sendto(sockfd,mesg,n, 0, pcliaddr ,len);    }}


客户端:

#include "unp.h"
#define DEST_IP "127.0.0.1"

int
main(int argc, char ** argv)
{
int sockfd;

struct sockaddr_in servaddr;

sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

bzero(&servaddr, sizeof(struct sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);

Inet_pton(AF_INET, DEST_IP, &servaddr.sin_addr);

dg_cli(stdin, sockfd, (SA *) &servaddr ,sizeof(servaddr));
exit(0);
}

#include "unp.h"voiddg_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);    }}


知道客户端临时端口号的任何进程都可以往客户发送数据报,这些数据报会与正常的服务器应答混杂。解决方法是修改recvfrom调用以返回数据报发送者的IP地址和端口号,保留来自数据报所发往服务器的应答,而忽略任何其他数据报。

#include "unp.h"

void
dg_cli(FILE *fp, int sockfd, const SA* pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE+1];
socklen_t len;
struct sockaddr *preply_addr;
preply_addr = Malloc(servlen);

while(Fgets(sendline, MAXLINE, fp) != NULL)
{
Sendto(sockfd, sendline, strlen(sendline), 0 , pservaddr, servlen);

len = servlen;

n = Recvfrom(sockfd ,recvline, MAXLINE, 0, preply_addr, &len);

if(len != servlen || memcmp(pservaddr, preply_addr, len) != 0)
{
printf("reply from %s (ignored)\n", Sock_ntop(preply_addr, len));
continue;
}

recvline[n] = 0;

Fputs(recvline, stdout);
}
free(preply_addr);
}