00001
00002
00003
00004
00005
00006
00007 #include <iostream>
00008 #include <cstdlib>
00009 #include <string>
00010 #include <sys/socket.h>
00011 #include "ComPlusPlus/ComPlusPlus"
00012
00013 int main (int argc, char *argv [] ) {
00014
00015 if ( argc < 2 ) {
00016 std::cout << "Usage " << argv[0] << " [port] <reponsefile>\n";
00017 return 1;
00018 }
00019
00020 int port = std::atoi(argv [1]);
00021
00022 compp::SocketTcp *srv1 = NULL;
00023
00024 try {
00025
00026 compp::SocketTcp Server (port);
00027 Server.AttachFD();
00028 Server.SetSockOptReuseAddress ( true);
00029 Server.SetSockOptTcpNoDelay ( true ) ;
00030 Server.SetReadTimeout(10000000);
00031 Server.SetWriteTimeout(10000000);
00032 Server.SetLingerTime(0);
00033
00034
00035 srv1 = Server.Accept( );
00036
00037 srv1->AttachFD();
00038 srv1->SetSockOptReuseAddress ( true);
00039 srv1->SetSockOptTcpNoDelay ( true ) ;
00040 srv1->SetReadTimeout(10000000);
00041 srv1->SetWriteTimeout(10000000);
00042 srv1->SetLingerTime(0);
00043
00044 std::cout << "Connection established. Receiving Data ...\n";
00045
00046
00047 if ( argc == 2) {
00048
00049 do {
00050 int i;
00051 char buf[10000];
00052 std::memset((void*)buf, 0, sizeof(buf));
00053
00054 i = srv1->Read(buf, sizeof(buf)-1);
00055 if ( i > 0 )
00056 std::cout << buf;
00057
00058 } while ( ! srv1->IsEOF() );
00059
00060 std::cout << "Connection closed\n";
00061
00062 Server.Shutdown(0);
00063 delete srv1;
00064 } else {
00065
00066 int i;
00067 char buf[10000];
00068 std::memset((void*)buf, 0, sizeof(buf));
00069
00070 i = srv1->Read(buf, sizeof(buf)-1);
00071 if ( i > 0 )
00072 std::cout << buf;
00073 else {
00074 Server.Shutdown(0);
00075 delete srv1;
00076 exit(0);
00077 }
00078
00079 std::string Reply;
00080 if ( ! syspp::Tools::LoadTxtFile ( argv[2], Reply ) ) {
00081 std::cout << " Cannot load file " << argv[2] << "\n";
00082 Server.Shutdown(0);
00083 delete srv1;
00084 exit(1);
00085 }
00086 srv1->Writen ( Reply );
00087 srv1->Shutdown(0);
00088 Server.Shutdown(0);
00089 delete srv1;
00090 }
00091
00092 } catch ( syspp::ComException e ) {
00093 delete srv1;
00094 std::cout << "Exception " << e.what() << "\n" ;
00095 }
00096 return 0;
00097 }