数据没有被正确地从TCP套接字用C接收到的(闭合)(Data is not received cor

2019-09-24 01:19发布

我使用Ubuntu 12.04 32位版本我写了一个程序,从TCP客户机接收XML文件。 相同的程序从另一个进程由UNIX域套接字还接收数据。 对于我使用的民意调查()系统调用。

我的问题是,有些时候我没有得到正确的XML数据,或者一段时间内它缺少了。 但由于我使用TCP,如果有数据丢失的客户就知道了。 但客户没有显示任何错误。 有谁请告诉我为什么发生这种情况?

我可以提供一些代码:

int config_server_tcp(int port)
{   
    int sockfd = -1;
    struct sockaddr_in my_addr;                     // my address information
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    {
        perror("socket() failed.");
    }
    else
    {
        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(port);
        my_addr.sin_addr.s_addr = htonl(INADDR_ANY);    // automatically fill with my IP
        memset(&(my_addr.sin_zero), 0, 8);              // zero the rest of the struct
        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) 
        {
            perror("bind() failed.");
        }
        else
        {
            if (listen (sockfd, 8) == -1)
            {
                perror("listen() failed.");
            }
        }
    }
    return sockfd;
}

int send_to_tcp_server(unsigned char * message, int size, char * server_ip, int port) 
{
    int sockfd;
    struct sockaddr_in their_addr;
    int numbytes = -1;
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket() failed.");
    }
    else
    {
        their_addr.sin_family = AF_INET;
        their_addr.sin_port = htons(port);
        their_addr.sin_addr.s_addr=inet_addr(server_ip);
        memset(&(their_addr.sin_zero), '\0', 8);                // zero the rest of the struct
        if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof (their_addr)) == -1)
        {
            perror("connect() failed.");
        }
        else
        {
            if ((numbytes=send(sockfd , message, size, 0)) == -1) 
            {
                printf ("Sending failed.\n");
            }
        }
        close (sockfd);
    }
    return numbytes;
}


void process_tcp (int sock)
{
    struct sockaddr_in their_addr;                  // talker's address information
    int received;
    socklen_t addr_len;
    char buffer[BUFF_SIZE];

    addr_len = sizeof (their_addr);
    int clientfd = accept (sock, (struct sockaddr *)&their_addr, &addr_len);
    if (clientfd == -1)
    {
        perror("accept() failed.");
    }       
    else
    {
        do
        {
            received = recv(clientfd, buffer, BUFF_SIZE, 0);
            if (received == -1) 
            {
                perror("recv() failed.");
                break;
            }
            else
            {
                //do something
            }
        }
        while (received != 0);
        close (clientfd);
    }
}

该过程TCP函数被调用一个循环

Answer 1:

该缺陷是几乎可以肯定在代码中你没有显示,是根据你的XML-过TCP协议组装的应用级信息的代码位。 下面是做这件事:

void process_tcp (int sock)
{
    struct sockaddr_in their_addr;
    int received, total_received;
    socklen_t addr_len;
    char buffer[BUFF_SIZE];

    addr_len = sizeof (their_addr);
    int clientfd = accept (sock, (struct sockaddr *)&their_addr, &addr_len);
    if (clientfd == -1)
    {
        perror("accept() failed.");
    }       
    else
    {
        total_received = 0;
        do
        {
            received = recv(clientfd, buffer + total_received,
                            BUFF_SIZE - total_received, 0);
            if (received == -1) 
            {
                perror("recv() failed.");
                break;
            }
            if (received > 0)
                total_received += received;
        }
        while (received != 0);
        buffer[total_received] = 0;
        // here we can do something with 'buffer'              
        close (clientfd);
    }
}

需要注意的是很多错误检查的缺失。



文章来源: Data is not received correctly from TCP socket using C [closed]