better TestSocketServer()
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6760 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -723,19 +723,22 @@ static void TestSocketServer()
|
|||||||
{
|
{
|
||||||
puts("*** Testing wxSocketServer ***\n");
|
puts("*** Testing wxSocketServer ***\n");
|
||||||
|
|
||||||
// we want to launch a server
|
static const int PORT = 3000;
|
||||||
|
|
||||||
wxIPV4address addr;
|
wxIPV4address addr;
|
||||||
addr.Service(3000);
|
addr.Service(PORT);
|
||||||
|
|
||||||
wxSocketServer *server = new wxSocketServer(addr);
|
wxSocketServer *server = new wxSocketServer(addr);
|
||||||
if ( !server->Ok() )
|
if ( !server->Ok() )
|
||||||
{
|
{
|
||||||
puts("ERROR: failed to bind");
|
puts("ERROR: failed to bind");
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( ;; )
|
for ( ;; )
|
||||||
{
|
{
|
||||||
puts("Server: waiting for connection...");
|
printf("Server: waiting for connection on port %d...\n", PORT);
|
||||||
|
|
||||||
wxSocketBase *socket = server->Accept();
|
wxSocketBase *socket = server->Accept();
|
||||||
if ( !socket )
|
if ( !socket )
|
||||||
@@ -746,47 +749,59 @@ static void TestSocketServer()
|
|||||||
|
|
||||||
puts("Server: got a client.");
|
puts("Server: got a client.");
|
||||||
|
|
||||||
wxString s;
|
server->SetTimeout(60); // 1 min
|
||||||
char ch = '\0';
|
|
||||||
for ( ;; )
|
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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ch == '\r' )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if ( ch == '\n' )
|
||||||
|
break;
|
||||||
|
|
||||||
|
s += ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ch != '\n' )
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Server: got '%s'.\n", s.c_str());
|
||||||
|
if ( s == _T("bye") )
|
||||||
|
{
|
||||||
|
delete socket;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ch == '\r' )
|
socket->Write(s.MakeUpper().c_str(), s.length());
|
||||||
continue;
|
socket->Write("\r\n", 2);
|
||||||
|
printf("Server: wrote '%s'.\n", s.c_str());
|
||||||
if ( ch == '\n' )
|
|
||||||
break;
|
|
||||||
|
|
||||||
s += ch;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ch != '\n' )
|
puts("Server: lost a client.");
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Server: got '%s'.\n", s.c_str());
|
socket->Destroy();
|
||||||
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());
|
|
||||||
|
|
||||||
delete socket;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete server;
|
// same as "delete server" but is consistent with GUI programs
|
||||||
|
server->Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TestSocketClient()
|
static void TestSocketClient()
|
||||||
@@ -2608,9 +2623,9 @@ int main(int argc, char **argv)
|
|||||||
#endif // TEST_MIME
|
#endif // TEST_MIME
|
||||||
|
|
||||||
#ifdef TEST_SOCKETS
|
#ifdef TEST_SOCKETS
|
||||||
if ( 0 )
|
|
||||||
TestSocketServer();
|
|
||||||
if ( 1 )
|
if ( 1 )
|
||||||
|
TestSocketServer();
|
||||||
|
if ( 0 )
|
||||||
{
|
{
|
||||||
TestSocketClient();
|
TestSocketClient();
|
||||||
TestProtocolFtp();
|
TestProtocolFtp();
|
||||||
|
Reference in New Issue
Block a user