{"id":2767,"date":"2022-08-30T15:27:36","date_gmt":"2022-08-30T15:27:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/08\/segmentation-fault-with-c-class-network-programming-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:27:36","modified_gmt":"2022-08-30T15:27:36","slug":"segmentation-fault-with-c-class-network-programming-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/segmentation-fault-with-c-class-network-programming-collection-of-common-programming-errors\/","title":{"rendered":"Segmentation Fault with C++ class network programming-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;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.<\/p>\n<p>Fortunately, I&#8217;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&#8217;ll post the code to make it a little easier to show where I&#8217;m having issues.<\/p>\n<pre><code>#include \n#include  \/\/for memset\n#include \n#include  \/\/network\n#include  \/\/network\n#include  \/\/network\n\nusing namespace std;\nclass network\n{\n  public:\n    void my_listen();\n    static void *handleClient(void * in_stream);\n};\n\nvoid* network::handleClient(void * in_stream)\n{\n\n  int *stream = reinterpret_cast(in_stream);\n  write(*stream,\"Hello Client\\n\", 12);\n}\n\nvoid network::my_listen()\n{\n  \/*\n   * Name: my_listen()\n   * Purpose: Listens and accepts new connections. Once accpeted, a new thread\n   *          is spun off. \n   * Input: none\n   * Output: none\n  *\/\n\n  int *new_socket_desc;\n  int port_num = 9876;\n  socklen_t client_addr_len;\n\n  int socket_desc = socket(AF_INET,SOCK_STREAM,0);\n\n  sockaddr_in serv_addr, cli_addr;\n\n  if(socket_desc == -1)\n  {\n    cerr<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2014-02-08 16:02:20. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8217;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. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2767","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2767"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2767\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}