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

  • Home
  • SEARCH
  • 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 8341427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:28:41+00:00 2026-06-09T05:28:41+00:00

I have 2 machines running a simple C TCP server that I have written

  • 0

I have 2 machines running a simple C TCP server that I have written for testing purposes, 1 with Fedora 16, the other with Ubuntu 11.10. My Fedora machine works perfectly but on the Ubuntu machine, recv() does not block. Please keep in mind that these machines are running the same exact code. Has anybody seen this before? Thanks

int TcpSocket::ReadFromClient(int socket, char* buf, int len)
{
    char *request = buf;
    int slen = len;

    int c = recv(socket, request, slen, 0);
    while((c > 0) && (request[c-1] != '\n'))
    {
        request += c;
        slen -= c;
        c = recv(socket, request, slen, 0);
    }

    if (c < 0)
    {
        return c;
    }
    else if(c == 0)
    {
        //Sending back an empty string
        buf[0] = '\0';
    }

    return len-slen;
}
  • 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-09T05:28:43+00:00Added an answer on June 9, 2026 at 5:28 am

    It looks like the intention of your code is to stop reading when a '\n' byte arrives. If that is the case then you need to read from the socket 1 byte at a time instead of using the entire available buffer size, especially since you are only checking the last byte of the buffer instead of checking every byte received.

    You should also change the loop logic to only call recv() in one place instead of two places. Your current implementation is calling recv() with slen=0 when the buffer is exhausted, which will set c=0 and nullify the first byte in the buffer.

    Try this instead:

    int TcpSocket::ReadFromClient(int socket, char* buf, int len)
    { 
        int slen = len;
        char ch;
    
        while (len > 0)
        {
            int ret = recv(socket, &ch, 1, 0); 
            if (ret > 0)
            {
                *buf = ch; 
                ++buf; 
                --len; 
    
                if (ch == '\n')
                    break;
            }
            else
            {
                if ((ret == 0) || (errno != EAGAIN))
                    return ret;
    
                fd_set readfd;
                FD_ZERO(&readfd);
                FD_SET(socket, &readfd);
    
                timeval tv;
                tv.tv_sec = 5;
                tv.tv_usec = 0;
    
                ret = select(socket+1, &readfd, NULL, NULL, &tv);
                if (ret < 0)
                    return ret;
    
                if (ret == 0)
                {
                    // timeout elapsed while waiting for data
                    // do something if desired...
                }
            } 
        } 
    
        return slen - len;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server that has several virtual machines running on it. I'm trying
I have 2 machines running same workers. One machine shoud be primary as it
I'm having a really strange problem. I have 2 virtual machines running ubuntu 11.4
We have a set of Mac machines (mostly PPC) that are used for running
Let's say I have two SQL Server databases running on separate machines, call them
I have a virtual machine running windows 2003 server. It is on a separate
I have a simple single threaded utility written in C# that inserts data into
I am running Linux Ubuntu 10.04 and I have a Windows 7 machine and
I have two programs written in C++ that use Winsock. They both accept TCP
I have a simple little command-line program, written in C#, running under .NET 4.0,

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.