0

我正在创建一个简单的服务器/客户端 UDP 套接字程序,但遇到了问题。

问题是 recvfrom() 函数不断地重新读取最后发送的数据。

因此,如果我从客户端向服务器发送两个数据包,那么 recvfrom() 将读取第一个数据包并打印其数据,然后它将不断地一遍又一遍地读取第二个数据包。

据我了解,一旦执行了成功的读取操作,就应该从套接字中删除数据包,但这似乎并没有发生。

我知道客户端不会重新发送数据这一事实,因为每当客户端发送数据时都会写入控制台输出。

这是发件人的功能

int sendPacket(int socketFd, int type, char *typeString, char *data, int seq, int     windowSize, struct sockaddr_in serverName) {

    struct packet *sendPacketPtr, sendPacket;
    fd_set writeFdSet;

    sendPacketPtr = &sendPacket;

    sendPacket.flags = type;
    sendPacket.windowsize = windowSize;
    sendPacket.seq = seq;
    sendPacket.id = getpid();

    if (type == NORMAL)
        strcpy(sendPacket.data, data);

    FD_ZERO(&writeFdSet);
    FD_SET(socketFd, &writeFdSet);

    if(select(FD_SETSIZE, NULL, &writeFdSet, NULL, NULL) > 0) {
        if(sendto(socketFd, sendPacketPtr, sizeof(sendPacket), 0, (struct sockaddr     *)&serverName, sizeof(serverName)) < 0) {
            printf("%s packet was not sent\n", typeString);
            perror("Send error");
        } else {
            printf("%s packet was sent\n", typeString);
        }
    }

    return 0;
}

使用此循环调用它,该循环针对其给定值运行两次。

for (int i = seqBase; i <= seqMax && i < packetNum; i++) {
      sendPacket(socketFd, NORMAL, "DATA", dataArray[i], i, 0, serverName);
}

以及接收功能

struct packet receivePacket (int socketFd, struct sockaddr_in *address, int timeout,     int useTimeout) {

    struct packet buffer;
    fd_set readFdSet;
    struct sockaddr_in dest_addr;

    struct timeval selectTimeout;
    selectTimeout.tv_usec = 0;
    selectTimeout.tv_sec = timeout;

    FD_ZERO(&readFdSet);
    FD_SET(socketFd, &readFdSet);

    socklen_t len = sizeof(dest_addr);

    if(useTimeout == 0) {
        if(select(FD_SETSIZE, &readFdSet, NULL, NULL, NULL) > 0) {
            if(recvfrom(socketFd, &buffer, sizeof(buffer), 0, (struct sockaddr     *)&dest_addr, &len) < 0) {
                perror("Read error");
            }else {
                *address = dest_addr;
            }
        }
    }else {
        if(select(FD_SETSIZE, &readFdSet, NULL, NULL, &selectTimeout) > 0) {
            if(recvfrom(socketFd, &buffer, sizeof(buffer), 0, (struct sockaddr     *)&dest_addr, &len) < 0) {
                perror("Read error");
            }else {
                *address = dest_addr;
            }
        }
    }

    return buffer;

}

从服务器使用此循环调用

receivedPacket = receivePacket(socketFd, &destAddress, 0, 0);
while (receivedPacket.flags == NORMAL) {
      printf("Data received: \"%s\", sequence: %d\n", receivedPacket.data, receivedPacket.seq);
      receivedPacket = receivePacket(socketFd, &destAddress, TIMEOUT, 1);
}

永远运行的输出(因为它不断重新读取最后一个数据包)是:

Data received: "Packet 0", sequence: 0
Data received: "Packet 1", sequence: 1
Data received: "Packet 1", sequence: 1
Data received: "Packet 1", sequence: 1
Data received: "Packet 1", sequence: 1
........
4

1 回答 1

2
  1. 在失败的情况下(即如果 recvfrom 或当 select 在超时时返回),您还将返回未初始化的“缓冲区”,因此内容可以是先前的数据,但行为未定义。所以最好在调用 recvfrom 之前将其初始化为 0 (memset(&buffer,0,sizeof(struct packet)))

  2. 在 while 循环中,您正在检查 (receivedPacket.flags == NORMAL) ,如果 NORMAL
    则只有您正在打印缓冲区并再次调用该函数。

    在选择超时或 recvfrom 失败的“receivePacket”函数中,将“receivedPacket.flags”显式更新为 NORMAL,这样您的 while 循环就会中断。

于 2014-03-19T04:59:55.860 回答