00001
00002
00003
00004
00005
00006
00007 #include "SysPlusPlus/SysPlusPlus"
00008 #include "ComPlusPlus/ComPlusPlus"
00009
00010
00011 #include <string>
00012 #include <strings.h>
00013
00014 int main(int argc, char *argv [] ) {
00015
00016 const int TestPort = 31234;
00017 const int LOOPS = 100;
00018
00019 try {
00020
00021 compp::SocketTcp server(TestPort);
00022 compp::SocketTcp * srv;
00023
00024 compp::SocketTcp client;
00025 client.Connect ( "127.0.0.1", TestPort );
00026
00027 srv = server.Accept();
00028
00029 std::cout << "Client: ";
00030 std::cout << client.GetSockIPAddr()<< ":" << client.GetSockPort()
00031 << " connected to "
00032 << client.GetPeerSockIPAddr()<< ":" << client.GetPeerSockPort()
00033 << "\n";
00034
00035 std::cout << "Server(Worker): GetSockIPAddr() GetSockPort() GetPeerSockIPAddr() GetPeerSockPort()";
00036 std::cout << srv->GetSockIPAddr()<< ":" << srv->GetSockPort()
00037 << " connected to "
00038 << srv->GetPeerSockIPAddr()<< ":" << srv->GetPeerSockPort()
00039 << "\n";
00040
00041
00042 std::cout << "Server Address" << srv->GetSocketAddress () << ":" << srv->GetSocketPort() << "\n";
00043
00044 char c1 [10];
00045 char c2 [10];
00046
00047 strcpy ( c1, "Hello") ;
00048 memset ((void*) c2, 0, sizeof ( c2 ) );
00049
00050
00051 client.Write ( c1, strlen (c1) );
00052
00053
00054 if ( !srv->PollRcv( 1000 ) ) {
00055 std::cout << " 1.) Polling is NOT OK\n";;
00056 } else {
00057 std::cout << " 1.) Polling is OK\n";
00058 }
00059 srv->Read ( c2, sizeof ( c2 ) );
00060
00061 std::cout << " 2.) (Should say "<<c1 <<" ) Says: " << c2 << "\n";
00062
00063 std::string str1 = "" ;
00064 std::string str2 = "" ;
00065
00066 for ( int i = 0; i < LOOPS; ++ i )
00067 str1 += "Hello World";
00068
00069 char c[ 2000 ];
00070
00071 int sent = 0;
00072 int rcvd = 0;
00073 std::cout << " 3.) Mass volume test (every dot means " << str1.length() * LOOPS << "bytes sent and received correctly)\n";
00074 for ( int j = 0; j < LOOPS; ++ j ){
00075
00076 sent += client.Writen ( str1.c_str(), str1.length() );
00077 rcvd += srv->Readn ( c, str1.length() );
00078
00079 std::cout << "." ;
00080 std::cout.flush();
00081 }
00082
00083 std::cout << "\n" ;
00084 std::cout << " Sent " << sent << " Received " << rcvd << "\n";
00085 client.Shutdown ( 2 );
00086 srv->Shutdown ( 2 );
00087 delete srv;
00088 } catch ( syspp::ComException e ) {
00089 perror ( "Accept");
00090 std::cout << "Exception" << e.what() << "\n";
00091 } catch ( ... ) {
00092 std::cout << "Exception\n";
00093 }
00094
00095 try {
00096
00097 compp::SocketTcp srv(TestPort+1) ;
00098 compp::SocketTcp *s1;
00099 compp::SocketTcp clnt;
00100 compp::Poll Multiplexer;
00101
00102 Multiplexer.AddReadQueue(&srv);
00103
00104 std::cout << " 4.) Multiplexer 1 (0 excpected ) got " << Multiplexer.Multiplexer(2000) << "\n";
00105 clnt.Connect("127.0.0.1", TestPort+1 );
00106 std::cout << " 4.) Multiplexer 2 (1 excpected ) got " << Multiplexer.Multiplexer(2000) << "\n";
00107 s1 = srv.Accept();
00108 Multiplexer.AddReadQueue(s1);
00109 std::cout << " 4.) Multiplexer 3 (0 excpected ) got " << Multiplexer.Multiplexer(2000) << "\n";
00110 s1->Write ( "Hello\n" );
00111 Multiplexer.AddReadQueue(&clnt);
00112 std::cout << " 4.) Multiplexer 4 (1 excpected ) got " << Multiplexer.Multiplexer(2000) << "\n";
00113 clnt.Write ( "World\n" );
00114 std::cout << " 4.) Multiplexer 5 (2 excpected ) got " << Multiplexer.Multiplexer(2000) << "\n";
00115 Multiplexer.EmptyReadQueue();
00116 std::cout << " 4.) Multiplexer 6 (0 excpected ) got " << Multiplexer.Multiplexer(2000) << "\n";
00117
00118
00119 std::cout << "Listener (should be open): " << (srv.IsEOF() ? "closed\n" : "open\n") ;
00120 std::cout << "Worker (should be open): " << (s1->IsEOF() ? "closed\n" : "open\n") ;
00121 std::cout << "Shutting down worker...\n";
00122
00123 std::cout << "Closing Client ... Server should still be open\n";
00124 clnt.Shutdown();
00125 std::cout << "Listener(should be open): " << (srv.IsEOF() ? "closed\n" : "open\n") ;
00126 std::cout << "Worker(should be open): " << (s1->IsEOF() ? "closed\n" : "open\n") ;
00127
00128 char c[9000];
00129 memset ( (void*) c, 0, sizeof ( c ) ) ;
00130 std::cout << "Read from closed socket should deliver the string World :" << s1->Read(c, 100) << " " << c << "\n";
00131 std::cout << "Listener(should still be open): " << (srv.IsEOF() ? "closed\n" : "open\n") ;
00132 std::cout << "Worker(should be open): " << (s1->IsEOF() ? "closed\n" : "open\n") ;
00133
00134 strcpy ( c, "" );
00135 std::cout << "Read from closed socket " << s1->Read(c, 100) << ":" << c << "\n";
00136 std::cout << "Listener (must be open): " << (srv.IsEOF() ? "closed\n" : "open\n") ;
00137 std::cout << "Worker (must be closed now): " << (s1->IsEOF() ? "closed\n" : "open\n") ;
00138 delete s1;
00139
00140 compp::SocketTcp srv2(TestPort+2) ;
00141 compp::SocketTcp *s2;
00142 compp::SocketTcp clnt2;
00143 clnt2.SetReadAhead( true );
00144
00145 clnt2.Connect("127.0.0.1", TestPort+2 );
00146 std::cout << " 5.) Read Ahead Cache Test: connect\n";
00147 s2 = srv2.Accept();
00148
00149 std::cout << " 5.) Read Ahead Cache Test: accepted\n";
00150 s2->Write ( "111111\n" );
00151 clnt2.Read ( c, 1 );
00152 c[1] = 0;
00153 std::cout << " 5.) Read Ahead Cache Test: Read () 1 expected got " << c << "\n";
00154 clnt2.Shutdown ( );
00155
00156 clnt2.Connect("127.0.0.1", TestPort+2 );
00157 s2 = srv2.Accept();
00158 std::cout << " 5.) Read Ahead Cache Test: accepted\n";
00159
00160 s2->Write ( "2222222\n" );
00161 clnt2.Read ( c, 1 );
00162 c[1] = 0;
00163 std::cout << " 5.) Read Ahead Cache Test: Read () 2 expected got " << c << "\n";
00164 clnt2.Shutdown ( );
00165
00166 } catch ( syspp::ComException e ) {
00167 perror ( "SocketTest");
00168 std::cout << "Exception" << e.what() << "\n";
00169 }
00170
00171 return 0;
00172 }