Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

socket.h

00001 /*
00002  * General sockets class wrapper
00003  *
00004  * Copyright (C) 2001 Barnaby Gray <barnaby@beedesign.co.uk>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef SOCKET_H
00023 #define SOCKET_H
00024 
00025 #include <string>
00026 #include <exception>
00027 
00028 #include <errno.h>
00029 #include <netdb.h>
00030 #include <sys/types.h>
00031 #include <sys/socket.h>
00032 #include <netinet/in.h>
00033 #include <arpa/inet.h>
00034 #include <unistd.h>
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <fcntl.h>
00038 
00039 unsigned int StringtoIP(const std::string& ip);
00040 
00041 std::string IPtoString(unsigned int ip);
00042 
00043 class Buffer;
00044 
00045 class TCPSocket {
00046  public:
00047   enum State {
00048     NOT_CONNECTED,
00049     NONBLOCKING_CONNECT,
00050     CONNECTED
00051   };
00052   
00053  private:
00054   static const unsigned int max_receive_size = 4096;
00055   
00056   unsigned long gethostname(const char *hostname);
00057 
00058   int m_socketDescriptor;
00059   bool m_socketDescriptor_valid;
00060   struct sockaddr_in remoteAddr, localAddr;
00061   bool blocking;
00062   State m_state;
00063 
00064   void fcntlSetup();
00065 
00066  public:
00067   TCPSocket();
00068   ~TCPSocket();
00069 
00070   // used after a successful accept on TCPServer
00071   TCPSocket( int fd, struct sockaddr_in addr );
00072 
00073   void Connect();
00074   void FinishNonBlockingConnect();
00075   void Disconnect();
00076   
00077   int getSocketHandle();
00078 
00079   void Send(Buffer& b);
00080 
00081   bool Recv(Buffer& b);
00082 
00083   bool connected();
00084 
00085   void setRemoteHost(const char *host);
00086   void setRemotePort(unsigned short port);
00087   void setRemoteIP(unsigned int ip);
00088   
00089   void setBlocking(bool b);
00090   bool isBlocking() const;
00091 
00092   State getState() const;
00093 
00094   unsigned int getRemoteIP() const;
00095   unsigned short getRemotePort() const;
00096 
00097   unsigned int getLocalIP() const;
00098   unsigned short getLocalPort() const;
00099 
00100 };
00101 
00102 class TCPServer {
00103  private:
00104   int m_socketDescriptor;
00105   bool m_socketDescriptor_valid;
00106   struct sockaddr_in localAddr;
00107 
00108  public:
00109   TCPServer();
00110   ~TCPServer();
00111 
00112   int getSocketHandle();
00113   
00114   void StartServer();
00115   void StartServer(unsigned short lower, unsigned short upper);
00116   void Disconnect();
00117 
00118   bool isStarted() const;
00119 
00120   // blocking accept
00121   TCPSocket* Accept();
00122   
00123   unsigned short getPort() const;
00124   unsigned int getIP() const;
00125 
00126 };
00127 
00128 class SocketException : std::exception {
00129  private:
00130   std::string m_errortext;
00131 
00132  public:
00133   SocketException(const std::string& text);
00134   ~SocketException() throw() { }
00135 
00136   const char* what() const throw();
00137 };
00138 
00139 #endif

Generated on Sun Jul 21 10:57:32 2002 for libicq2000 by doxygen1.2.16