我正在尝试编写一个使用类、启动和侦听网络连接的 c++ 程序,然后为每个新客户端启动一个新线程。
幸运的是,我已经想出了如何从类中生成线程,但是当尝试在类中执行 accept() 时,我遇到了分段错误。我将发布代码,以便更容易地显示我遇到问题的地方。
#include <iostream>
#include <string.h> //for memset
#include <pthread>
#include <sys/types.h> //network
#include <sys/socket.h> //network
#include <netinet/in.h> //network
using namespace std;
class network
{
public:
void my_listen();
static void *handleClient(void * in_stream);
};
void* network::handleClient(void * in_stream)
{
int *stream = reinterpret_cast<int *>(in_stream);
write(*stream,"Hello Client\n", 12);
}
void network::my_listen()
{
/*
* Name: my_listen()
* Purpose: Listens and accepts new connections. Once accpeted, a new thread
* is spun off.
* Input: none
* Output: none
*/
int *new_socket_desc;
int port_num = 9876;
socklen_t client_addr_len;
int socket_desc = socket(AF_INET,SOCK_STREAM,0);
sockaddr_in serv_addr, cli_addr;
if(socket_desc == -1)
{
cerr << "Unable to create new sockets\n";
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port_num);
if(bind(socket_desc, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
cerr << "Error on binding " << port_num << endl;
}
listen(socket_desc,5);
client_addr_len = sizeof(cli_addr);
pthread_t thread[10];
int count = 0;
*new_socket_desc = accept(socket_desc, (struct sockaddr *) &cli_addr, &client_addr_len); //Right here I segment fault
cout << "Connected client " << " @ " << new_socket_desc << " (" << *new_socket_desc << ")" << endl;
pthread_create(thread[0], handleClient, (void *)new_socket_desc);
pthread_join(thread[0],NULL);
}
主要不是那么令人兴奋:
#include "network.h"
using namespace std;
int main()
{
network my_network;
my_network.my_listen();
}
有趣的是,我可以在不使用类的情况下完成所有这些工作。我确定它与范围有关,但我不知道为什么。
顺便说一句,我使用的是 gcc 4.6.2,目标平台为 x86_64