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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:10:35+00:00 2026-06-03T09:10:35+00:00

I’m creating a socket program to transfer data from one pc to another, but

  • 0

I’m creating a socket program to transfer data from one pc to another, but i have a problem when i send some binary data to process to the other side.
In this case i need one thread to listen the message socket while the data socket send the data.
So i discovered that the problem wasn’t the socket, the problem happens if i try to just write the data to the screen (no socket this time).
So i tried to flush the data using fflush(stdout) and no luck.
The codes work in this way.

Initialize the 2 sockets.
Initialize 2 threads.
  One to get the data back through the data socket.
  The other send the data.    
And while sending all the data one pthread_join(m_thread2) in the main function, because the data can take 1 second to be processed or one hour so i keep the program alive this way.

I created a smaller version using two thread to read and send to the screen and in the main just the while.

Code:

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

const int RCVBUFSIZE=2000;
char echoString[RCVBUFSIZE];
int recvMsgSize;
static void * _sendExec(void *instance);
static void * _recvExec(void *instance);
int main(){
  pthread_t m_thread, m_thread2;
  int merror, merror2;
  merror=pthread_create(&m_thread, NULL, _sendExec, NULL);
  merror2=pthread_create(&m_thread2, NULL, _recvExec, NULL);
  pthread_join(m_thread2, NULL);
}
static void * _sendExec(void *instance){
  int size;
  for(;;){
    while((size=read(fileno(stdin), echoString, RCVBUFSIZE))>0){
       write(fileno(stdout), echoString, size);
    }
    fflush(stdin);
    fflush(stdout);
    pthread_exit(0);
  }
}
static void * _recvExec(void *instance){
  while(1){
     //recvMsgSize=msgTmp->recv(buffer, RCVBUFSIZE)
     write(fileno(stdout), "", 0);
     sleep(1);
  }
}

if you try cat file.tar.gz | ./a.out | tar -zvt you can see that not all the data is showed on the screen, if i put on the main, remove the pthread_join its ok, the problem is i need the data back and it can take time.
It’s just like if i do an cat file.tar.gz | ssh root@server "tar -zvt".
The problem is, i just can kill the sendExec after receive all the data back using the recvExec, but it just flush the stdin to me after
Changed the code and removed the socket part, just to illustrate the problem

Thanks people

  • 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-03T09:10:36+00:00Added an answer on June 3, 2026 at 9:10 am

    In your example, tar is waiting for more input because you never provide an end-of-file indication. Try this:

    static void * _sendExec(void *instance){
      int size;
      for(;;){
        while((size=read(fileno(stdin), echoString, RCVBUFSIZE))>0){
           write(fileno(stdout), echoString, size);
        }
        fflush(stdin);
        fflush(stdout);
        fclose(stdout); // THIS IS THE LINE THAT FIXES THE SAMPLE PROGRAM
        pthread_exit(0);
      }
    }
    

    While adding fclose fixes your sample program, I wouldn’t necessarily recommend it in your main program. Your sample still has an infinite loop in it (in _recvExec) and will never terminate.

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

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.