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() { //create tcp socket and establish a connection TCPServerSocket tcpServerSocket; int ret = tcpServerSocket.initialize(); ASSERT(ret == 0); int serverUdpPort = tcpServerSocket.negotiate(); ASSERT(serverUdpPort > 1024); tcpServerSocket.close(); //open a udp socket and listen for str UDPSocket udpSocket; ret = udpSocket.open(serverUdpPort); ASSERT(ret == 0); LOG("Server opened UDP Port %i\n", serverUdpPort); char buf[1024]; unsigned long remoteIp; int remotePort; //read, reverse string and send it back to client int readBytes = udpSocket.read(buf, 1024, remoteIp, remotePort); buf[readBytes] = '\0'; ASSERT(readBytes > 0); LOG("Server Received: %s\n", buf); //Reverse the string and write to the buffer again str_reverse(buf); int writeBytes = udpSocket.write(remoteIp, remotePort, (const char *) buf, readBytes); ASSERT(writeBytes > 0); LOG("Server sent: %s\n", buf); udpSocket.close(); 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; }
void runServer(const char * host, int port, bool withBlocking) { printf("Setting up a server socket to %s:%d\n", host, port); char buf[1024]; TCPServerSocket server; int i, err = server.open(host, port, 5); // err = server.open(0, port, 5) if(err) { const int msgSize = 128; char msgBuf[msgSize] = ""; printf("%s cannot open server socket to %s:%d -> %s\n", appname, host, port, strerror_s(msgBuf, msgSize, err)); return; } for(i = 0; i < 10; i++) { puts("Waiting for a connection"); while(withBlocking && !server.isPendingConnection(1000000)) { printf("."); fflush(0); } TCPSocket * socket = server.accept(); if(!socket) { Radiant::error("Could not create accept a socket connection."); return; } socket->setNoDelay(true); printf("Got a new socket %p\n", socket); fflush(0); for(int j = 0; j < iterations; j++) { int32_t len = 0; buf[0] = '\0'; int n = socket->read( & len, 4); if(n != 4) { Radiant::error("Could not read 4 header bytes from the socket, got %d", n); delete socket; return; } n = socket->read(buf, len); if(n != len) { Radiant::error("Could not read %d data bytes from the socket, got %d", len, n); } printf("Received \"%s\"\n", buf); if(withreplies) { socket->write( & len, 4); socket->write( & buf, len); } } delete socket; } fflush(0); info("%s %d clients handled, returning", appname, i); }