git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6760
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
{
puts("*** Testing wxSocketServer ***\n");
{
puts("*** Testing wxSocketServer ***\n");
- // we want to launch a server
+ static const int PORT = 3000;
+
wxSocketServer *server = new wxSocketServer(addr);
if ( !server->Ok() )
{
puts("ERROR: failed to bind");
wxSocketServer *server = new wxSocketServer(addr);
if ( !server->Ok() )
{
puts("ERROR: failed to bind");
- puts("Server: waiting for connection...");
+ printf("Server: waiting for connection on port %d...\n", PORT);
wxSocketBase *socket = server->Accept();
if ( !socket )
wxSocketBase *socket = server->Accept();
if ( !socket )
puts("Server: got a client.");
puts("Server: got a client.");
- wxString s;
- char ch = '\0';
- for ( ;; )
+ server->SetTimeout(60); // 1 min
+
+ while ( socket->IsConnected() )
- if ( socket->Read(&ch, sizeof(ch)).Error() )
+ wxString s;
+ char ch = '\0';
+ for ( ;; )
- puts("ERROR: in wxSocket::Read.");
+ if ( socket->Read(&ch, sizeof(ch)).Error() )
+ {
+ // don't log error if the client just close the connection
+ if ( socket->IsConnected() )
+ {
+ puts("ERROR: in wxSocket::Read.");
+ }
- if ( ch == '\r' )
- continue;
+ if ( ch == '\r' )
+ continue;
- if ( ch == '\n' )
- break;
+ if ( ch == '\n' )
+ break;
- if ( ch != '\n' )
- {
- break;
- }
+ if ( ch != '\n' )
+ {
+ break;
+ }
- printf("Server: got '%s'.\n", s.c_str());
- if ( s == _T("bye") )
- {
- delete socket;
+ printf("Server: got '%s'.\n", s.c_str());
+ if ( s == _T("bye") )
+ {
+ delete socket;
+ break;
+ }
+
+ socket->Write(s.MakeUpper().c_str(), s.length());
+ socket->Write("\r\n", 2);
+ printf("Server: wrote '%s'.\n", s.c_str());
- socket->Write(s.MakeUpper().c_str(), s.length());
- socket->Write("\r\n", 2);
- printf("Server: wrote '%s'.\n", s.c_str());
+ puts("Server: lost a client.");
+ // same as "delete server" but is consistent with GUI programs
+ server->Destroy();
}
static void TestSocketClient()
}
static void TestSocketClient()
#endif // TEST_MIME
#ifdef TEST_SOCKETS
#endif // TEST_MIME
#ifdef TEST_SOCKETS
- if ( 0 )
- TestSocketServer();
+ TestSocketServer();
+ if ( 0 )
{
TestSocketClient();
TestProtocolFtp();
{
TestSocketClient();
TestProtocolFtp();