有人可以帮我处理这些代码吗?我正在尝试使客户端和服务器进行异步通信。我的意思是客户端和服务器都不会互相等待(例如,当服务器或客户端从 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:代码现已删除...