cls_sock.h

来自「ABis无线接口全套资料」· C头文件 代码 · 共 375 行

H
375
字号
/* ======================================================================== *\
 |
 |
 |  JOYIT Communication Technology
 |  Copyright (C)  2003-2006,  All Right Reserved.
 |
 |  Filename: cls_sock.h
 |  Environment: Red Hat Linux 7.2 & GNU C/C++ Compiler 2.96
 |				 Windows Microsoft Visual C/C++ 6.0
 |  Function description: 
 |		(1) CLS_Socket operating on TCP socket;
 |		(2) CLS_TCPConnect inherited from CLS_Socket, used by CLS_TCPSvr;
 |		(3) CLS_TCPSvr supply a TCP Server class;
 |		(4) CLS_TCPClient supply a TCP Client class;
 |		(5) CLS_UDP supply a UDP class
 |
\* ======================================================================== */

#ifndef _CLS_SOCKET
#define _CLS_SOCKET

extern "C"
{
#ifndef _STDLIB_H
#include <stdlib.h>
#endif

#ifndef _STDIO_H
#include <stdio.h>
#endif

#ifndef _ERRNO_H
#include <errno.h>
#endif

#ifndef _STRING_H
#include <string.h>
#endif

#ifndef _FCNTL_H
#include <fcntl.h>
#endif

#ifndef _UNISTD_H
#include <unistd.h>
#endif

#ifndef _SYS_SOCKET_H
#include <sys/socket.h>
#endif

#ifndef _NETDB_H
#include <netdb.h>
#endif

#ifndef _NETINET_IN_H
#include <netinet/in.h>
#endif

#ifndef _ARPA_INET_H
#include <arpa/inet.h>
#endif

#ifndef _SYS_SELECT_H
#include <sys/select.h>
#endif
};

#define MAX_IP_LEN		32		// IP Address buffer length
#define MAX_CONNECTION	128		// Maximium Connections A Server can support
#define MAX_TRY_SOCK	10		// Maximium try times when a (nonblocking) socket send data
#define SOCK_SLEEP		50		// Sleep wait block is disappeared
//#define MAX_WAIT_CONN	1		// Wait time when a nonblocking socket wait connecting to server

#define TCP_RECV_BUFF	10240	// TCP recv buffer length of each socket
#define TCP_SEND_BUFF	10240	// TCP send buffer length of each socket

//Socket status
#define SS_UNUSED		0		// unused socket
#define SS_CREATED		1		// socket is created
#define SS_BOUND		2		// socket is bound a IP address
#define SS_LISTEN		3		// socket is listening for client connecting in
#define SS_CONNECT		4		// socket is connected

int GetTCPLibVer(char *strVer);
/*************************************************************************
Decription: Get TCP Class Library's Version information
strVer: 	Version information buffer, will be filled on return
Return:		Version Number, every byte represent a sub-version NO from highest
			significant byte to lowest significant byte

*************************************************************************/

#ifdef _WIN32
extern int g_iSockCount;
#endif

class CLS_Socket
{
protected:
	int m_iSocket;				// socket id
	int m_iStatus;				// socket status
	int m_iSocketError;

public:
	CLS_Socket();
	CLS_Socket(int iSocket, int iStatus=SS_CREATED);	// constructor from a socket id
	virtual ~CLS_Socket();

public:
	int SetSockOpt(int iLevel, int iOptName, const char *pOptVal, int iOptLen);
/*************************************************************************
Decription: set all kind of options of the socket
iLevel: 	big group of option
iOptName:	option's name
pOptVal:	the pointer to the buffer for the option's value
iOptLen:	the length of buffer for option's value
Return:		Success:0; Fail: other value, (mostly -1)
*************************************************************************/

	int GetSockOpt(int iLevel, int iOptName, char *pOptVal, int *pOptLen);
/*************************************************************************
Decription: get all kind of options of the socket
iLevel: 	big group of option
iOptName:	option's name
pOptVal:	the pointer to the buffer for the option's value
pOptLen:	the length of buffer for option's value, must be filled by correct
Return:		Success:0; Fail: other value, (mostly -1)
*************************************************************************/

public:
	virtual int CreateSocket();
/*************************************************************************
Decription: Create Socket for the class, default is a TCP(STREAM) socket
Return:		Success: socket value; Fail: -1
*************************************************************************/

	int CloseSocket();	
/*************************************************************************
Decription: Close Socket
Return:		Success: 0; Fail: -1
*************************************************************************/

	int SetNonBlock(unsigned long iFlag=1);
/*************************************************************************
Decription: Set the Socket as a nonblocking socket
Return:		Success: socket value; Fail: -1
*************************************************************************/

	virtual int SetSocketProp();
/*************************************************************************
Decription: Set Socket set some option, default set SO_REUSEADDR, 
			SO_SNDBUF, SO_RCVBUF
Return:		Success: socket value; Fail: <0
*************************************************************************/

public:
	int Bind(const char *strIP, int iPort);
/*************************************************************************
Decription: Bind socket to the specified IP and Port
strIP:		IP address the socket to be bound, local IP Address
iPort:		Port the socket to be bound
Return:		Success: 0; Fail: -1
*************************************************************************/

	char *GetLocalHost(int *pPort);
/*************************************************************************
Decription: Get Local Host's IP address and Port of the socket, 
			use this function after bind() or listen or connected
pPort:	 	local Port on return
Return:		local IP address on success; Fail: NULL
*************************************************************************/

	virtual int SendData(void *pData, int iMaxLen);
/*************************************************************************
Decription: Send data on the socket
pData:		data to be send
iMaxLen:	data's length
Return:		Success: iMaxLen; Fail: <0
*************************************************************************/

	virtual int RecvData(void *pData, int iMaxLen);
/*************************************************************************
Decription: Receive data on the socket
pData:		data to be received
iMaxLen:	data's length
Return:		Success: iMaxLen; Fail: <0
*************************************************************************/

	virtual int SendTo(const char *strPeerAddr, int iPeerPort, void *pData, int iMaxLen);
/*************************************************************************
Decription: Send data on the socket, specify peer's IP address and port
strPeerAddr:Peer's IP address
iPeerPort:	Peer's Port
pData:		data to be send
iMaxLen:	data's length
Return:		Success: iMaxLen; Fail: <0
*************************************************************************/

	virtual int RecvFrom(char *strPeerAddr, int *pPeerPort, void *pData, int iMaxLen);
/*************************************************************************
Decription: Receive data on the socket, can get peer's IP address and port
strPeerAddr:Peer's IP address
pPeerPort:	Peer's Port
pData:		data to be received
iMaxLen:	data's length

Return:		Success: iMaxLen; Fail: <0
*************************************************************************/

public:
	int GetStatus();

	int GetSocket()
	{
		return m_iSocket;
	}

	int GetSocketError()
	{
		return m_iSocketError;
	}
};

// TCP Connect struct used by TCP Server class
class CLS_TCPConnect:public CLS_Socket
{
protected:
	char m_strIP[MAX_IP_LEN];
	int m_iPort;

public:
	CLS_TCPConnect();
	CLS_TCPConnect(int iSocket, const char *strIP, int iPort);

public:
	void SetConnectProp(int iSocket, const char *strIP, int iPort);
	char *GetIP();
	int GetPort();
};

// TCP Server Class
class CLS_TCPSvr:public CLS_Socket
{
protected:
	char m_strLocalIP[MAX_IP_LEN];	// local IP address
	int m_iLocalPort;				// local Port the server listening on
	CLS_TCPConnect *m_pClient[MAX_CONNECTION];	// TCP connections of clients
	int m_iCount;					// connections count

public:
	CLS_TCPSvr();
	virtual ~CLS_TCPSvr();

public:								// Server's protery 
	void SetLocalPort(int iPort);
	int GetLocalPort();
	void SetLocalIP(const char *strLocalIP);
	char *GetLocalIP();
	CLS_TCPConnect *GetClient(int index);
	int GetClientCount();

public:								// server's opertion
	int Bind();
/*************************************************************************
Decription: Bind socket to the local IP and Port
Return:		Success: 0; Fail: -1
*************************************************************************/

	int Listen();					
/*************************************************************************
Decription: Listening on the local port for client connecting in
Return:		Success: 0; Fail: <0
*************************************************************************/

	int Accept();
/*************************************************************************
Decription: Accept client connction
Return:		Success: TCP Connection's index; Fail: <0
*************************************************************************/
	int AddConnect(int iSocket, const char *strIP, int iPort);
};

// TCP Client Class
class CLS_TCPClient:public CLS_Socket
{
protected:
	char m_strSvrIP[MAX_IP_LEN];	// Server's IP Addres to connect
	int m_iSvrPort;					// Server's Port
	char m_strLocalIP [MAX_IP_LEN];	// Local IP Address
	int m_iLocalPort;				// Local Port

public:
	CLS_TCPClient();
	virtual ~CLS_TCPClient();

public:								// Client's property
	void SetSvrIP(const char *strIP);
	char *GetSvrIP();
	void SetSvrPort(int iPort);
	int GetSvrPort();
	void SetLocalIP(const char *strLocalIP);
	char *GetLocalIP();
	void SetLocalPort(int iPort);
	int GetLocalPort();

public:								// Client's opertion
	int Bind();
/*************************************************************************
Decription: Bind default IP and Port to local
Return:		Success: 0; Fail: -1
*************************************************************************/

	int Connect();
/*************************************************************************
Decription: Connect to Server
Return:		Success: 0; Fail: <0
*************************************************************************/
};

// UDP class
class CLS_UDP:public CLS_Socket
{
protected:
	char m_strPeerIP[MAX_IP_LEN];	// peer's IP Address
	int m_iPeerPort;				// peer's Port
	char m_strLocalIP [MAX_IP_LEN];	// local's IP Address
	int m_iLocalPort;				// local's Port

public:
	CLS_UDP();
	virtual ~CLS_UDP();

public:								// UDP's property
	void SetPeerIP(const char *strIP);
	char *GetPeerIP();
	void SetPeerPort(int iPort);
	int GetPeerPort();
	void SetLocalIP(const char *strIP);
	char *GetLocalIP();
	void SetLocalPort(int iPort);
	int GetLocalPort();

public:								// UDP's opertion
	virtual int CreateSocket();
/*************************************************************************
Decription: Create Socket for the class, UDP(DGRAM) socket
Return:		Success: socket value; Fail: -1
*************************************************************************/

	int Bind();
/*************************************************************************
Decription: Bind default IP and Port to local
Return:		Success: 0; Fail: -1
*************************************************************************/

	int Connect();
/*************************************************************************
Decription: Connect to Peer UDP Entity, using peer IP and Port
Return:		Success: 0; Fail: <0
*************************************************************************/
};
#endif

// ------------------------------------------------------------------------
//
//  Revision list.
//  ==============
//
//  1.0,        2003-05-05,     Yang guangliang
//      Initial version.
//
// ------------------------------------------------------------------------

⌨️ 快捷键说明

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