封装socket系列函数

这几天给师弟们辅导socket编程,虽然理论方面都感觉不会太难,但编程部分好像很难理解。所以写了一个类,参考了python的socket编程方法,把常用函数封装成一个头文件,力求简洁。这样果然就好多了,呵呵。

挺高兴的,纪念下。

///////////////////////////////////////
//NewSocket.h
///////////////////////////////////////
#pragma
once #include <winsock2.h> #pragma comment(lib,"ws2_32") //Socket信息结构 struct SocketInfo { char IP[16]; int PORT; }; //定义类 class NewSocket { public: SOCKET S; SocketInfo SockInfo; NewSocket(); ~NewSocket(); //服务端函数 bool Bind(char *IP,int PORT); bool Listen(int backlog); void Accept(NewSocket &client); //客户端函数 bool Connect(char *RemoteIP,int RemotePort); //通用函数 bool Send(char *SendData,int SendLen); bool Recv(char *RecvData,int RecvLen); bool SetReuseAddr(bool Flag); bool SetKeepAlive(bool Flag); bool SetRecvBufferSize(int BufferSize); bool SetSendBufferSize(int BufferSize); bool SetRecvTimeOut(int TimeOut); bool SetSendTimeOut(int TimeOut); bool Close(); }; //构造函数 NewSocket::NewSocket() { WSADATA ws; WSAStartup(MAKEWORD(2,2),&ws); S=INVALID_SOCKET; strcpy(SockInfo.IP,"0.0.0.0"); SockInfo.PORT=0; } //析构函数 NewSocket::~NewSocket() { WSACleanup(); closesocket(this->S); } //绑定函数(绑定IP,绑定端口) bool NewSocket::Bind(char *IP,int PORT) { SOCKADDR_IN sin; sin.sin_family=AF_INET; sin.sin_port=htons(PORT); if (IP==NULL) { sin.sin_addr.s_addr=INADDR_ANY; strcpy(SockInfo.IP,"127.0.0.1"); } else { sin.sin_addr.s_addr=inet_addr(IP); strcpy(SockInfo.IP,IP); } SockInfo.PORT=PORT; S=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if (::bind(this->S,(sockaddr *)&sin,sizeof(sin))!=0) return false; else return true; } //监听函数(同时连接数) bool NewSocket::Listen(int backlog=1) { if (::listen(this->S,backlog)!=0) return false; else return true; } //接受客户函数(套接字变量) void NewSocket::Accept(NewSocket &client) { SOCKADDR_IN sin; int sinlen=sizeof(sin); client.S=::accept(this->S,(sockaddr *)&sin,&sinlen); strcpy(client.SockInfo.IP,inet_ntoa(sin.sin_addr)); client.SockInfo.PORT=htons(sin.sin_port); } //发送数据(待发送数据,数据长度) bool NewSocket::Send(char *SendData,int SendLen=-1) { if (SendLen==-1) SendLen=strlen(SendData); if (::send(this->S,SendData,SendLen,0)!=SOCKET_ERROR) return true; else return false; } //接收数据(接收数据的变量名,接收的数据长度) bool NewSocket::Recv(char *RecvData,int RecvLen) { if (::recv(this->S,RecvData,RecvLen,0)>0) return true; else return false; } //客户端连接函数(远程IP,远程端口) bool NewSocket::Connect(char *RemoteIP,int RemotePort) { SOCKADDR_IN sin,localsin; int sinlen=sizeof(sin); sin.sin_family=AF_INET; sin.sin_addr.s_addr=inet_addr(RemoteIP); sin.sin_port=htons(RemotePort); S=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if (::connect(this->S,(sockaddr *)&sin,sizeof(sin))!=0) return false; else { if (getsockname(S,(sockaddr *)&localsin,&sinlen)==0){ strcpy(this->SockInfo.IP,inet_ntoa(localsin.sin_addr)); this->SockInfo.PORT=(int)htons(localsin.sin_port); } return true; } } //关闭socket后是否立刻关闭端口(true or false) bool NewSocket::SetReuseAddr(bool Flag=true) { if (setsockopt(this->S,SOL_SOCKET,SO_REUSEADDR,(const char *)&Flag,sizeof(BOOL))==0){ return true; }else{ return false; } } //是否保持连接(true or false) bool NewSocket::SetKeepAlive(bool Flag=true) { if (setsockopt(this->S,SOL_SOCKET,SO_KEEPALIVE,(const char *)&Flag,sizeof(BOOL))==0){ return true; }else{ return false; } } //设置发送缓冲区大小(新大小) bool NewSocket::SetSendBufferSize(int BufferSize=1024) { if (setsockopt(this->S,SOL_SOCKET,SO_SNDBUF,(const char *)&BufferSize,sizeof(int))==0){ return true; }else{ return false; } } //设置接收缓冲区大小(新大小) bool NewSocket::SetRecvBufferSize(int BufferSize=1024) { if (setsockopt(this->S,SOL_SOCKET,SO_RCVBUF,(const char *)&BufferSize,sizeof(int))==0){ return true; }else{ return false; } } //设置接收数据超时(时间/毫秒) bool NewSocket::SetRecvTimeOut(int TimeOut=0) { if (setsockopt(this->S,SOL_SOCKET,SO_RCVTIMEO,(const char *)&TimeOut,sizeof(int))==0){ return true; }else{ return false; } } //设置发送数据超时(时间/毫秒) bool NewSocket::SetSendTimeOut(int TimeOut=0) { if (setsockopt(this->S,SOL_SOCKET,SO_SNDTIMEO,(const char *)&TimeOut,sizeof(int))==0){ return true; }else{ return false; } } //关闭套接字() bool NewSocket::Close() { if (closesocket(this->S)==0){ this->S=INVALID_SOCKET; return true; }else{ return false; } }

 

用法示例:

///////////////////////////
//client
//////////////////////////
#include <stdio.h> #include <NewSocket.h> void main() { NewSocket client; char szRecv[255]={0}; if (client.Connect("127.0.0.1",5566)) { client.Send("hello"); client.Recv(szRecv,255); printf("%s\n",szRecv); } else printf("Connect failed!\n"); client.Close(); }
/////////////////////
//server
////////////////////
#include <stdio.h> #include <NewSocket.h> void main() { NewSocket server,client; char szRecv[255]={0}; //绑定所有ip server.Bind(NULL,5566); //监听,同时最大连接数默认为1 server.Listen(); //保存客户套接字到client server.Accept(client); client.Send("hello"); client.Recv(szRecv,255); printf("%s\n",szRecv); }

 

posted @ 2012-05-21 10:11  little evil  阅读(564)  评论(0编辑  收藏  举报