0

我目前正在实现基于 Stop-and-Wait 协议的 UDP 服务器和客户端。完成服务器端后,我还没有开始做客户端。我想先测试我的服务器。

#include "headsock.h"

void str_ser(int sockfd); // transmitting and receiving function

int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in my_addr;

    //create socket
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        printf("error in socket\n");
        exit(1);
    }

    my_addr.sin_family = AF_INET; // Address family; must be AF_INET
    my_addr.sin_port = htons(MYUDP_PORT); // Internet Protocol (IP) port.
    my_addr.sin_addr.s_addr = INADDR_ANY; // IP address in network byte order. INADDR_ANY is 0.0.0.0 meaning "all the addr"
    // places nbyte null bytes in the string s. This function will be used to set all the socket structures with null values.
    bzero(&(my_addr.sin_zero), 8);

    // binds the socket to all available interfaces
    if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) {
        printf("error in binding\n");
        perror("socket error");
        exit(1);
    }

    while (1)
    {
        str_ser(sockfd); // send and receive
    }

    close(sockfd);
    exit(0);
}

void str_ser(int sockfd) // transmitting and receiving function
{   
    FILE *fp;
    char buf[BUFSIZE];
    char recvs[DATALEN];
    int end, n = 0;
    long lseek=0;
    struct ack_so ack;

    struct sockaddr_in addr;
    socklen_t len = sizeof(struct sockaddr_in);

    printf("receiving data!\n");

    while(!end)
    {
        // receive the packet
        if ((n = recvfrom(sockfd, &recvs, DATALEN, 0, (struct sockaddr *)&addr, &len)) == -1)
        {
            printf("error when receiving\n");
            exit(1);
        }

        // if it is the EoF
        if (recvs[n-1] == '\0')
        {
            end = 1;
            n--;
        }

        memcpy((buf+lseek), recvs, n);
        lseek += n;

        // up to here, successfully received a packet
        // send ACK back
        ack.num = 1;
        ack.len = 0;
        if ((n = sendto(sockfd, &ack, ack.len, 0, (struct sockaddr *)&addr, len)) == -1)
        {
            printf("ACK send error!\n");
            exit(1);
        }
    }


    if ((fp = fopen ("myUDPreceive.txt", "wt")) == NULL)
    {
        printf("File doesn't exit\n");
        exit(0);
    }

    fwrite (buf , 1 , lseek , fp); //write data into file
    fclose(fp);
    printf("A file received!\nThe total data received is %d bytes\n", (int)lseek);
}

当我运行服务器时,由于当前没有客户端,我希望看到服务器被困在while(!end)循环中。但是,当我运行它时,我最终得到了这个:

receiving data!
A file received!
The total data received is 0 bytes
receiving data!
A file received!
The total data received is 0 bytes
receiving data!
A file received!
The total data received is 0 bytes
receiving data!
A file received!
The total data received is 0 bytes
receiving data!

这意味着 EoF 检查器片段

    // if it is the EoF
    if (recvs[n-1] == '\0')
    {
        end = 1;
        n--;
    }

被处决。

但是我那里没有客户!为什么会这样?

4

1 回答 1

1
int end, n = 0;

初始化n但不是end,这意味着测试

while(!end)

几乎肯定会失败。您需要显式初始化这两个变量

int end = 0, n = 0;
于 2013-10-08T16:31:58.350 回答