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

dsSocket.h

Go to the documentation of this file.
00001 /* 
00002  * $Id: dsSocket.h,v 1.25 2004/07/06 13:05:04 dsamersoff Exp $
00003  */
00004 #ifndef dsSocket_h
00005 #define dsSocket_h 
00006 
00007 #ifdef HAVE_CONFIG_H
00008 # include <config.h>
00009 #endif
00010 
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h> 
00014 #include <fcntl.h>
00015 #include <errno.h>
00016 
00017 #ifdef HAVE_UNISTD_H
00018 #include <unistd.h>
00019 #endif
00020 
00021 #ifdef HAVE_SYS_TIME_H
00022 # include <sys/time.h>
00023 #endif
00024 
00025 #ifdef HAVE_IO_H
00026 # include <io.h>
00027 #endif
00028 
00029 
00030 #ifdef POSIX_OS
00031 # include <sys/socket.h>
00032 # include <sys/types.h>
00033 # include <netinet/in.h>
00034 # include <arpa/inet.h>
00035 # include <netdb.h>
00036 #endif
00037 
00038 #ifdef Win32
00039 # include <winsock.h>
00040 #endif
00041 
00042 #include <dsSmartException.h>
00043 #include <dsform.h>
00044 
00045 #define NO_FLAGS 0                   // No special flags specified
00046 
00047 #ifdef Win32
00048  typedef int socklen_t;
00049 #endif
00050 
00051 DECLARE_EXCEPTION(dsSocket);
00052 
00057 class dsSocketBase
00058 {
00059 protected:
00060     int           _sock;        // socket
00061 
00062     virtual size_t __read(char *buf,   size_t bytes) = 0;
00063     virtual size_t __write(const char *ptr,  size_t bytes) = 0;
00064 
00065 public:
00066   
00067   dsSocketBase();
00068 
00069   virtual void Connect(const char *host, const int port, const int proto) = 0;
00070   virtual void Close() = 0;
00071 
00072   int fd() const { return _sock; };
00073 
00074 
00083   size_t Read(char *buffer, size_t bytes, int sec = 10, int usec = 0 );
00084   size_t Read(std::ostream& os, int sec = 10, int usec = 0);
00085 
00086   size_t Write(const char *buffer, size_t bytes, int sec = 10, int usec = 0 );
00087   size_t Write(std::istream& is, int sec = 10, int usec = 0);
00088 
00089   size_t Write(const char *str, int sec = 10, int usec = 0 )
00090   {
00091         return  Write(str,strlen(str), sec, usec);
00092   }
00093 
00094   size_t Printf(const char *format, ...);
00095 
00096          
00097   
00110   size_t ReadLine(std::ostream& os, int sec = 10);
00111   size_t ReadLine(char *buf, size_t size, int sec = 10);
00112 
00113   size_t WriteLine(std::istream& is, int sec = 10);
00114   size_t WriteLine(const char *str,  int sec = 10);
00115 };
00116 
00117 
00128 class dsSocket : public dsSocketBase
00129 {
00130 protected:
00131     struct sockaddr_in      _sockAddr; 
00132     struct hostent          *_pHostEnt;
00133 
00134 #ifdef Win32
00135      WSADATA _ws;
00136 #endif
00137 
00138 public:
00139   
00140   dsSocket(const char *host, const int port, const int proto = IPPROTO_TCP);
00141   dsSocket();
00142   virtual ~dsSocket();
00143 
00144   void Open(const int proto = IPPROTO_UDP, const int fcntl_flags = 0); // O_NONBLOCK
00145 
00146   virtual void Connect(const char *host, const int port, const int proto = IPPROTO_TCP);
00147   virtual void Close();
00148 
00149   virtual size_t __read(char *buf,   size_t bytes);
00150   virtual size_t __write(const char *ptr,  size_t bytes);
00151   virtual size_t __sendto(const char *buf, size_t bytes, const sockaddr *to, socklen_t len);
00152 
00153   size_t Sendto(const char *ptr, size_t bytes, const char *host, const int port);
00154 
00155   unsigned long getip();
00156   unsigned long getpeerip();
00157   unsigned int  getport();
00158 
00159   char *getpeer_aip();
00160   char *get_aip();
00161 };
00162 
00163 class dsServerSocket : public dsSocket
00164 {
00165   int                     _srv_sock;    // for listen
00166   struct sockaddr_in      _srv_sockAddr;
00167 
00168 public:
00169 
00170 /* listen and accept for fork based server */
00171   void Listen(int port);
00172 #ifdef POSIX_OS
00173   pid_t FAccept();
00174 #endif
00175   int   Accept();
00176 
00177   int  srvsocket(){ return _srv_sock; };
00178   virtual void Close();
00179 };
00180 
00181 #endif

Generated on Mon May 16 18:26:58 2005 for libdms4 by doxygen1.3-rc2