跨平台C++ Socket类, sock.cpp
(2005-05-26 11:09:22)
转载▼
标签:杂谈
分类:计算机与 Internet
//---------------------------------------------------------------------------
#include
#include "sock.h"
#ifndef WIN32
#define strnicmp strncasecmp
#endif
BaseSock::BaseSock()
{
m_bUDP = false;
m_sock = -1;
m_nPort = 0;
m_bConnected = false;
}
BaseSock::~BaseSock()
{
Close();
}
SOCKETBaseSock::GetHandle()
{
return m_sock;
}
void BaseSock::Close()
{
if(m_sock!=-1)
{
#ifdef WIN32
shutdown(m_sock,SD_BOTH);
closesocket(m_sock);
#else
shutdown(m_sock, SHUT_RDWR);
close(m_sock);
#endif
m_sock=-1;
}
m_bConnected = false;
}
bool BaseSock::Create(boolbUDP)
{
m_bUDP = bUDP;
if(!m_bUDP)
m_sock = socket(AF_INET,SOCK_STREAM,0);
else
{
m_sock = socket(AF_INET,SOCK_DGRAM,0);
if(m_sock!=-1)
{
m_bConnected = true;
}
}
return (m_sock!=-1);
}
bool BaseSock::Connect(conststring& host,unsigned short port)
{
if(m_sock==-1)
return false;
m_strHost=host;
m_nPort=port;
struct hostent *he=gethostbyname(host.c_str());
if(he==NULL)
{
Close();
return false;
}
struct sockaddr_insin;
sin.sin_family=AF_INET;
sin.sin_addr=*((struct in_addr*)he->h_addr);
memset(sin.sin_zero,0,8);
sin.sin_port=htons(port);
if(connect(m_sock,(struct sockaddr *)&sin,sizeof(sin)))
{
Close();
return false;
}
//unsigned longrb=1;
//ioctlsocket(m_sock,FIONBIO,&rb);
int bufsize = MAX_RECV_BUFFERSIZE;
setsockopt(m_sock, SOL_SOCKET, SO_RCVBUF,(char*)&bufsize, sizeof(bufsize));
//int timeout= 20000;
//setsockopt(m_sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout, sizeof(timeout));
m_bConnected = true;
return true;
}
long BaseSock::Send(const char*buf,long buflen)
{
//printf("Send:%.*s\r\n",buflen,buf);
if(m_sock==-1)
{
return-1;
}
int start=0;
int sended=0;
do
{
int len=send(m_sock,buf+sended,buflen-sended,0);
if(len
{
break;
}
sended+=len;
}while(sended
return sended;
}
long BaseSock::Recv(char*buf,long buflen)
{
if(m_sock==-1)
{
return-1;
}
fd_set fd;
FD_ZERO(&fd);
FD_SET(m_sock,&fd);
struct timeval val= {SOCKET_TIMEOUT,0};
int selret =select(m_sock+1,&fd,NULL,NULL,&val);
if (selret
{
returnselret;
}
int len=recv(m_sock,buf,buflen,0);
//printf("Recv:%.*s\r\n",len,buf);
return len;
}
boolBaseSock::GetPeerName(string&strIP,unsigned short &nPort)
{
struct sockaddr_in addr;
memset(&addr,0,sizeof(addr));
int addrlen=sizeof(addr);
#ifdef WIN32
if(getpeername(m_sock,(structsockaddr*)&addr,&addrlen)!=0)
#else
if(getpeername(m_sock,(structsockaddr*)&addr,(socklen_t*)&addrlen)!=0)
#endif
return false;
char szIP[64];
#ifdef WIN32
sprintf(szIP,"%u.%u.%u.%u",addr.sin_addr.S_un.S_addr&0xFF,(addr.sin_addr.S_un.S_addr>>8)&0xFF,(addr.sin_addr.S_un.S_addr>>16)&0xFF,(addr.sin_addr.S_un.S_addr>>24)&0xFF);
#else
sprintf(szIP,"%u.%u.%u.%u",addr.sin_addr.s_addr&0xFF,(addr.sin_addr.s_addr>>8)&0xFF,(addr.sin_addr.s_addr>>16)&0xFF,(addr.sin_addr.s_addr>>24)&0xFF);
#endif
strIP=szIP;
nPort=ntohs(addr.sin_port);
return true;
}
boolBaseSock::isConnected()
{
return(m_sock!=-1)&&m_bConnected;
}
bool BaseSock::isUDP()
{
return m_bUDP;
}
long BaseSock::SendTo(constchar* buf,int len,const struct sockaddr_in* toaddr,int tolen)
{
if(m_sock==-1)
{
return-1;
}
return sendto(m_sock,buf,len,0,(const structsockaddr*)toaddr,tolen);
}
long BaseSock::RecvFrom(char*buf,int len,struct sockaddr_in* fromaddr,int* fromlen)
{
if(m_sock==-1)
{
return-1;
}
#ifdef WIN32
return recvfrom(m_sock,buf,len,0,(structsockaddr*)fromaddr,fromlen);
#else
return recvfrom(m_sock,buf,len,0,(structsockaddr*)fromaddr,(socklen_t*)fromlen);
#endif
}
bool BaseSock::Bind(unsignedshort nPort)
{
if(m_sock==-1)
{
return false;
}
struct sockaddr_in sin;
sin.sin_family=AF_INET;
#ifdef WIN32
sin.sin_addr.S_un.S_addr=0;
#else
sin.sin_addr.s_addr=0;
#endif
memset(sin.sin_zero,0,8);
sin.sin_port=htons(nPort);
if(bind(m_sock,(sockaddr*)&sin,sizeof(sockaddr_in))!=0)
return false;
listen(m_sock,1024);
m_bConnected = true;
return true;
}
boolBaseSock::Accept(BaseSock&client)
{
if(m_sock==-1)
{
return false;
}
client.m_sock = accept(m_sock,NULL,NULL);
client.m_bConnected = true;
return (client.m_sock != -1);
}
分享:
分享到新浪Qing
0
喜欢
阅读(606)┊评论 (0)┊收藏(0) ┊转载(0) ┊喜欢▼ ┊打印┊举报
前一篇:跨平台C++ Socket类 sock.h
后一篇:跨平台C++ Http Get和Post 函数,http.h
跨平台C++ Socket类, sock.cpp
(2005-05-26 11:09:22)
转载▼
标签:杂谈
分类:计算机与 Internet
//---------------------------------------------------------------------------
#include
#include "sock.h"
#ifndef WIN32
#define strnicmp strncasecmp
#endif
BaseSock::BaseSock()
{
m_bUDP = false;
m_sock = -1;
m_nPort = 0;
m_bConnected = false;
}
BaseSock::~BaseSock()
{
Close();
}
SOCKETBaseSock::GetHandle()
{
return m_sock;
}
void BaseSock::Close()
{
if(m_sock!=-1)
{
#ifdef WIN32
shutdown(m_sock,SD_BOTH);
closesocket(m_sock);
#else
shutdown(m_sock, SHUT_RDWR);
close(m_sock);
#endif
m_sock=-1;
}
m_bConnected = false;
}
bool BaseSock::Create(boolbUDP)
{
m_bUDP = bUDP;
if(!m_bUDP)
m_sock = socket(AF_INET,SOCK_STREAM,0);
else
{
m_sock = socket(AF_INET,SOCK_DGRAM,0);
if(m_sock!=-1)
{
m_bConnected = true;
}
}
return (m_sock!=-1);
}
bool BaseSock::Connect(conststring& host,unsigned short port)
{
if(m_sock==-1)
return false;
m_strHost=host;
m_nPort=port;
struct hostent *he=gethostbyname(host.c_str());
if(he==NULL)
{
Close();
return false;
}
struct sockaddr_insin;
sin.sin_family=AF_INET;
sin.sin_addr=*((struct in_addr*)he->h_addr);
memset(sin.sin_zero,0,8);
sin.sin_port=htons(port);
if(connect(m_sock,(struct sockaddr *)&sin,sizeof(sin)))
{
Close();
return false;
}
//unsigned longrb=1;
//ioctlsocket(m_sock,FIONBIO,&rb);
int bufsize = MAX_RECV_BUFFERSIZE;
setsockopt(m_sock, SOL_SOCKET, SO_RCVBUF,(char*)&bufsize, sizeof(bufsize));
//int timeout= 20000;
//setsockopt(m_sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout, sizeof(timeout));
m_bConnected = true;
return true;
}
long BaseSock::Send(const char*buf,long buflen)
{
//printf("Send:%.*s\r\n",buflen,buf);
if(m_sock==-1)
{
return-1;
}
int start=0;
int sended=0;
do
{
int len=send(m_sock,buf+sended,buflen-sended,0);
if(len
{
break;
}
sended+=len;
}while(sended
return sended;
}
long BaseSock::Recv(char*buf,long buflen)
{
if(m_sock==-1)
{
return-1;
}
fd_set fd;
FD_ZERO(&fd);
FD_SET(m_sock,&fd);
struct timeval val= {SOCKET_TIMEOUT,0};
int selret =select(m_sock+1,&fd,NULL,NULL,&val);
if (selret
{
returnselret;
}
int len=recv(m_sock,buf,buflen,0);
//printf("Recv:%.*s\r\n",len,buf);
return len;
}
boolBaseSock::GetPeerName(string&strIP,unsigned short &nPort)
{
struct sockaddr_in addr;
memset(&addr,0,sizeof(addr));
int addrlen=sizeof(addr);
#ifdef WIN32
if(getpeername(m_sock,(structsockaddr*)&addr,&addrlen)!=0)
#else
if(getpeername(m_sock,(structsockaddr*)&addr,(socklen_t*)&addrlen)!=0)
#endif
return false;
char szIP[64];
#ifdef WIN32
sprintf(szIP,"%u.%u.%u.%u",addr.sin_addr.S_un.S_addr&0xFF,(addr.sin_addr.S_un.S_addr>>8)&0xFF,(addr.sin_addr.S_un.S_addr>>16)&0xFF,(addr.sin_addr.S_un.S_addr>>24)&0xFF);
#else
sprintf(szIP,"%u.%u.%u.%u",addr.sin_addr.s_addr&0xFF,(addr.sin_addr.s_addr>>8)&0xFF,(addr.sin_addr.s_addr>>16)&0xFF,(addr.sin_addr.s_addr>>24)&0xFF);
#endif
strIP=szIP;
nPort=ntohs(addr.sin_port);
return true;
}
boolBaseSock::isConnected()
{
return(m_sock!=-1)&&m_bConnected;
}
bool BaseSock::isUDP()
{
return m_bUDP;
}
long BaseSock::SendTo(constchar* buf,int len,const struct sockaddr_in* toaddr,int tolen)
{
if(m_sock==-1)
{
return-1;
}
return sendto(m_sock,buf,len,0,(const structsockaddr*)toaddr,tolen);
}
long BaseSock::RecvFrom(char*buf,int len,struct sockaddr_in* fromaddr,int* fromlen)
{
if(m_sock==-1)
{
return-1;
}
#ifdef WIN32
return recvfrom(m_sock,buf,len,0,(structsockaddr*)fromaddr,fromlen);
#else
return recvfrom(m_sock,buf,len,0,(structsockaddr*)fromaddr,(socklen_t*)fromlen);
#endif
}
bool BaseSock::Bind(unsignedshort nPort)
{
if(m_sock==-1)
{
return false;
}
struct sockaddr_in sin;
sin.sin_family=AF_INET;
#ifdef WIN32
sin.sin_addr.S_un.S_addr=0;
#else
sin.sin_addr.s_addr=0;
#endif
memset(sin.sin_zero,0,8);
sin.sin_port=htons(nPort);
if(bind(m_sock,(sockaddr*)&sin,sizeof(sockaddr_in))!=0)
return false;
listen(m_sock,1024);
m_bConnected = true;
return true;
}
boolBaseSock::Accept(BaseSock&client)
{
if(m_sock==-1)
{
return false;
}
client.m_sock = accept(m_sock,NULL,NULL);
client.m_bConnected = true;
return (client.m_sock != -1);
}
分享:
分享到新浪Qing
0
喜欢
阅读(606)┊评论 (0)┊收藏(0) ┊转载(0) ┊喜欢▼ ┊打印┊举报
前一篇:跨平台C++ Socket类 sock.h
后一篇:跨平台C++ Http Get和Post 函数,http.h