Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #ifndef __CXUTILS_SOCKET_H
00042 #define __CXUTILS_SOCKET_H
00043
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string>
00047 #include <vector>
00048
00049 #ifdef WIN32
00050 #include <winsock.h>
00051 #else
00052 #include <errno.h>
00053 #include <sys/ioctl.h>
00054 #include <sys/types.h>
00055 #include <unistd.h>
00056 #include <netinet/in.h>
00057 #include <sys/socket.h>
00058 #include <netdb.h>
00059 #include <arpa/inet.h>
00060 #endif
00061
00062 #include "cxutils/cxbase.h"
00063 #include "cxutils/packet.h"
00064 #include "cxutils/networking/ip4address.h"
00065 #include "cxutils/networking/ip6address.h"
00066
00067 namespace CxUtils
00068 {
00076 class CX_UTILS_DLL Socket
00077 {
00078 public:
00079 enum Type
00080 {
00081 Undefined = 0,
00082 TCPListen,
00083 TCPServer,
00084 TCPClient,
00085 UDPServer,
00086 UDPClient
00087 };
00088
00089 Socket();
00090 Socket(const Socket &arg);
00091 virtual ~Socket();
00092 virtual void Shutdown();
00093
00094 virtual int Send(const Packet &packet) const;
00095 virtual int Send(const char* buffer,
00096 const unsigned int length) const;
00097 int Recv(Packet &packet,
00098 const unsigned int receiveSize,
00099 const long int timeOutMilliseconds,
00100 IPAddress* ipAddress = NULL,
00101 unsigned short* port = NULL) const;
00102 int Recv(char* buffer, const unsigned int length,
00103 const long int timeOutMilliseconds,
00104 IPAddress* ipAddress = NULL,
00105 unsigned short* port = NULL) const;
00106 bool IsValid() const;
00107 bool SetNetworkInterface(const int num);
00108 bool SetNetworkInterface(const IP4Address& ipAddress);
00109 int GetSocket() const;
00110 Type GetType() const;
00111 int GetNetworkInterface() const;
00112
00113 static unsigned int GetHostAddresses(IP4Address::List& ipAddresses);
00114 static bool IsHostAddress(const IP4Address& ipAddress);
00115 static int GetNetworkInterface(const IP4Address& ipAddress);
00116 static unsigned int GetNumNetworkConnections();
00117 static int IsIncommingData(const Socket* sock, const long int timeOutMilliseconds = 0);
00118
00119 Socket &operator=(const Socket &arg);
00120
00121 protected:
00122 virtual int SendFromBuffer(const char* buffer,
00123 const unsigned int length) const = 0;
00124 virtual int RecvToBuffer(char* buffer,
00125 const unsigned int length,
00126 const long int timeOutMilliseconds = 0,
00127 IPAddress* ipAddress = NULL,
00128 unsigned short* port = NULL) const = 0;
00129 static void InitializeSockets();
00130 #ifdef WIN32
00131 SOCKET mSocket;
00132 static unsigned int mWinsockInitCount;
00133 #else
00134 int mSocket;
00135 #endif
00136 Type mSocketType;
00137 int mServiceLength;
00138 int mNetworkInterface;
00139 bool mGoodSocket;
00140 sockaddr_in mService;
00141 };
00142 }
00143
00144 #endif
00145
00146