【问题标题】:Making asynchronous socket server and client working ( written in C)使异步套接字服务器和客户端工作(用C编写)
【发布时间】:2011-01-18 03:04:47
【问题描述】:

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

【问题讨论】:

    标签: c++ c sockets asynchronous nonblocking


    【解决方案1】:

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

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

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

    看看Beej's Guide到socket编程...

    希望这会有所帮助, 最好的祝福, 汤姆。

    【讨论】:

    • 感谢您的回复!两者都使用相同的端口。你的意思是我应该再给高一点?
    • @mk:不!两个端口应该是相同的,但是它们已经超过了最大端口号,最大 16 位是 65536,给它一个较低的端口,比如说...12345 客户端和服务器...
    • @tommieb75:非常感谢您的评论,因为我没有注意到端口高于限制。当我在 Solaris 上工作时,我虽然端口取决于操作系统,它是 32 位或 64 位...谢谢增益,我今天收到你的一个很好的评论...
    • 我把端口改成了2023,问题依旧。客户端和服务器不交换数据,尽管它们相互连接......
    • @mk - 他们连接是什么意思? UDP是无连接协议吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 2011-08-14
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多