gpt4 book ai didi

c++ - 从套接字在线程中接收消息

转载 作者:行者123 更新时间:2023-11-30 01:12:57 24 4
gpt4 key购买 nike

我正在编写套接字程序并在线程中接收消息。但是我遇到了段错误。当我直接接收而不启动线程时,没有这样的问题(在评论部分显示)。

在下面的代码中,我直接接收并发送给客户端。部分代码如下所示:

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 个参数:

The pthread_create() function starts a new thread in the calling process. The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine(). (Source: man pthread_create)

主线程:

TCPStream *stream = acceptor->accept();
(...)
pthread_create( &sniffer_thread, NULL, connection_handler, stream)
// ^^^^^^

工作线程:

void *connection_handler(void *arg)
{
TCPStream* stream = (TCPStream *)arg;
// ^^^^^^^^^^^^^^^^

关于c++ - 从套接字在线程中接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33144476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com