Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
DataServer: Common/Socket.h Source File Main Page Modules Namespaces Classes Files Directories File List File Members Common Socket.hGo to the documentation of this file. 00001 /* 00002 @file Socket.h 00003 00004 Cross platform sockets. 00005 00006 $Id: Socket.h 6338 2009-11-29 22:08:13Z ds $ 00007 */ 00008 00009 #ifndef __SOCKET_H__ 00010 #define __SOCKET_H__ 00011 00012 #include 00013 #include 00014 00015 #ifdef _WIN32 00016 # include 00017 # include "windows.h" 00018 # include "winbase.h" 00019 # include "winnt.h" 00020 typedef int socklen_t; 00021 # ifdef WINDOWS_NT 00022 # ifndef MSG_WAITALL 00023 # define MSG_WAITALL 4 00024 # endif 00025 # endif 00026 #elif UNIX 00027 # include 00028 # include // for struct sockaddr_in 00029 # include // for TCP_NODELAY 00030 # include 00031 # include 00032 # include 00033 # include 00034 # include 00035 # include 00036 # include 00037 # include 00038 # ifdef PLATFORM_LINUX 00039 # define FIONBIO 0x5421 00040 # endif 00041 #else 00042 # error "Looks like the build scripts didn't set the platform type" 00043 #endif 00044 00051 class Socket 00052 { 00053 private: 00054 #ifdef WINDOWS_NT 00055 // Windows NT version of the MSG_WAITALL option 00056 int ReceiveMessageAll( void *pMessage, int nMessageSize ); 00057 #endif 00058 protected: 00059 int m_nPort; // Socket port number 00060 short int m_nSocketFd;// Socket file descriptor 00061 int m_nBlocking; // Blocking flag 00062 double m_dfLastRead; // Supprot for timing 00063 int m_nIsLocal; //< set if client is on same machine as server 00064 // for clients connected via accept 00065 std::string m_sClientHostname; 00066 struct sockaddr_in *m_pClientAddress; // Address of the client 00067 int m_nClientAddrStructLen; 00068 char *m_pDataBuffer; 00069 int m_iDataBufferSize, m_iMaxPacketSize; 00070 00071 public: 00072 Socket(); 00073 Socket( int nPort ); 00074 ~Socket(); 00075 00076 00077 static bool SocketsInit(); 00079 Socket *Accept(); 00080 00081 Socket *AcceptWithTimeout( double dTimeout ); 00082 00084 void Listen( int nNumPorts = 5 ); 00085 00087 void BindSocket(); 00088 00090 bool Connect( const std::string &sHostname ); 00091 00092 00093 bool HasData(); 00094 00104 void *ReceiveMessageWithHeader( 00105 int &iNumOfBytesRead, double dfTimeoutInSecs=0.0 ); 00111 int SendMessageWithHeader( void *pMessage, int iNumOfBytesToWrite ); 00116 int SendMessage( void *pMessage, int nMessageSize ); 00118 int ReceiveMessage( void *pMessage, int nMessageSize, int nOption = 0 ); 00119 00120 // allows a read with a timeout to prevent from blocking indefinitely 00121 int ReadMessageWithTimeout( 00122 void *pMessage, 00123 int nMessageSize, 00124 double dfTimeOut, 00125 bool &bPeerDown, 00126 int nOption=0 00127 ); 00128 00129 std::string ClientName( ); // check the client name 00130 void SetReceiveTimeOut( int nTimeOut ); 00131 void SetReadTime( double dfTime ); 00132 double GetReadTime(); 00133 //returns integer number of last socket error 00134 int GetLastError(); 00135 // The following member functions retrieve socket option settings 00136 // The following member functions sets socket options on and off 00137 void SetTcpNoDelay( int nToggle ); 00138 void SetDebug( int nToggle ); 00139 void SetBroadcast( int nToggle ); 00140 void SetReuseAddr( int nToggle ); 00141 void SetKeepAlive( int nToggle ); 00142 void SetLinger( struct linger lingerOption ); 00143 bool SetSocketBlocking( int nToggle ); 00144 // Sets the size of the send and receive buffer 00145 void SetSendBufSize( int nSendBufSize ); 00146 void SetReceiveBufSize( int nReceiveBufSize ); 00147 00148 int GetDebug(); 00149 int GetBroadcast(); 00150 int GetReuseAddr(); 00151 int GetKeepAlive(); 00152 void GetLinger( struct linger &_lingerOption ); 00153 int GetSendBufSize(); 00154 int GetReceiveBufSize(); 00155 int GetSocketBlocking(); 00156 00157 // Returns the socket file descriptor 00158 short int GetSocketFd(); 00159 }; 00160 #endif 00161 Generated on Mon Nov 30 09:54:57 2009 for DataServer by  1.6.1