multithreading - Networking and multihreading in C++. Errors and how to make my code design better? -


i trying create simple local server accepts connections on different thread. trying create simple backup program files(kinda dumbed down cloud house). have setup networking things in different classes. getting error when trying run accept on different thread. have not included required includes here save space , have not completed final code right wanna make working can @ least accept connections when use main server class's accept(down below) without thread compiles doesn't work expected not wait connection request prints out -1 fd. when put thread following compiler error. thank time.

bipgen@genbox:~/documents/college projects/trans_lair$ g++ -std=c++11 server.cpp server_socket.cpp client.cpp in file included /usr/include/c++/5/thread:39:0,                  server.cpp:9: /usr/include/c++/5/functional: in instantiation of ‘struct std::_bind_simple<void (*(int))(server&)>’: /usr/include/c++/5/thread:137:59:   required ‘std::thread::thread(_callable&&, _args&& ...) [with _callable = void (&)(server&); _args = {int}]’ server.cpp:55:27:   required here /usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<void (*(int))(server&)>’        typedef typename result_of<_callable(_args...)>::type result_type;                                                              ^ /usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<void (*(int))(server&)>’          _m_invoke(_index_tuple<_indices...>) 

my classes follows:

#ifndef server_socket_h #define server_socket_h #endif #include<sys/socket.h>  //creating simle server socket class  class server_socket { protected:     int fd;     struct addrinfo hints, *res;     void setup_structure(int port);     void setup_socket();  public:     server_socket(int port = 6666); }; 

it's corresponding cpp file

/* *it's implementioan * */ #include "server_socket.h"  void server_socket :: setup_structure(int port) {    memset(&hints, 0 , sizeof hints);     hints.ai_family = af_unspec;    hints.ai_socktype = sock_stream;    hints.ai_flags = ai_passive;     int status;    std::string s = std::to_string(port);    char const *charport = s.c_str();     if( (status = getaddrinfo(null, charport,&hints, &res)) != 0 )    {         //throw    } }  void server_socket :: setup_socket() {     if(fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol ) < 0 )     {         //throw     } }  server_socket :: server_socket(int port) {     setup_structure(port);     setup_socket(); } 

a class hold clients , methods

#ifndef client_h #define client_h #endif  #include<sys/types.h> #include<sys/socket.h>  class client {     protected:     int fd;      public:     client(int &fd);     int read(char* buffer);     int write(char* buffer); }; 

it's cpp file

#include "client.h"  using namespace std;  client :: client(int &fd) {     this->fd = fd; }  int client :: read(char* buffer) {     int status;     status = read(fd,buffer,0);     if( status < 0 )     {         //throw     }     return status; }  int client :: write(char* buffer) {     int status;      status = send(fd ,buffer, sizeof(buffer),0);     if( status < 0 )     {         //throw     }     return status; } 

finally server class main class

#include "server_socket.h" #include<vector> #include <mutex> #include "client.h"  class server : public server_socket {     protected:     std::vector <client> conn_list;     fd_set readset;     fd_set writeset;      public:     server(int port = 6667,int backlog = 20);      friend void accept(server &a); }; 

it's main cpp file

#include "server.h"  using namespace std;  server :: server(int port,int backlog) : server_socket(port) {     //bind local socket     bind(fd,res->ai_addr,res->ai_protocol);      //listen local socket     listen(fd, backlog);     cout<<"server initialized @ port: "<<port<<endl; } std :: mutex m;  void  accept(server &a) {     struct sockaddr client;     socklen_t client_size;      while(1)     {         int new_conn_fd = accept(a.fd,&client,&client_size);         if( new_conn_fd < 0 )         {             //throw         }        // m.lock();         client s(new_conn_fd);         a.conn_list.push_back(new_conn_fd);         cout<<"accepted: "<<new_conn_fd;        // m.unlock();      } }   int main() {     server s;     //accept(s); compiles prints -1 infinitely     std::thread t(accept,5); //does not compile     if(t.joinable())     t.join(); } 


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -