📄 socket.h
字号:
/***************************************************************************
socket.h - description
-------------------
begin : Fri Jul 20 2001
copyright : (C) 2001 by Mark
email : alben@yeah.net
modifier : yt yfrom_to@sina.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef SOCKET_H_
#define SOCKET_H_
#include "exception.h"
#if defined(OS_SUN)
#define _POSIX_C_SOURCE 199506L
/* Note that you need to define __EXTENSIONS__ when using sockets. */
#define __EXTENSIONS__
#endif
#if defined(WIN32)
#include <Winsock2.h>
#else
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h> //inet_addr
//#include <sys/un.h> //unix domain
#include <arpa/inet.h> //inet_addr
#include <netdb.h> //gethostbyname
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
#ifndef INADDR_NONE
#define INADDR_NONE -1
#endif
#endif
namespace mylib{
typedef unsigned short int tcport_t;
class CSocket;
class CTcpSocket;
class CInetAddress
{
friend class CSocket;
friend class CTcpSocket;
public:
CInetAddress();
CInetAddress(const struct sockaddr_in& stInetAddr);
CInetAddress(const char* ipAddr,int port);
CInetAddress& operator = (const struct sockaddr_in& stInetAddr);
CInetAddress& operator = (const CInetAddress& sInetAddr);
/*由于
//int TcpSocket::OpenwithTmOut(const CInetAddress& stInetAddr,int nSec,int nMs);
//int TcpSocket::OpenwithTmOut(const char *IpAddr,tcport_t port,int nSec,int nMs);
调用OpenwithTmOut(const char *IpAddr,tcport_t port,int nSec)
时如果大意丢失一个参数就会导致CInetAddress(const char *IpAddr)
被调用而port参数则作为nSec被调用了
//CInetAddress(const char* sInetAddr);
//CInetAddress& operator = (const char* sInetAddr);
*/
inline struct sockaddr_in Getsockaddr() const { return m_stInetAddr; }
tcport_t GetPort() const {return m_stInetAddr.sin_port;}
std::string GetIPaddstr(const char splitch='.') const;
bool IsInetAddress() const;
protected:
struct sockaddr_in m_stInetAddr;
};
#if defined(WIN32)
class Cinit_WSA
{
public:
Cinit_WSA();
~Cinit_WSA();
};
#endif
class CSocket
{
friend class CTcpSocket;
protected:
enum CState
{
S_INITIAL,
S_AVAILABLE,
S_BOUND,
S_CONNECTED,
S_CONNECTING
};
public:
int SocketFd() const{ return m_iSocket; }
CInetAddress GetInetAddr() const {return m_InetAddr;}
void SetBlock(bool bBlock) throw (CSocketException);
int Shutdown(int how) throw();
void End() throw();
protected:
CSocket(int iDomain, int iType, int iProtocol = 0);
CSocket(){Init();}
virtual ~CSocket() { End(); }
void ServerBind(const tcport_t tPort);
void SockREUSEADDR();
void SetMember(int sockfd,bool bloc,CState sta,int dom);
protected:
#if defined(WIN32)
SOCKET m_iSocket;
#else
int m_iSocket;
#endif
int m_iDomain;
CState m_eState;
bool m_bBlock;
CInetAddress m_InetAddr;
private:
void Init();
void Socket(int iDomain, int iType, int iProtocol = 0)throw(CSocketException);
//存储本地ip,端口号
void StoreLocalIP_PORT();
};
class CTcpSocket : public CSocket
{
public:
struct ChSok{};
protected:
// return false will close new connected socket
virtual bool OnAccept(const CInetAddress& stInetAddr, tcport_t tPort){ return true; }
virtual bool OnCheckOpenIntr() { return true; }
virtual bool OnAcceptIntr() { return true; }
public:
virtual ~CTcpSocket() {}
//服务器
CTcpSocket(const tcport_t tPort);
//服务器
//open as server to listen ...
void Open(int iBackLog = 5);
//服务器生成
CTcpSocket(ChSok a){};
//客户端
CTcpSocket();
//客户端
//open as client to connect ...
//if EINPROGRESS or EINTR error occurs return false
//if other error occurs throw exception
//else return true
bool Open(const CInetAddress& stInetAddr);
bool Open(const char *IpAddr,tcport_t port);
int OpenwithTmOut(const CInetAddress& stInetAddr,int nSec,int nMs);
int OpenwithTmOut(const char *IpAddr,tcport_t port,int nSec,int nMs);
//if open with return value false should call this function to check if already opened
//if not connected return false
//if error occurs throw exception
//if connected return true
bool CheckOpen(long lSec, long lUSec = 0);
//accept incomming connections ...
bool Accept(CSocket &connectsock);
bool Accept(CSocket *connectsock){return Accept(*connectsock);}
int AcceptwithTmOut(CSocket &connectsock,int nSec,int nMs=0);
int AcceptwithTmOut(CSocket *connectsock,int nSec,int nMs=0){return AcceptwithTmOut(*connectsock,nSec,nMs);}
//receive one line(end with \n)set last char to '\0'
//suppose the server return one line once
int ReadLine(char *bufprt,size_t len);
//需要明确实际接受、发送的字节数
int Writen(const char* pch, const int nSize, const int nSecs,const int nMs=0);
int Readn(char* pch, const int nSize, const int nSecs,const int nMs=0);
//以下的可以使用大等于,实际接受、发送的字节数
int Send(const char* pch, const int nSize, const int nSecs,const int nMs=0);
int Receive(char* pch, const int nSize, const int nSecs,const int nMs=0);
int BaseSend(const char* pch, const int nSize);
int BaseRead(char* pch, const int nSize);
//////////////////////////////////////////////////////////////////////////
private:
void Listen(int iBackLog = 5);
bool Isconnected(fd_set *rd, fd_set *wr, fd_set *ex );
};
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -