我正在创建一个套接字程序将数据从一台PC传输到另一台PC,但是当我发送一些二进制数据以处理到另一端时,我遇到了问题。在这种情况下,我需要一线程来侦听消息套接字,而数据套接字发送数据。所以我发现问题不在于套接字,如果我尝试将数据写入屏幕(这次没有套接字(,就会出现问题。所以我尝试使用 fflush(stdout( 刷新数据,但没有运气。代码以这种方式工作。
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.
我使用两个线程创建了一个较小的版本来读取并发送到屏幕,并且主要只是一会儿。
法典:
#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);
}
}
如果您尝试cat file.tar.gz | ./a.out | tar -zvt
,您可以看到并非所有数据都显示在屏幕上,如果我戴上主,请删除pthread_join没关系,问题是我需要数据回来,这可能需要时间。就像我做cat file.tar.gz | ssh root@server "tar -zvt"
一样.问题是,在使用 recvExec 接收所有数据后,我只能杀死 sendExec,但它只是在之后将标准清刷新给我更改了代码并删除了套接字部分,只是为了说明问题
谢谢大家
在您的示例中,tar
正在等待更多输入,因为您从未提供文件结束指示。试试这个:
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);
}
}
虽然添加fclose
修复您的示例程序,但我不一定建议在您的主程序中使用它。您的样本中仍然有一个无限循环(以_recvExec
为单位(,并且永远不会终止。