C++ World

C++ Resources, Tips,
Documentation, Examples,
HOWTO's, FAQ's, Tools
Libraries, Frameworks, IDE's

 
© 2005,2009 C++ World - All rights reserved - webdesign.arttina
C++, Sockets, SysProg, Libraries, Examples, Tools, Compilers, IDE, STL, XML
Google
FAQ Sockets Socket Library Software Engineering Sysprog Examples Books
 
Home
C++
The C Language
STL
BOOST
GNU
GNU C Compiler
GNU Debugger
GNU Profiler
HP C++ Compiler
DDD
Eclipse
Curses
XML
X11
KDevelop
MS Visual Studio
Tutorials
// **************************************************************
// Com ++ Framework by the C++ World
// Please report defects: mailto:compp@cplusplusworld.com
// Please visit: http://www.cplusplusworld.com/socketlibrary.html
// **************************************************************

#include <iostream>
#include <cstdlib>
#include <string>
#include <sys/socket.h>
#include "ComPlusPlus/ComPlusPlus"

int main (int argc, char *argv [] ) {
	
if ( argc < 2 ) {
	std::cout << "Usage " << argv[0] << " [port] <reponsefile>\n";
  	return 1;
}

int port = std::atoi(argv [1]);

 compp::SocketTcp *srv1 = NULL;
 
try {
 // -> Comment 1
 compp::SocketTcp Server (port);	
 Server.AttachFD();
 Server.SetSockOptReuseAddress ( true);
 Server.SetSockOptTcpNoDelay ( true ) ;
 Server.SetReadTimeout(10000000);
 Server.SetWriteTimeout(10000000);
 Server.SetLingerTime(0);

 // -> Comment 2
 srv1 = Server.Accept( ); 

 srv1->AttachFD();
 srv1->SetSockOptReuseAddress ( true);
 srv1->SetSockOptTcpNoDelay ( true ) ;
 srv1->SetReadTimeout(10000000);
 srv1->SetWriteTimeout(10000000);
 srv1->SetLingerTime(0);

 std::cout << "Connection established. Receiving Data ...\n";
 

 if ( argc == 2) {

   do {
     int i;
     char buf[10000];
     std::memset((void*)buf, 0, sizeof(buf));
     // -> Comment 3
     i = srv1->Read(buf, sizeof(buf)-1);
     if ( i > 0 )
       std::cout << buf;	
     // -> Comment 4
   } while ( ! srv1->IsEOF() );
   
   std::cout << "Connection closed\n";
   // -> Comment 5
   Server.Shutdown(0);
   delete srv1; 
 } else {

     int i;
     char buf[10000];
     std::memset((void*)buf, 0, sizeof(buf));
     // -> Comment 3
     i = srv1->Read(buf, sizeof(buf)-1);
     if ( i > 0 )
       std::cout << buf;	
     else {
       Server.Shutdown(0);
       delete srv1;
       exit(0);
     }

     std::string Reply; 
     if ( ! syspp::Tools::LoadTxtFile ( argv[2], Reply ) ) {
       std::cout << " Cannot load file " << argv[2] << "\n";
       Server.Shutdown(0);
       delete srv1;
       exit(1);
     }
     srv1->Writen ( Reply );
     srv1->Shutdown(0);
     Server.Shutdown(0);
     delete srv1;
 }

} catch ( syspp::ComException e ) {
  delete srv1;
  std::cout << "Exception " << e.what() << "\n" ;
}
 return 0;	
}


syntax highlighted by Code2HTML, v. 0.9.1


Franz Brandel Contact Legal Statement Sitemap C++ World