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 8330137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:01:39+00:00 2026-06-09T02:01:39+00:00

I’m trying to implement a TCP client in C which needs to work as

  • 0

I’m trying to implement a TCP client in C which needs to work as follows:

  • Ability to open a connection to a given server
  • Ability to send arbitrary data to the server through the established connection, and
  • Ability to receive arbitrary data from the server (think of it as responses to the ‘questions’ that my client sent to the server).

For example, the client should be able to open a connection to an arbitrary HTTP server, send the ‘HEAD’ message and print the response that arrives from the HTTP server.

(My goal is to create a generic ‘TCP client plugin’ for a specific software environment that I’m using for my daily work and which lacks networking abilities. I know the SDK of my environment quite well, but I don’t really have a deep experience in socket programming.)

Currently, I have 2 separate threads for sending and receiving data. The workflow of the receiver thread (which starts automatically as soon as the server address & port is set by the user) is the following one (here I only mention the main sequence of socket calls):

globalSocket = socket(); // Create socket and store it globally
bind(); // Bind the local port
connect(); // Connect to remote host & port
listen(); // Listen to the socket
while (isAlive) {
  select(... &readfds ...); // Check for ready reader descriptors
  accept(); // Accept the incoming connection
  recv(); // Receive data from server
}
close(); // End the connection

The sender thread is quite simple, it uses the globalSocket created by the receiver thread to execute the send() command.

Now, here’s my problem: I can open connections to remote servers without any problem. I can also send any arbitrary data without any problem (I confirmed that the data that I send actually arrives to the server side without problems). However, I can’t get any data back from the server. After some tests, it seems that select would never return with a positive value.

I tried a lot of modifications in my code (like changing parameters to select, omitting listen etc.), I read Beej’s guide at least 10 times during this very day and tried every ad-hoc change that I just could imagine, but the behaviour is still the same. Therefore, before I start asking specific questions with specific code excerpts, I’d like to know whether my approach to this problem was correct or whether I’m having some serious conceptual problems here.

Thanks indeed for your answers,

Ádám

P.S. I couldn’t post this codepart in the comments as it is too long; here’s the code snippet that manages the select – accept – recv cycle:

while ( thread->isActive ) {
   // Accept connection
   timeVal.tv_sec = TIMEOUT_SEC;
   timeVal.tv_usec = TIMEOUT_USEC;
   FD_ZERO( & fileDescriptor );
   FD_SET( socketDescriptor, & fileDescriptor ); // socketDescriptor is the global socket
   result = select ( FD_SETSIZE, & fileDescriptor, NULL, NULL, & timeVal );
   if ( result > 0 ) {
      post ( "select" ); // This line is actually never reached
      connectionDescriptor = accept ( socketDescriptor, ( struct sockaddr * ) & clientAddress, & clientLength ); // connectionDescriptor is the local socket created by accept
      if ( connectionDescriptor < 0 ) { // Some error happened
         outlet_int ( thread->parent->statusOutlet, errno );
      } else {
         // Receive data
         data.clear ( ); // 'data' is an std::vector of chars that stores the incoming data and makes it accessible for the rest of the environment
         size = thread->parent->bufferSize;
         buffer = new unsigned char [ size ]; // this buffer is used for receiving the data from 'recv'
         receivedBytes = 1;
         while ( receivedBytes > 0 ) {
            receivedBytes = recv ( connectionDescriptor, ( char * ) buffer, size, 0 );
            if ( receivedBytes < 0 ) { // Socket error
               outlet_int ( thread->parent->statusOutlet, errno );
            }
            data.insert ( data.end ( ), buffer, buffer + receivedBytes );
         }
         delete [ ] buffer;
#ifdef WIN_VERSION
         closesocket ( connectionDescriptor );
#else
         close ( connectionDescriptor );
#endif
         // Output received data
         ... blah ... blah ... blah
      }
   }
}
  • 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-09T02:01:41+00:00Added an answer on June 9, 2026 at 2:01 am

    So, there are several independent issues here, which are split between several answers and comments, so I’m going to summarize them.

    The sender thread is OK. However, the connector/listener thread should look as follows:

    socket();
    connect();
    while (isAlive) {
       if (select(... &readfds ...) > 0) {
          recv();
       }
    }
    close();
    

    Regarding the originally posted code excerpt, the while cycle that was being used to handle recv is a conceptual error and should be removed, since it would block the thread – which would make the select statement unnecessary (since in that case, select would have no practical effect).

    It should also be pointed out that if recv returns 0, that means that the socket was closed by the remote peer, so in that case the while (isAlive) loop should be aborted even if isAlive was not set to false by the parent thread. However, an extra close statement is not needed inside the loop (which was also a conceptual error in the original code).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.