简体   繁体   English

C ++类网络编程的分段错误

[英]Segmentation Fault with C++ class network programming

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. 我正在尝试编写一个使用类,启动和侦听网络连接的c ++程序,然后为每个新客户端旋转一个新线程。

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. 幸运的是,我已经弄清楚如何从类中生成一个线程,但是当我尝试在类中执行一个accept()时,我会遇到一个分段错误。 I'll post the code to make it a little easier to show where I'm having issues. 我将发布代码,以便更容易地显示我遇到问题的地方。

#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);
}

Main isn't that exciting: 主要不是那么令人兴奋:

#include "network.h"
using namespace std;

int main()
{
  network my_network;

  my_network.my_listen();
}

What is interesting is that I can get all of this to work without using a class. 有趣的是,我可以在不使用课程的情况下完成所有这些工作。 I'm sure it has something to do with scope, but I don't know why. 我确定它与范围有关,但我不知道为什么。

By the way, I'm using gcc 4.6.2 with a target platform of x86_64 顺便说一下,我正在使用gcc 4.6.2和x86_64的目标平台

You declare new_socket_desc as a pointer but never set what it is pointing to. 您将new_socket_desc声明为指针,但从不设置它指向的内容。 When you try to put a value at the location of the pointer (with the result of your accept call) it is writing to some random location in memory, which may or may not cause an immediate crash (in this version of your code it is causing a crash) but is always very bad. 当你试图在指针的位置放一个值(带有接受调用的结果)时,它会写入内存中的某个随机位置,这可能会也可能不会立即导致崩溃(在此版本的代码中,它是导致崩溃)但总是非常糟糕。

Make new_socket_desc a regular int member of your class, use the & operator on it when creating the thread, and don't use the * operator on it otherwise, and you should have better luck. 使new_socket_desc成为类的常规int成员,在创建线程时使用&运算符,否则不要使用*运算符,你应该有更好的运气。

int *new_socket_desc;
// ....
*new_socket_desc = accept(socket_desc ....

You declaring new_socket_desc as a unitialized pointer, then assigning result of accept to random memory location pointed by new_socket_desc . 您声明new_socket_desc为未初始化指针,然后分配的结果, accept由随机指向的内存位置new_socket_desc So obviously this causes SEGFAULT. 显然,这会导致SEGFAULT。 You should do: 你应该做:

int new_socket_desc;
// ....
new_socket_desc = accept(socket_desc ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM