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

📄 udpsocket.h

📁 网络socket,IO,线程池
💻 H
字号:
/*
   2003 by zhy
*/
#ifndef _MY_UDP_SOCKET_H
#define _MY_UDP_SOCKET_H

#pragma warning(disable:4786)

#include <string>
using namespace std;

#include <winsock2.h>
#include "SocketBase.h"




namespace NET{

class	CUdpSocket:public CSocketBase
{
public:
	////////////////////////////////////////////////////////////////////////////
	// Constructors / Destructor                                              //
	////////////////////////////////////////////////////////////////////////////
	
	// Construct
	CUdpSocket(SOCKET sock = INVALID_SOCKET);
	// Deconstruct
	~CUdpSocket();
	
	////////////////////////////////////////////////////////////////////////////
	// Inline Set/Get Operations                                              //
	////////////////////////////////////////////////////////////////////////////
	inline	int		gettimeout() const { return m_timeout; }
	inline	int		gettimeoutsec_u() const { return m_timeoutU; }
	inline	void		settimeout(int timeout = 0,int timeoutUsec = 0) 
	{ 
		m_timeout = timeout; 
		m_timeoutU = timeoutUsec;
	}
	
	inline  void   		Getpeerinfo(struct sockaddr_in & peeradd)
	{
		memcpy(&peeradd,&m_peeradd,sizeof(m_peeradd));
	}	
	inline  void   		Setpeerinfo(const struct sockaddr_in & peeradd)
	{
		memcpy(&m_peeradd,&peeradd,sizeof(m_peeradd));
	}
	inline  void   		Setpeerinfo(unsigned short sin_port,unsigned long sin_addr)
	{
		m_peeradd.sin_addr.s_addr = sin_addr;
		m_peeradd.sin_port = sin_port;
	}
	
	// You should allocate the space for ip yourself
	inline  void   		Getpeerip(unsigned char* ip)
	{
		memcpy(ip,&m_peeradd.sin_addr.S_un.S_un_b,sizeof(in_addr));
	}
	inline  void   		Getpeerip(string& ip)
	{
	        unsigned char cip[4];
    		Getpeerip(cip);
			char buf[10];
			ip = "";	
			for(int i = 0; i < 4; i++)
			{
				itoa(cip[i],buf,10);
        			ip += buf;
				if(i != 3)
					ip += ".";
			}
	}	
	
	////////////////////////////////////////////////////////////////////////////
	// Operations		                                                  //
	////////////////////////////////////////////////////////////////////////////
	bool	 Listen(unsigned short uPort,unsigned long ip = INADDR_ANY);
	bool	 Connectbroadcast(unsigned short uPort);
	bool	 Connect(const string& IP, unsigned short uPort);

	// Muticast's receive should judge if the computer support it
	// Muticast's Address range from 224.0.1.0 to 239.255.255.255
	bool 	 Supportmuticast(unsigned long ip,unsigned short port);
	bool     SetTTL(unsigned char TTL);
	
	// the Main R/W Operation
	int 	 Write(const void *buf, size_t len);
	int 	 Read(void *buf, size_t len);
	int 	 Peek(void *buf, size_t len);
	
	// Close
	void 	 Close();
	void 	 Onlyclose();
	void 	 Shutdown();
	
	// Hidden Type Change
	operator SOCKET() const { return m_sock; }

	
protected:
	
	sockaddr_in			m_peeradd;
	int				m_length;
	int				m_timeout;
	int				m_timeoutU;
	bool				m_isserver;
};




}

#endif

⌨️ 快捷键说明

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