Segmentation Fault with C++ class network programming-Collection of common programming errors
I’m trying to write a c++ program that uses classes, starts and listens for network connections, and then spins off a new thread for each new client.
Fortunately, I’ve figured out how to spawn a thread from inside a class, but when trying to do a accept() in the class I get a segmentation fault. I’ll post the code to make it a little easier to show where I’m having issues.
#include
#include //for memset
#include
#include //network
#include //network
#include //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(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