00001
00002
00003
00004 #include <winsock.h>
00005
00006 #ifndef _BLOCKSOCK_H_
00007 #define _BLOCKSOCK_H_
00008
00009 typedef const struct sockaddr* LPCSOCKADDR;
00010
00011 class CSockAddr : public sockaddr_in
00012 {
00013 public:
00014
00015 CSockAddr()
00016 {
00017 sin_family = AF_INET;
00018 sin_port = 0;
00019 sin_addr.s_addr = 0;
00020 }
00021
00022 CSockAddr(const SOCKADDR& sa)
00023 {
00024 memcpy(this, &sa, sizeof(SOCKADDR));
00025 }
00026
00027 CSockAddr(const SOCKADDR_IN& sin)
00028 {
00029 memcpy(this, &sin, sizeof(SOCKADDR_IN));
00030 }
00031
00032 CSockAddr(const ULONG ulAddr, const USHORT ushPort = 0)
00033 {
00034 sin_family = AF_INET;
00035 sin_port = htons(ushPort);
00036 sin_addr.s_addr = htonl(ulAddr);
00037 }
00038
00039 CSockAddr(const char* pchIP, const USHORT ushPort = 0)
00040 {
00041 sin_family = AF_INET;
00042 sin_port = htons(ushPort);
00043 sin_addr.s_addr = inet_addr(pchIP);
00044 }
00045
00046
00047 char* DottedDecimal()
00048 {
00049 return inet_ntoa(sin_addr);
00050 }
00051
00052
00053 USHORT Port() const
00054 {
00055 return ntohs(sin_port);
00056 }
00057
00058 ULONG IPAddr() const
00059 {
00060 return ntohl(sin_addr.s_addr);
00061 }
00062
00063 const CSockAddr& operator=(const SOCKADDR& sa)
00064 {
00065 memcpy(this, &sa, sizeof(SOCKADDR));
00066 return *this;
00067 }
00068
00069 const CSockAddr& operator=(const SOCKADDR_IN& sin)
00070 {
00071 memcpy(this, &sin, sizeof(SOCKADDR_IN));
00072 return *this;
00073 }
00074
00075 operator SOCKADDR()
00076 {
00077 return *((LPSOCKADDR) this);
00078 }
00079
00080 operator LPSOCKADDR()
00081 {
00082 return (LPSOCKADDR) this;
00083 }
00084
00085 operator LPSOCKADDR_IN()
00086 {
00087 return (LPSOCKADDR_IN) this;
00088 }
00089 };
00090
00091
00092
00093 class CBlockingSocket
00094 {
00095 public:
00096 enum {DEFAULT_TIMEOUT=30};
00097 SOCKET m_hSocket;
00098 CBlockingSocket()
00099 {
00100 m_hSocket = NULL;
00101 }
00102
00103 void Cleanup();
00104 void Create(int nType = SOCK_STREAM);
00105 void Close();
00106 void Bind(LPCSOCKADDR psa);
00107 void Listen();
00108 void Connect(LPCSOCKADDR psa);
00109 bool Accept(CBlockingSocket& s, LPSOCKADDR psa);
00110 int Send(const char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00111 int Write(const char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00112 int Receive(char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00113 int SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa, const int nSecs=10);
00114 int ReceiveDatagram(char* pch, const int nSize, LPSOCKADDR psa, const int nSecs=10);
00115 void GetPeerAddr(LPSOCKADDR psa);
00116 void GetSockAddr(LPSOCKADDR psa);
00117 static CSockAddr GetHostByName(const char* pchName, const USHORT ushPort = 0);
00118 static const char* GetHostByAddr(LPCSOCKADDR psa);
00119 operator SOCKET()
00120 {
00121 return m_hSocket;
00122 }
00123 };
00124
00125 class CTelnetSocket : public CBlockingSocket
00126 {
00127 public:
00128 enum {nSizeRecv = 1024};
00129 enum {FLAG_VALIDATED=1,FLAG_PLAYING=2};
00130 CTelnetSocket();
00131 ~CTelnetSocket();
00132 int ReadLine(char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00133 int ReadResponse(char* pch, const int nSize, const int nSecs=DEFAULT_TIMEOUT);
00134 int Print(const char* msg);
00135 bool SetLoginName(const char* name);
00136
00137 public:
00138
00139 WORD m_wFlags;
00140 char* m_pLoginName;
00141 private:
00142 char* m_pReadBuf;
00143 int m_nReadBuf;
00144
00145 };
00146
00147
00148 #endif