📄 sh_socket.h
字号:
// SH_Socket.h: interface for the SH_Socket class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SH_SOCKET_H__73D2AF62_4FB0_4DD2_9EB0_D6BC18F8E173__INCLUDED_)
#define AFX_SH_SOCKET_H__73D2AF62_4FB0_4DD2_9EB0_D6BC18F8E173__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <Winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#include "SH_Object.h"
class AFX_EXT_CLASS SH_Socket : public SH_Object
{
public:
//If you want derived from SH_Socket,you'd better override
//this virtual functions below.
//
virtual void OnTimer() {}
/*Data to be read*/
virtual BOOL OnRead(int nError){ return FALSE;}
/*Data to be write*/
virtual BOOL OnWrite(int nError){ return FALSE;}
/*Peer connection close on the other end*/
virtual BOOL OnClose(int nError){ return FALSE;}
/*Peer connection established*/
virtual BOOL OnConnect(int nError){ return FALSE;}
/*Get new connection request*/
virtual BOOL OnAccept(int nError){ return FALSE;}
/*Receive Out-Of-Band data*/
virtual BOOL OnReadOOB(int nError){ return FALSE;}
public:
/*Get IP Packet checksum*/
WORD CheckSum(WORD * pSendData,int nSize);
/*Get socket window handle*/
HWND GetSafeHwnd();
/*Use WSAAsyncSelect to work*/
BOOL SelectEx(LONG lEvent);
/*Whether the socket is block or not*/
BOOL IsBlock();
/*Connect with timeout*/
BOOL ConnectEx(const struct sockaddr_in * pSockAddr,int ms = 1000);
/*Connect without timeout*/
BOOL Connect(const struct sockaddr_in * pSockAddr);
/*Connect without timeout*/
BOOL Connect(LPCTSTR pszAddress,WORD port);
/*Connect without timeout*/
BOOL Connect(DWORD ip,WORD port);
/*Set socket option*/
BOOL SetSockOpt(int level,int optname,const char FAR* optval,int optlen);
/*Get socket option*/
BOOL GetSockOpt(int level,int optname,char FAR* optval,int FAR* optlen);
/*Set socket attribute*/
BOOL Ioctl(DWORD cmd,DWORD FAR * argp);
/*Set socket attribute*/
BOOL IoctlEx(DWORD cmd,
LPVOID pInBuf,
DWORD InLen,
LPVOID pOutBuf,
DWORD OutLen,
LPDWORD lpcbBytesReturned,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
/*Set socket attribute*/
BOOL IoctlEx(DWORD cmd,LPVOID pInBuf,DWORD InLen);
/*Unblock socket*/
BOOL Unblock();
/*Block socket*/
BOOL Block();
int Send(const char * pSendBuf,int nBufLen);
int Send(const char * pSendBuf,int nBufLen,int flags);
int SendTo(const char *pSendBuf, int nBufLen,struct sockaddr * addr);
int SendTo(const char *pSendBuf, int nBufLen,struct sockaddr * addr,int flags);
int OOBRecv(char * pRecvBuf,int nBufLen);
int PseudoRecv(char * pRecvBuf,int nBufLen);
int Recv(char * pRecvBuf,int nBufLen);
int Recv(char * pRecvBuf,int nBufLen,int flag);
int RecvFrom(char * pRecvBuf,int nBufLen,struct sockaddr * addr,int flag);
int RecvFrom(char * pRecvBuf,int nBufLen,struct sockaddr * addr);
BOOL Bind(const struct sockaddr_in * pSockAddr);
BOOL Bind(LPCTSTR pszAddress,WORD port);
BOOL Bind(DWORD ip,WORD port);
SOCKET Accept(struct sockaddr_in * pSockAddr);
int Select(int nfds,
fd_set FAR* readfds,
fd_set FAR* writefds,
fd_set FAR* exceptfds,
const struct timeval FAR* timeout);
BOOL Listen(int backlog = 5);
void Shutdown(int how);
void Close();
BOOL Open(int family,int type,int protocol);
BOOL Open();
SOCKET GetSocket(void) const;
void SetFamily(int family) { m_nFamily = family;}
int GetFamily(void) const { return m_nFamily;}
void SetType(int type) { m_nType = type;}
int GetType(void) const { return m_nType;}
void SetProtocol(int protocol) { m_nProtocol = protocol;}
int GetProtocol(void) const { return m_nProtocol;}
SH_Socket();
virtual ~SH_Socket();
//Initialize and uninitialize function
//
//eg.
//CExampleApp::InitInstance()
//{
// ...
// SH_Socket::Initialize()
// ...
//}
//CExampleApp::ExitInstance()
//{
// ...
// SH_Socket::Uninitialize()
// ...
//}
//
static BOOL Initialize();
static BOOL Uninitialize();
//Other utils
private:
SOCKET m_hSocket;
int m_nFamily;
int m_nType;
int m_nProtocol;
BOOL m_bBlock;
};
#endif // !defined(AFX_NET_H__73D2AF62_4FB0_4DD2_9EB0_D6BC18F8E173__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -