int main(int argc, char** argv){ char* hname; char* sname; debug_lib::init(argv[0]); if ( argc == 2 ){ hname = NULL; sname = argv[1]; }else if ( argc == 3 ){ hname = argv[1]; sname = argv[2]; }else{ debug_lib::log( "exit: wrong arguments passed %d\n", argc ); print_help(); exit(1); } try{ TCPServerSocket* sock = new TCPServerSocket(); sock->bind(hname, sname); sock->listen(); do{ while( true ){ TCPSocket* acceptsock = sock->accept(); MessageFramer* framer = new FixedLenFramer(sizeof(nowait_struct)); TCPAcceptSocketNoWait* acceptsocknw = new TCPAcceptSocketNoWait( acceptsock->getFd(), framer); Message msg; msg.setFramer(framer); struct nowait_struct stmsg; strcpy( stmsg.str, "hello world in a structure" ); msg.putDataRaw( (char*)&stmsg, sizeof(nowait_struct) ); acceptsocknw->send( msg ); //delete only closes the socket from the server side. The socket is still open on the client side. delete acceptsock; delete framer; delete acceptsocknw; } } while(1); }catch(debug_lib::Exception& e){ debug_lib::log( "exit due to error in server: %s", e.what()); exit(1); }catch(...){ debug_lib::log( "exit due to error in server: %s", "unknown error" ); exit(1); } return 0; }
int main(int argc, char** argv){ char* hname; char* sname; debug_lib::init(argv[0]); if ( argc == 2 ){ hname = NULL; sname = argv[1]; }else if ( argc == 3 ){ hname = argv[1]; sname = argv[2]; }else{ debug_lib::log( "exit: wrong arguments passed %d\n", argc ); print_help(); exit(1); } try{ TCPServerSocket* sock = new TCPServerSocket(); sock->bind(hname, sname); sock->listen(); do{ TCPSocket* acceptsock = sock->accept(); service(acceptsock); //delete only closes the socket from the server side. The socket is still open on the client side. delete acceptsock; } while(0); }catch(debug_lib::Exception& e){ debug_lib::log( "exit due to error in server: %s", e.what()); exit(1); }catch(...){ debug_lib::log( "exit due to error in server: %s", "unknown error" ); exit(1); } return 0; }
int main() { #ifdef OS_WIN32 WSAData wsaData; int nCode; assert( WSAStartup(MAKEWORD(2, 2), &wsaData) == 0); #endif int sock = socket(AF_INET, SOCK_STREAM, 0); TCPServerSocket server (sock); TCPSocketAddress localAddr("127.0.0.1", 10001); if (server.bind(&localAddr)) { std::cout << server.getLastError().getErrorString() << std::endl; return -1; } if (server.listen(10)) { perror("While listening"); return -2; } TCPSocketAddress *addr = 0; server.poll(IODevice::POLL_READ,-1); TCPSocket *socket = (TCPSocket *)(server.accept((SocketAddress **)&addr)); if (socket == NULL) { std::cout << server.getLastError() << std::endl; return -3; } std::cout << "Get remote "<<socket->getPeerAddress()->getReadable()<<std::endl; socket->close(); server.close(); return 0; }