TCP Server on Linux + boost
Hey, Im writing small TCP server on Linux (first time). I'm using boost lib. And the code which was working on Windows isnt on Linux. I'm compiling using g++. Here is the source (btw any advices about structure, classes, performance from experienced coders welcome :) ).
Code:
#include "share.h"
struct sockaddr_in serv_addr, cli_addr;
class Client
{
private:
int ID, sock;
struct sockaddr_in clientinfo;
public:
boost::thread thread;
Client(){};
~Client(){};
void SetSock(int sock){this->sock = sock;}
void SetCInfo(struct sockaddr_in clientinfo){this->clientinfo = clientinfo;}
int GetID(){return this->ID;}
void SetID(int id){this->ID = id;}
bool ValidSock(){if (this->sock < 0) return 0; else return 1;}
};
class ClientHandler
{
private:
boost::thread ListenThread;
vector<Client> Clients;
public:
ClientHandler();
~ClientHandler(){}
int ServeClient(int ID);
int WaitClients(int ok);
bool StartListen();
};
ClientHandler CHandler;
class NetworkHandler
{
private:
bool shutdown;
bool InitSocket()
{
ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ListenSocket < 0)
{
printf("NetworkHandler::InitSocket()::Error\n");
return 0;
}
printf("NetworkHandler::InitSocket()\n");
return 1;
};
bool BindSocket(int port)
{
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(ListenSocket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
printf("NetworkHandler::BindSocket()::Error\n");
return 0;
}
printf("NetworkHandler::InitSocket()\n");
return 1;
};
bool StartListen()
{
if (listen(ListenSocket, SOMAXCONN) < 0)
{
printf("NetworkHandler::StartListen()::Error\n");
return 0;
}
printf("NetworkHandler::StartListen()\n");
return 1;
};
public:
int ListenSocket;
NetworkHandler(){};
~NetworkHandler(){};
bool StartServer(int port)
{
printf("NetworkHandler::StartServer(PORT %d)\n", port);
if (!InitSocket()) return 0;
if (!BindSocket(port)) return 0;
if (!StartListen()) return 0;
printf("NetworkHandler::ServerStatus -> Working!\n");
CHandler.StartListen();
return 1;
};
bool isShutDown(){return this->shutdown;}
void SetShutDown(bool shut){this->shutdown = shut;}
};
NetworkHandler NHandler;
bool ClientHandler::StartListen()
{
printf("ClientHandler::Initialize\n");
if (NHandler.ListenSocket >= 0)
{
boost::thread thread(&ClientHandler::WaitClients, CHandler, 0);
ListenThread = std::move(thread);
} else {
printf("ClientHandler::Listener::StartListen()::Error\n");
return 0;
}
return 1;
}
int ClientHandler::WaitClients(int ok)
{
printf("ClientHandler::Listener::StartListen()\n");
do
{
int NewSock = -1;
struct sockaddr_in cinfo;
int cilen = sizeof(struct sockaddr);
if (NHandler.ListenSocket < 0)
{
printf("ClientHandler::ListenSocket::Closed!\n");
printf("ClientHandler::WaitClients::ExitThread\n");
ExitThread(0);
}
Client tmp;
tmp.SetSock(accept(NHandler.ListenSocket, (LPSOCKADDR)&cinfo, &cilen));
if (!tmp.ValidSock)
{
if(!NHandler.isShutDown()) printf("ClientHandler::NewConnection::Failed!\n");
} else {
tmp.SetCInfo(cinfo);
printf("ClientHandler::NewConnection::Connected from %s\n", inet_ntoa(cinfo.sin_addr));
boost::thread thread(&ClientHandler::ServeClient, CHandler, Clients.Size()+1);
tmp.thread = std::move(thread);
}
} while (!NHandler.isShutDown());
return 0;
}
int ClientHandler::ServeClient(int ID)
{
//
}
int main()
{
if (!NHandler.StartServer(8383))
{
printf("NetworkHandler::ServerStatus -> Failed to Initialize!\n");
return 1;
}
return 0;
}
Errors :
Code:
server.cpp:26: error: ISO C++ forbids declaration of âvectorâ with no type
server.cpp:26: error: expected â;â before â<â token
/usr/include/boost/thread/detail/thread.hpp: In copy constructor âClientHandler::ClientHandler(ClientHandler&)â:
/usr/include/boost/thread/detail/thread.hpp:108: error: âboost::thread::thread(boost::thread&)â is private
server.cpp:23: error: within this context
server.cpp: In member function âbool ClientHandler::StartListen()â:
server.cpp:101: note: synthesized method âClientHandler::ClientHandler(ClientHandler&)â first required here
server.cpp:101: error: initializing argument 2 of âboost::thread::thread(F, A1, A2) [with F = int (ClientHandler::*)(int), A1 = ClientHandler, A2 = int]â
server.cpp: In member function âint ClientHandler::WaitClients(int)â:
server.cpp:122: error: âExitThreadâ was not declared in this scope
server.cpp:125: error: âLPSOCKADDRâ was not declared in this scope
server.cpp:126: error: could not convert âtmp.Client::ValidSockâ to âboolâ
server.cpp:126: error: in argument to unary !
server.cpp:131: error: âinet_ntoaâ was not declared in this scope
server.cpp:132: error: âClientsâ was not declared in this scope
In file included from /usr/include/boost/bind/bind.hpp:1596,
from /usr/include/boost/bind.hpp:22,
from /usr/include/boost/thread/detail/thread.hpp:20,
from /usr/include/boost/thread/thread.hpp:22,
from share.h:4,
from server.cpp:2:
/usr/include/boost/bind/bind_mf2_cc.hpp: In function âboost::_bi::bind_t<Rt2, boost::_mfi::mf1<R, T, B1>, typename boost::_bi::list_av_2<A1, A2>::type> boost::bind(boost::type<R>, R (T::*)(B1), A1, A2) [with Rt2 = void, R = int, T = ClientHandler, B1 = int, A1 = ClientHandler, A2 = int]â:
/usr/include/boost/thread/detail/thread.hpp:243: instantiated from âboost::thread::thread(F, A1, A2) [with F = int (ClientHandler::*)(int), A1 = ClientHandler, A2 = int]â
server.cpp:101: instantiated from here
/usr/include/boost/bind/bind_mf2_cc.hpp:48: error: no matching function for call to âboost::_bi::value<ClientHandler>::value(boost::_bi::value<ClientHandler>)â
/usr/include/boost/bind/bind.hpp:116: note: candidates are: boost::_bi::value<T>::value(const T&) [with T = ClientHandler]
/usr/include/boost/bind/bind.hpp:113: note: boost::_bi::value<ClientHandler>::value(boost::_bi::value<ClientHandler>&)
/usr/include/boost/bind/bind_mf2_cc.hpp:48: error: initializing argument 1 of âboost::_bi::list2<A1, A2>::list2(A1, A2) [with A1 = boost::_bi::value<ClientHandler>, A2 = boost::_bi::value<int>]â
In file included from /usr/include/boost/bind.hpp:22,
from /usr/include/boost/thread/detail/thread.hpp:20,
from /usr/include/boost/thread/thread.hpp:22,
from share.h:4,
from server.cpp:2:
/usr/include/boost/bind/bind.hpp: In constructor âboost::_bi::value<T>::value(const T&) [with T = ClientHandler]â:
/usr/include/boost/bind/bind_mf2_cc.hpp:48: instantiated from âboost::_bi::bind_t<Rt2, boost::_mfi::mf1<R, T, B1>, typename boost::_bi::list_av_2<A1, A2>::type> boost::bind(boost::type<R>, R (T::*)(B1), A1, A2) [with Rt2 = void, R = int, T = ClientHandler, B1 = int, A1 = ClientHandler, A2 = int]â
/usr/include/boost/thread/detail/thread.hpp:243: instantiated from âboost::thread::thread(F, A1, A2) [with F = int (ClientHandler::*)(int), A1 = ClientHandler, A2 = int]â
server.cpp:101: instantiated from here
/usr/include/boost/bind/bind.hpp:116: error: no matching function for call to âClientHandler::ClientHandler(const ClientHandler&)â
server.cpp:28: note: candidates are: ClientHandler::ClientHandler()
server.cpp:23: note: ClientHandler::ClientHandler(ClientHandler&)
In file included from /usr/include/boost/thread/thread.hpp:22,
from share.h:4,
from server.cpp:2:
/usr/include/boost/thread/detail/thread.hpp: In constructor âboost::detail::thread_data<F>::thread_data(F&&) [with F = boost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > >]â:
/usr/include/boost/thread/pthread/thread_heap_alloc.hpp:24: instantiated from âT* boost::detail::heap_new(A1&&) [with T = boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > > >, A1 = boost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > >]â
/usr/include/boost/thread/detail/thread.hpp:126: instantiated from âstatic boost::detail::thread_data_ptr boost::thread::make_thread_info(F&&) [with F = boost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > >]â
/usr/include/boost/thread/detail/thread.hpp:243: instantiated from âboost::thread::thread(F, A1, A2) [with F = int (ClientHandler::*)(int), A1 = ClientHandler, A2 = int]â
server.cpp:101: instantiated from here
/usr/include/boost/thread/detail/thread.hpp:44: error: no matching function for call to âboost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > >::bind_t(boost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > >)â
/usr/include/boost/bind/bind.hpp:870: note: candidates are: boost::_bi::bind_t<R, F, L>::bind_t(F, const L&) [with R = void, F = boost::_mfi::mf1<int, ClientHandler, int>, L = boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> >]
/usr/include/boost/bind/bind.hpp:865: note: boost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > >::bind_t(boost::_bi::bind_t<void, boost::_mfi::mf1<int, ClientHandler, int>, boost::_bi::list2<boost::_bi::value<ClientHandler>, boost::_bi::value<int> > >&)