⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysocket.h

📁 学习java的经典教程源代码
💻 H
字号:
#ifndef __BRUCEBAI_SOCK_H__
#define __BRUCEBAI_SOCK_H__
/*

*/
#ifdef  _WIN32
#include <Winsock2.h>
#include <ws2tcpip.h>
//#include <wsipx.h>// IPX sockets
#pragma comment(lib, "Ws2_32.lib")
typedef  int SOCKLEN;

#else   //POSIX
#include <netdb.h>
//#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
typedef unsigned int SOCKLEN;
typedef unsigned int SOCKET ;
const   unsigned int INVALID_SOCKET=0;
const int SOCKET_ERROR=-1;
//typedef   unsigned long BOOL;
//#define   TRUE         1;
//#define   FALSE        0;

#endif

#include <string>

#ifdef  _WIN32
class CSocketInit
{
public:
	static CSocketInit& Instance();
	~CSocketInit();
protected:
    CSocketInit();

};
#endif	
class CMySocket 
{
	
private:
	//copy constructor
	CMySocket(const CMySocket& rSrc);// no implementation
	void operator=(const CMySocket& rSrc);// no implementation
protected:
	SOCKET m_hSocket;
public:
	
	enum { receives = 0, sends = 1, both = 2 };
	CMySocket()
	{
		m_hSocket = INVALID_SOCKET;
	}
	virtual ~CMySocket()
	{
		Close();		
	}

	virtual int OnReceive()
	{
		return 0;
	};
	virtual void OnConnect()
	{
	   //socket connected;
	}
	virtual void OnClose()
	{
	   //socket breaked;
	}
	////////////////////
	SOCKET GetSocket(){return m_hSocket;};
	//nSocketType:  for windows is SOCK_DGRAM/SOCK_STREAM 
	bool Create(unsigned int nSocketPort = 0,int nSocketType=SOCK_STREAM,int nProtocolType=0,const char* lpszSocketAddress =0);
	bool Listen(int nConnectionBacklog=5);
	virtual bool Accept(CMySocket& rConnectedSocket,sockaddr* lpSockAddr=NULL, int* lpSockAddrLen=NULL);
	virtual void Close();
	bool ShutDown(int nHow = sends);
	
	bool Connect(const char* lpszHostAddress, unsigned int nHostPort);
	
	int Receive(void* lpBuf, int nBufLen, int nFlags = 0);
	int ReceiveFrom(void* lpBuf, int nBufLen, std::string& rSocketAddress, unsigned int& rSocketPort, int nFlags=0);
	
	int  Send(const void* lpBuf, int nBufLen, int nFlags=0);
	//lpszHostAddress=NULL : broadcast
	int  SendTo(const void* lpBuf, int nBufLen, unsigned int nHostPort, const char* lpszHostAddress=NULL, int nFlags=0);
	
	
	bool GetPeerName(std::string& rPeerAddress, unsigned int& rPeerPort);
	bool GetSockName(std::string& rSocketAddress, unsigned int& rSocketPort);
	bool GetPeerInfo(unsigned int& rip,unsigned int& rPeerPort);
    bool GetSockInfo(unsigned int& rip,unsigned int& rSocketPort);
	
		
	bool SetRecvBuf(const unsigned int bufsize)
	{
		return SetSockOpt(SO_RCVBUF,&bufsize,sizeof(unsigned int));
	}
	bool GetRecvBuf(unsigned int& bufsize)
	{
		unsigned int len=sizeof(unsigned int);
		return GetSockOpt(SO_RCVBUF,&bufsize,&len);
	}
	bool SetSendBuf(const unsigned int bufsize)
	{
		
		return SetSockOpt(SO_SNDBUF,&bufsize,sizeof(unsigned int));
	}
    bool GetSendBuf(unsigned int& bufsize)
	{
		unsigned int len=sizeof(unsigned int);
		return GetSockOpt(SO_SNDBUF,&bufsize,&len);
	}

	bool SetKeepLive(bool islive)
	{
	    unsigned int live=islive?1:0;
		return SetSockOpt(SO_KEEPALIVE,&live,sizeof(unsigned int));
		
	}
	bool SetBlock(bool isblock)
	{
#ifdef _WIN32
		unsigned long block=isblock?0:1;
               	return ioctlsocket(m_hSocket,FIONBIO,&block)==0;
#else
                int flags;
                if(fcntl(m_hSocket,F_GETFL,0)<0)
			return false;
                if(isblock)
                   flags|=O_NONBLOCK;
                else
                   flags&=~O_NONBLOCK;
	        return fcntl(m_hSocket,F_SETFL,flags)>=0;   	
#endif
	}
	bool SetSockOpt(int nOptionName, const void* lpOptionValue,	unsigned int nOptionLen, int nLevel = SOL_SOCKET)
	{
		return (setsockopt(m_hSocket,nLevel,nOptionName,(const char*)lpOptionValue,nOptionLen)!=SOCKET_ERROR);
	}
	bool GetSockOpt(int nOptionName, void* lpOptionValue,unsigned int* lpOptionLen, int nLevel = SOL_SOCKET)
	{
		return (getsockopt(m_hSocket,nLevel,nOptionName,(char*)lpOptionValue,(SOCKLEN*)lpOptionLen)!=SOCKET_ERROR);
		
	}
	
	//for review myself
	bool IsSocketReadReady();
	bool IsSocketSendReady();
	bool IsConnectionDropped();
	
	//result >0 , come data,so can read,
	int  ReadableTimeOut(int second, int microsecond);
	//result >0 , buf isn't full, so can send,  
	int  SendableTimeOut(int second, int microsecond);

protected:
	bool Socket(int nSocketType=SOCK_STREAM, int nProtocolType =0,int nAddressFormat =AF_INET);
	bool Bind(unsigned int nSocketPort, const char* lpszSocketAddress);
};
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -