[英]Receving message in thread from socket
我正在編寫套接字程序並在線程中接收消息。 但是我遇到了細分錯誤。 當我直接接收而不啟動線程時,就沒有這樣的問題(顯示在注釋部分)。
在下面的代碼中,我直接接收並將其發送給客戶端。部分代碼如下所示:
if (acceptor->start() == 0)
{
while (1)
{
stream = acceptor->accept();
if (stream != NULL)
{
/*
ssize_t len;
char line[256];
while ((len = stream->receive(line, sizeof(line))) > 0) {
line[len] = 0;
printf("received - %s\n", line);
stream->send(line, len);
*/
pthread_t sniffer_thread;
if( pthread_create( &sniffer_thread, NULL, connection_handler,NULL) < 0)
{
perror("could not create thread");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
pthread_join( sniffer_thread , NULL);
}
delete stream;
}
}
exit(0);
現在,我在線程函數中收到了同樣的消息。 顯示分段錯誤。
代碼如下所示。
void *connection_handler(void *arg)
{
TCPStream* stream = NULL;
ssize_t len;
char line[256];
while ((len = stream->receive(line, sizeof(line))) > 0)
{
line[len] = 0;
printf("received - %s\n", line);
stream->send(line, len);
}
}
Valgrind輸出的一部分是
==5163== Memcheck, a memory error detector
==5163== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==5163== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==5163== Command: ./appdownload 9999 192.5.60
==5163==
==5163== Thread 2:
==5163== Invalid read of size 4
==5163== at 0x401975: TCPStream::receive(char*, unsigned long, int) (tcpstream.cpp:30)
==5163== by 0x40177B: connection_handler(void*) (appdownload.cpp:68)
==5163== by 0x4E3F181: start_thread (pthread_create.c:312)
==5163== by 0x566947C: clone (clone.S:111)
==5163== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==5163==
==5163==
==5163== Process terminating with default action of signal 11 (SIGSEGV)
==5163== Access not within mapped region at address 0x0
==5163== at 0x401975: TCPStream::receive(char*, unsigned long, int) (tcpstream.cpp:30)
==5163== by 0x40177B: connection_handler(void*) (appdownload.cpp:68)
==5163== by 0x4E3F181: start_thread (pthread_create.c:312)
==5163== by 0x566947C: clone (clone.S:111)
==5163== If you believe this happened as a result of a stack
==5163== overflow in you`enter code here`r program's main thread (unlikely but
==5163== possible), you can try to increase the size of the
==5163== main thread stack using the --main-stacksize= flag.
==5163== The main thread stack size used in this run was 8388608.
TCPStream* stream = NULL; // HERE
ssize_t len;
char line[256];
while ((len = stream->receive(line, sizeof(line))) > 0) // HERE
您在NULL
指針上調用receive
。 Valgrind告訴您:
==5163== Thread 2:
==5163== Invalid read of size 4
==5163== at 0x401975: TCPStream::receive(char*, unsigned long, int) (tcpstream.cpp:30) // HERE
==5163== by 0x40177B: connection_handler(void*) (appdownload.cpp:68)
==5163== by 0x4E3F181: start_thread (pthread_create.c:312)
==5163== by 0x566947C: clone (clone.S:111)
==5163== Address 0x0 is not stack'd, malloc'd or (recently) free'd
// ^- HERE
如果需要將TCPStream*
從主線程傳遞到新線程函數,則可以使用pthread_create
第4個參數:
pthread_create()函數在調用過程中啟動一個新線程。 新線程通過調用start_routine()開始執行。 arg作為start_routine()的唯一參數傳遞。 (來源:
man pthread_create
)
主線程:
TCPStream *stream = acceptor->accept();
(...)
pthread_create( &sniffer_thread, NULL, connection_handler, stream)
// ^^^^^^
工作線程:
void *connection_handler(void *arg)
{
TCPStream* stream = (TCPStream *)arg;
// ^^^^^^^^^^^^^^^^
顯示了valgrind跟蹤之后,很明顯,您只是在null指針上調用了receive
。
嗯,讓我們看看。
char line[256]; // 256 bytes space
while ((len = stream->receive(line, sizeof(line))) > 0) // receive up to 256 bytes
{
line[len] = 0; // put 257th byte in
我猜您可能會再為0
使用一個字節-嘗試接收最大為sizeof(line)-1
字節。
或僅使用std::vector
,然后push_back
0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.