udp下改写非阻塞模式的问题。
服务器端:
#include <sys/socket.h>
#include <sys/times.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
using namespace std;
#define PORT 9999
static void sig_alrm(int);
int pSeri, pAmount;
int j = 1;
int pecA, pecB;
int tempA = 1,tempB = 1;
int main(int argc, char **argv)
{
int sockfd;
int i,k;
int pDup, pRate;
int waitTim;
char buffer[1024];
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(PORT);
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
int len = sizeof(servaddr);
recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&cliaddr, (socklen_t *)&len);
sscanf(buffer, "%d %d %d %d %d", &i, &j, &pSeri, &pDup, &pRate );
printf("%d %d %d %d %dn", i, j, pSeri, pDup, pRate);
pAmount = pSeri * pDup;
waitTim = pAmount / pRate +10;
k = i;
if(signal(SIGALRM,sig_alrm) == SIG_ERR )
printf("signal(SIGALRM) error");
while(i < pSeri && j < pAmount)
{
recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&cliaddr, (socklen_t *)&len);
sscanf(buffer, "%d %d %d %d %d", &i, &j, &pSeri, &pDup, &pRate);
printf("%d %d %d %d %dn", i, j, pSeri, pDup, pRate);
if (k < i)
{
k = i;
tempA++;
}
tempB++;
alarm(5);
}
pecA = tempA * 100 / pSeri;
pecB = tempB * 100 / pAmount;
printf("%d serial packets received.n", tempA);
printf("%d total packets received.n", tempB);
printf("%d%% packets of serial received.n", pecA);
printf("%d%% packets of total received.n", pecB);
alarm(0);
exit(0);
}
static void sig_alrm(int signo)
{
pecA = tempA * 100 / pSeri;
pecB = tempB * 100 / pAmount;
printf("%d serial packets received.n", tempA);
printf("%d total packets received.n", tempB);
printf("%d%% packets of serial received.n", pecA);
printf("%d%% packets of total received.n", pecB);
exit(0);
}
客户端:
#include <sys/socket.h>
#include <sys/times.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define PORT 9999
static void wait(long);
int main(int argc, char **argv)
{
int sockfd;
int i, j;
int pSeri, pRate, pAmount;
int pDup;
char buffer[1024];
struct sockaddr_in servaddr, cliaddr;
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
servaddr.sin_port = htons(PORT);
int len =sizeof(servaddr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
pSeri = atoi(argv[2]);
pDup = atoi(argv[3]);
pRate = atoi(argv[4]);
for(i = 0; i < pSeri; i++)
{
for(j = 0; j < pDup ; j++)
{
sprintf(buffer,"%d %d %d %d %d", i, j, pSeri, pDup, pRate);
sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&servaddr, len);
wait(pRate);
bzero(buffer,sizeof(buffer));
}
}
}
void wait(long freq)
{
long sec=sysconf(_SC_CLK_TCK);
long tick=sec/freq;
clock_t c1=times(NULL);
while(1)
{
clock_t c2=times(NULL);
if((c2-c1)>tick)
return;
}
}
实现的是客户端往服务端发送udp包,同时靠客户端的参数指定发送包的数目,复制包数,每秒发送数(这个函数略有问题) 服务器接受后统计包数并输出 但是如果在客户端把循环中 wait(pRate);这一行注释掉,并且发送包数较大的情况,那么服务器丢包现象会很严重 请教各位高手如何把服务器改写成非阻塞模式。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论