CTCPSocket.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include "CTCPSocket.h"
00003 
00004 CTCPSocket::CTCPSocket():
00005   mIsInitialized(false){
00006 };
00007 
00009 CTCPSocket::CTCPSocket(const string& inPath,
00010            int   inPort):
00011   mIsInitialized(false){
00012   this->listenAtHostPort(inPath,
00013        inPort);
00014 };
00015 
00017 CTCPSocket::~CTCPSocket(){
00018   if(mIsInitialized){
00019     close(mSocketDescriptor);
00020   }
00021 };
00023 bool CTCPSocket::listenAtHostPort(const string& inHost,
00024           int inPort){
00025 
00026   if(inHost.size()){
00027     cerr << __FILE__ << ":" << __LINE__<< ":Warning:"
00028    << "Hostname currently unused" << endl;
00029   }
00030 
00031   mSocketAddress.sin_family = AF_INET ;
00032   /* get the port number */
00033   mSocketAddress.sin_port =  htons(inPort);
00034   mSocketAddress.sin_addr.s_addr = INADDR_ANY ;
00035 
00036   if((mSocketDescriptor = socket(PF_INET,
00037          SOCK_STREAM,
00038          IPPROTO_TCP))<0){
00039     cerr << "could not create socket" << endl;
00040     throw("socket");
00041   }
00042   
00043   int lOptionOn;
00044   if(0 > setsockopt(mSocketDescriptor,
00045         SOL_SOCKET,
00046         SO_REUSEADDR,
00047         (char*)&lOptionOn,
00048         sizeof(lOptionOn))){
00049     cerr << "could not set REUSEADDR: " 
00050    << strerror(errno)
00051    << flush 
00052    << endl;
00053     throw("setsockopt");
00054   };  
00055   /* assigns a name to the socket */
00056   if(bind(mSocketDescriptor,
00057     (struct sockaddr *) &mSocketAddress ,
00058     sizeof(mSocketAddress) ) == -1 ) {
00059     printf("bind error: %s\n",strerror(errno)) ;
00060     throw("bind");
00061   }
00062   /*listen to incoming connections */
00063   /* backlog = 5 */
00064   if(listen(mSocketDescriptor,
00065       5)==-1){ 
00066     
00067     printf("listen error: %s\n",strerror(errno)) ;
00068     throw("listen");
00069   }
00070 
00071   cerr << "TCP socket successfully initialized:" << mSocketDescriptor<< endl;
00072 
00073   return mIsInitialized=1;
00074 };
00076 CTCPSocket::operator bool()const{
00077   return mIsInitialized;
00078 }
00080 string CTCPSocket::getHost()const{
00081   return mHost;
00082 }
00084 int CTCPSocket::getPort()const{
00085   return mPort;
00086 }
00087 bool CTCPSocket::acceptAndServe(){
00088   struct sockaddr_in lAcceptedSocket;
00092   cerr << "Accept and serve: " << this->getSocketDescriptor() << endl;
00093 
00094   unsigned int lSize = sizeof(lAcceptedSocket);
00095   int s2(0);
00096   if(-1 == (s2 = accept(this->getSocketDescriptor(), (struct sockaddr *)&lAcceptedSocket, &lSize))) {
00097     perror("accept");
00098     return false;
00099   }
00100   cout << "connected: " << (this)->getSocketDescriptor() << "->" << s2 << endl;
00101 
00102   (this)->serveStream(s2);
00103   
00104   // close(s2); I cannot close sockets here, otherwise multithreading goes down the drain
00105   return true;
00106 }

Generated on Wed Jan 7 00:30:36 2009 for Gift by  doxygen 1.5.6