int main(int argc, char **argv) { std::ofstream file; file.open("log.txt"); ServerHost server = HostBuilder::newInstance() .setLogFileWriter(file) .setAddress("127.0.0.1") .setAddressFamilyIPv4() .setPortNumber(12345) .setProtocolUDP() .setSocketTypeDGRAM() .buildServer(); std::thread serverThread(&ServerHost::listenRequest,server); HostBuilder::newInstance() .setAddress("127.0.0.1") .setPortNumber(8888) .setAddressFamilyIPv4() .setProtocolUDP() .setSocketTypeDGRAM() .buildClient() .connectServer(server.getServerSocketAddress()); serverThread.join(); system("pause"); }
int main(int argc, char* argv[]) { po::options_description desc("Allowed options"); desc.add_options() ("help,h", "Display this help message") ("server,s", "Server"); po::variables_map vm; po::store(po::command_line_parser(argc, argv).options(desc).run(), vm); po::notify(vm); if (vm.count("help")) { std::cout << "Looks like you need help." << std::endl; } //Launch server only if (vm.count("server")) { } else { //Run the game server locally std::thread serverThread(createServer); serverThread.detach(); } Application game; return 0; }
/** * Use std::thread to initiate peloton server and pqxx client in separate * threads * Simple query test to guarantee both sides run correctly * Callback method to close server after client finishes */ TEST_F(SSLTests, BasicTest) { EXPECT_EQ(0, chmod(client_key.c_str(), S_IRUSR)); peloton::PelotonInit::Initialize(); LOG_INFO("Server initialized"); peloton::network::PelotonServer peloton_server; int port = 15721; try { peloton::network::PelotonServer::certificate_file_ = server_crt; peloton::network::PelotonServer::private_key_file_ = server_key; peloton::network::PelotonServer::root_cert_file_ = root_crt; peloton::network::PelotonServer::SSLInit(); peloton_server.SetPort(port); peloton_server.SetupServer(); } catch (peloton::ConnectionException &exception) { LOG_INFO("[LaunchServer] exception in thread"); } std::thread serverThread([&] { peloton_server.ServerLoop(); }); // server & client running correctly TestRoutine(port); peloton_server.Close(); serverThread.join(); LOG_INFO("Peloton is shutting down"); peloton::PelotonInit::Shutdown(); LOG_INFO("Peloton has shut down"); }
int ServerPic::Run(int argc, char *argv[]) { CThread serverThread(&ServerPic::_StartServer, this); serverThread.Detach(); // Mainloop while (!serverThread.IsOver()) { _cmdMgr->Update(); sleep(2); } return 0; }
int main(int argc, char *argv[]) { SifInitRpc(0); SifLoadModule("host:ps2ips.irx", 0, NULL); if(ps2ip_init() < 0) { printf("ERROR: ps2ip_init falied!\n"); SleepThread(); } serverThread(); return 0; }
int main(int argc, char** argv) { int port; std::string host; std::string id = ""; namespace po = boost::program_options; po::options_description desc("Options"); desc.add_options() ("help", "Print help messages") ("port,p", po::value<int>(&port)->required(), "port") ("host,p", po::value<std::string>(&host)->required(), "hostname") ("id", po::value<std::string>(&id), "id"); po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); if (vm.count("help")) { std::cout << "Basic Command Line Parameter App" << std::endl << desc << std::endl; return 1; } po::notify(vm); // throws on error, so do after help in case there are any problems } catch(po::error& e) { std::cerr << "ERROR: " << e.what() << std::endl << std::endl; std::cerr << desc << std::endl; return 1; } std::thread serverThread(bind(startServer, host, port, id)); // ... serverThread.join(); return 0; }
void on_init() { #ifndef NDEBUG AllocConsole(); freopen("CONIN$", "r", stdin); freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); #endif try { std::thread serverThread(serverFunc); serverThread.detach(); } catch (...) { popup_message::g_complain("foo_simpleserver: unable to create main server thread! Component will not work."); } try { std::thread announcerThread(announceServerFunc); announcerThread.detach(); } catch (...) { popup_message::g_complain("foo_simpleserver: unable to create announcer thread! That part of the component will not work."); } }
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { Q_UNUSED( hPrevInstance ); Q_UNUSED( nShowCmd ); #if defined(_DEBUG) /* AllocConsole(); CONSOLE_SCREEN_BUFFER_INFO coninfo; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); coninfo.dwSize.Y = 500; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); int hConHandle; long lStdHandle; FILE *fp; lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stdout = *fp; setvbuf( stdout, NULL, _IONBF, 0 ); lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); fp = _fdopen( hConHandle, "w" ); *stderr = *fp; setvbuf( stderr, NULL, _IONBF, 0 ); QString consoleTitle = QString("%1 %2 %3 - Debug Console").arg(productString()).arg(productBeta()).arg(productVersion()); SetConsoleTitle(consoleTitle.latin1());*/ #endif INITCOMMONCONTROLSEX initex; initex.dwICC = ICC_WIN95_CLASSES; initex.dwSize = sizeof( INITCOMMONCONTROLSEX ); InitCommonControlsEx( &initex ); #pragma comment(lib, "comctl32.lib") // needed for InitCommonControlsEx call appInstance = hInstance; guiThread = GetCurrentThreadId(); // Try to load riched20.dll HMODULE hRiched = LoadLibrary( "riched20.dll" ); if ( !hRiched ) { MessageBox( 0, "The riched20.dll library could not be found on your system.\nPlease install Microsoft Internet Explorer 4.0 or later.", "Missing DLL", MB_OK | MB_ICONERROR ); return 1; } hbSeparator = CreateSolidBrush( RGB( 0xAF, 0xAF, 0xAF ) ); hbBackground = CreateSolidBrush( RGB( 0, 64, 38 ) ); iconGreen = ( HICON ) LoadImage( appInstance, MAKEINTRESOURCE( IDI_ICONGREEN ), IMAGE_ICON, 16, 16, 0 ); iconRed = ( HICON ) LoadImage( appInstance, MAKEINTRESOURCE( IDI_ICONRED ), IMAGE_ICON, 16, 16, 0 ); // Create the WindowClass WNDCLASSEX wpClass; ZeroMemory( &wpClass, sizeof( WNDCLASSEX ) ); wpClass.cbSize = sizeof( WNDCLASSEX ); wpClass.hInstance = hInstance; wpClass.lpfnWndProc = wpWindowProc; wpClass.hCursor = LoadCursor( NULL, MAKEINTRESOURCE( IDC_ARROW ) ); wpClass.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( IDI_ICON1 ) ); wpClass.hbrBackground = hbBackground; wpClass.lpszClassName = WOLFPACK_CLASS; wpClass.hIconSm = iconRed; wpClass.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW; if ( !RegisterClassEx( &wpClass ) ) { MessageBox( 0, "Couldn't register Window Class.", "Window Class", MB_OK | MB_ICONERROR ); return 1; } // Create the Window itself hmMainMenu = LoadMenu( appInstance, MAKEINTRESOURCE( IDR_MAINMENU ) ); mainWindow = CreateWindow( WOLFPACK_CLASS, "Wolfpack", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, hmMainMenu, hInstance, NULL ); if ( mainWindow == 0 ) { MessageBox( 0, QString( "Couldn't create the window: " + getErrorString() ).latin1(), "Wolfpack", MB_OK | MB_ICONERROR ); return 1; } ShowWindow( mainWindow, SW_NORMAL ); // Create the System Tray Icon ZeroMemory( &icondata, sizeof( icondata ) ); icondata.cbSize = sizeof( icondata ); icondata.hWnd = mainWindow; icondata.uID = 0; icondata.uFlags = NIF_MESSAGE | NIF_ICON; icondata.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( IDI_ICON1 ) ); icondata.uCallbackMessage = WM_TRAY_NOTIFY; #if !defined(TTS_BALLOON) # define TTS_BALLOON 0x40 #endif // This is "ported" from MFC tooltip = CreateWindowEx( WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP | TTS_BALLOON, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, mainWindow, NULL, hInstance, NULL ); if ( tooltip ) { TOOLINFO info; info.cbSize = sizeof( info ); info.uFlags = TTF_TRANSPARENT | TTF_CENTERTIP; info.hwnd = mainWindow; info.uId = 0; info.hinst = 0; info.lpszText = LPSTR_TEXTCALLBACK; GetClientRect( mainWindow, &info.rect ); SendMessage( tooltip, TTM_ADDTOOL, 0, ( LPARAM ) & info ); } Shell_NotifyIconA( NIM_ADD, ( PNOTIFYICONDATAA ) & icondata ); cServerThread serverThread( lpCmdLine ); serverThread.start(); MSG msg; while ( GetMessage( &msg, 0, 0, 0 ) > 0 ) { if ( msg.message == WM_CHAR && msg.hwnd == inputWindow && msg.wParam == '\r' ) { if ( Server::instance()->getState() == RUNNING ) { char command[512] = { 0, }; GetWindowText( inputWindow, command, 512 ); SetWindowText( inputWindow, "" ); // We are in a different Thread. Remember that. Console::instance()->queueCommand( command ); } continue; } else if ( msg.message == WM_TIMER ) { char message[512]; unsigned int seconds, minutes, hours, days; days = Server::instance()->time() / 86400000; hours = ( Server::instance()->time() % 86400000 ) / 3600000; minutes = ( ( Server::instance()->time() % 86400000 ) % 3600000 ) / 60000; seconds = ( ( ( Server::instance()->time() % 86400000 ) % 3600000 ) % 60000 ) / 1000; sprintf( message, "Uptime: %u:%02u:%02u:%02u", days, hours, minutes, seconds ); SetWindowText( lblUptime, message ); // Update the icon static unsigned int lastState = 0xFFFFFFFF; if ( lastState != Server::instance()->getState() ) { if ( Server::instance()->getState() == RUNNING ) { SendMessage( mainWindow, WM_SETICON, ICON_SMALL, ( WPARAM ) iconGreen ); SendMessage( statusIcon, STM_SETIMAGE, IMAGE_ICON, ( LPARAM ) iconGreen ); } else { SendMessage( mainWindow, WM_SETICON, ICON_SMALL, ( WPARAM ) iconRed ); SendMessage( statusIcon, STM_SETIMAGE, IMAGE_ICON, ( LPARAM ) iconRed ); } } lastState = Server::instance()->getState(); } TranslateMessage( &msg ); DispatchMessage( &msg ); } Shell_NotifyIconA( NIM_DELETE, ( PNOTIFYICONDATAA ) & icondata ); Server::instance()->cancel(); serverThread.wait(); return serverThread.returnValue(); }