www.pudn.com > mputil.rar > socketio.h


#ifndef _SOCKETIO_ 
#define _SOCKETIO_ 
 
#ifdef WIN32 
#pragma warning(disable:4786) 
 
#include  
#include  
#else 
#include  
#include  
#include  
#include  
#include  
#include  
#endif 
#include  
#include  
 
#define MAXRECV 500 
#define MAXCONNECTIONS 5 
 
// ========================================================= 
	class MPSocketIO 
	{ 
	private: 
 
		int _isConnect; 
		int isConvert; 
#ifdef WIN32 
		SOCKET m_sock; 
#else 
		int m_sock; 
#endif 
	  sockaddr_in m_addr; 
 
		char m_hostName[40]; 
		int m_hostPort; 
 
		union INT_DATA { 
			int data; 
			char byte[4]; 
		}int_data; 
		union FLOAT_DATA { 
			float data; 
			char byte[4]; 
		}float_data; 
		union LONG_DATA{ 
			long data; 
			char byte[4]; 
		}long_data; 
		union DOUBLE_DATE{ 
			double data; 
			char byte[4]; 
		}double_data; 
		void	writeConvert(char byte[],int numWrite); 
		int		readConvert(char byte[],int numRead); 
#ifndef WIN32 
		inline void closesocket(int sock) { ::close(sock); }; 
#endif 
 
	public: 
		MPSocketIO(); 
		MPSocketIO(char _hostName[],int _hostPort,int _isConvert=false); 
		virtual ~MPSocketIO(); 
		int		connectServer(); 
		int  connect(void); 
		int	  isConnect(); 
		void	disconnectServer(); 
		void  disconnect(); 
		void	writeByte(const char &b); 
		void	write(const int &i); 
		void	write(const long &l); 
		void	write(const char &ch); 
		void	write(const char *s); 
		void	write(const float &f); 
		void	write(const double &d); 
		char	readByte(); 
		int		readInt(); 
		long	readLong(); 
		float	readFloat(); 
		double	readDouble(); 
		char	readChar(); 
		int readString(std::string &s); 
		int		readFull(char bur[],int numRead); 
		const MPSocketIO& operator >> (std::string& s) const; 
		const MPSocketIO& operator << (const std::string& s) const; 
		bool  listen() const; 
		int  bind (); 
		bool  accept(MPSocketIO& new_socket) const; 
	}; 
// ================================================================ 
 
#endif //_SOCKETIO_