1

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

4

2 回答 2

1

检查您的端口,我认为它们已达到极限……应该是 65535,这是端口号(16 位)的最大值!

给它一个较小的数字,你应该没问题!

编辑:使用的最大端口数为 65536,最大 16 位,因为它是一个短整数。如果超过最大值,它将失败。为客户端和服务器提供大于 1024 且小于 65536 的任意端口号。

看看Beej 的套接字编程指南......

希望这会有所帮助,最好的问候,汤姆。

于 2010-02-18T00:17:17.230 回答
0

在您的接收期间,您可以使用 MSG_PEEK | MSG_DONTWAIT 作为 recv 的选项,这将允许您检查是否有任何需要读取的数据。

于 2010-02-18T00:17:42.273 回答