加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

跨平台C++ Socket类 sock.h

(2005-05-26 11:08:38)
标签:

杂谈

分类: 计算机与 Internet


//---------------------------------------------------------------------------

#ifndef SockUnitH
#define SockUnitH
//---------------------------------------------------------------------------
#ifdef WIN32
#include <winsock2.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>

#define SOCKET int
#endif

#pragma warning (disable: 4786)

#include <string>
#include <vector>
#include <map>
using namespace std;

#define SOCKET_TIMEOUT 30
#define MAX_RECV_BUFFERSIZE 65535


//!Socket基本功能封装类
class BaseSock
{
public:
 SOCKET m_sock;

public:
 BaseSock();
 virtual ~BaseSock();

 bool Create(bool bUDP=false);

 virtual bool Connect(const string& host,unsigned short port);
 virtual bool isConnected();
 virtual bool isUDP();
 virtual bool Bind(unsigned short nPort);
 virtual bool Accept(BaseSock& client);
 virtual void Close();

 virtual long Send(const char* buf,long buflen);
 virtual long Recv(char* buf,long buflen);
 virtual long SendTo(const char* buf,int len,const struct sockaddr_in* toaddr,int tolen);
 virtual long RecvFrom(char* buf,int len,struct sockaddr_in* fromaddr,int* fromlen);

 virtual bool GetPeerName(string& strIP,unsigned short &nPort);

 SOCKET GetHandle();
private:
 bool m_bUDP;
 bool m_bConnected;

 string m_strHost;
 unsigned short m_nPort;

};

#endif

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有