Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:04:28+00:00 2026-06-12T19:04:28+00:00

I am using Ubuntu 12.04 32 bit edition I wrote a program to receive

  • 0

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.

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??

I can provide some code:

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

The process TCP function is called in a loop

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:04:29+00:00Added an answer on June 12, 2026 at 7:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ubuntu 64-bit and trying to run a .asm file on NASM. But
I have a simple C++ program compiled using gcc 4.2.4 on 32-bit Ubuntu 8.04.
I am using ubuntu 12.04 on 64 bit machine, I have this simple C
I'm using GCC 4.3.3 on Ubuntu 9.04 64-bit and was getting errors using C++-style
I'm using Ubuntu 12.04 I tried to change my default apache2 port from 80
I'm using Ubuntu and Qt Creator 4 I have a .cpp program in the
I'm using Ubuntu 10.04 and Qt4.6, and I've created an executable binary file on
I'm using emacs23 on Ubuntu 32-bit 10.04/10.10 with GNOME and Compiz. Pressing M-RET in
I am using Ubuntu 10.10 (64 bit) with gcc and I wanted to use
I wanted to install ruby on my ubuntu 64 bit box using rvm but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.