使用C无法从TCP套接字正确接收数据 [英] Data is not received correctly from TCP socket using C

查看:43
本文介绍了使用C无法从TCP套接字正确接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ubuntu 12.04 32位版本我编写了一个程序来从TCP客户端接收XML文件.相同的程序也通过Unix域套接字从另一个进程接收数据.为此,我正在使用poll()系统调用.

I am using Ubuntu 12.04 32 bit edition I wrote a program to receive an XML file from a TCP client. The same program is receiving data from another process by a unix domain socket also. For that I am using the poll() system call.

我的问题是,有时我无法正确获取XML数据,或者有时也丢失了XML数据.但是由于我使用的是TCP,因此如果发生数据丢失,客户端就会知道.但客户端未显示任何错误.有人可以告诉我为什么会这样吗?

My problem is, some times I am not getting the XML data correctly or some time it was missing too. But since I am using TCP, if there is a data loss client will know. but client is not showing any error. Could anybody please tell me why this is happening??

我可以提供一些代码:


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函数被循环调用

The process TCP function is called in a loop

推荐答案

几乎可以肯定该错误是在您未显示的代码中,该代码根据您的XML-over-TCP组合了应用程序级消息协议.这是一种实现方法:

The bug is almost certainly in the bit of code you didn't show, the code that assembles an application-level message according to your XML-over-TCP protocol. Here's one way to do it:

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);
    }
}

请注意,缺少许多错误检查.

Note that a lot of error checking is missing.

这篇关于使用C无法从TCP套接字正确接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆