有些人可以帮我解决这些代码吗?我试图使客户端和服务器异步通信。我的意思是客户端和服务器不会彼此等待(例如,当服务器或客户端从recvfrom()读取并且数据不存在时,它将获取最后一个(我命名为备份)。以下是代码:
客户端
...
/* Create a datagram/UDP socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet addr family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Set signal handler for SIGIO */
handler.sa_handler = SIGIOHandler;
/* Create mask that mask all signals */
if (sigfillset(&handler.sa_mask) < 0)
DieWithError("sigfillset() failed");
/* No flags */
handler.sa_flags = 0;
if (sigaction(SIGIO, &handler, 0) < 0)
DieWithError("sigaction() failed for SIGIO");
/* We must own the socket to receive the SIGIO message */
if (fcntl(sock, F_SETOWN, getpid()) < 0)
DieWithError("Unable to set process owner to us");
/* Arrange for nonblocking I/O and SIGIO delivery */
if (fcntl(sock, F_SETFL, O_NONBLOCK | FASYNC) < 0)
DieWithError("Unable to put server sock into non-blocking");
...
服务器 ...
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithError("socket() failed");
/* Set up the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interf*/
echoServAddr.sin_port = htons(echoServPort); /* Port */
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("bind() failed");
/* Set signal handler for SIGIO */
handler.sa_handler = SIGIOHandler;
/* Create mask that mask all signals */
if (sigfillset(&handler.sa_mask) < 0)
DieWithError("sigfillset() failed");
/* No flags */
handler.sa_flags = 0;
if (sigaction(SIGIO, &handler, 0) < 0)
DieWithError("sigaction() failed for SIGIO");
if (fcntl(sock, F_SETOWN, getpid()) < 0)
DieWithError("Unable to set process owner to us");
/* Arrange for nonblocking I/O and SIGIO delivery */
if (fcntl(sock, F_SETFL, O_NONBLOCK | FASYNC) < 0)
DieWithError("Unable to put client sock into non-blocking");
...
代码被编译并链接到任何问题,但它们不相互交换数据,为什么? ......某处有问题吗?
感谢您的回复,
PS:代码现已删除......
答案 0 :(得分:1)
检查你的端口,我认为它们最大化...应该是65535,这是端口号(16位)的最大值!
给它一个较小的数字,你应该没问题!
编辑:使用的最大端口数为65536,最大为16位,因为它是一个短整数。如果超过最大值,它将失败。为客户端和服务器提供一个大于1024且小于65536的任意端口号。
看一下Beej's Guide到socket编程......
希望这有帮助, 最好的祝福, 汤姆。
答案 1 :(得分:0)
在您的recv期间,您可以使用MSG_PEEK | MSG_DONTWAIT作为recv的选项,它允许您只检查是否有任何需要读取的数据。