|
楼主 |
发表于 2003-10-28 17:41:23
|
显示全部楼层
mysocket.cpp
[code:1]
/*File :mysocket.cpp*/
#include <string.h>
#include <fcnt1.h>
#include "mysocket.h"
MySocket::MySocket() //实现MySocket的构造函数
{
address_family=AF_INET;
socket_type=SOCK_STREAM;
protocol_family_IPPROTO_TCP;
port_number=MySOCKET_SEFAULT_PORT;
Mysocket=-1;
conn_socket=-1;
bytes_read=bytes_moved=0;
is_conn_socket=0;
is_bound=0;
socket_error=MySOCKET_NO_ERROR;
}
MySocket::MySocket(sa_family_t af, int st, int pf,int port, char *hostname)
{
Mysocket=-1;
conn_socket=-1;
bytes_read=bytes_moves=0;
is_connected=0;
is_bound=0;
socket_error=MySOCKET_NO_ERROR;
InitSocket(af,st,pf,port,hostname);
}
MySocket::MySocket(int st, int port, char*hostname)
{
Mysocket=-1;
conn_socket=-1;
bytes_read=bytes_moved=0;
is_conneted=0;
is_bound=0;
socket_error=MySOCKET_NO_ERROR;
Initsocket(st, port,hostname);
}
MySocket::~MySocket() //析构函数
{
close();
}
int MySocket::Socket() //产生套接字
{
Mysocket=socket(address_family, socket_type, protocol_family);
if(Mysocket<0)
{
socket_error=MySOCKET_INIT_ERROR;
return -1;
}
return Mysocket;
}
int MySocket::InitSocket(sa_family_t af,int st,int pf,int port,char *hostname) //初始化套接字
{
address_family=af;
socket_type=st;
protoco_family=pf;
port_number=port;
sin.sin_family=address-family;
if(hostname){
hostent *hostnm=gethostbyname(hostname);
if(hostnm==(struct hostent *) 0){
socket_error=MySOCKET_HOSTNAME_ERROR;
return -1;
}
else
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_port=htons(port_number);
if(Socket()<0){
socket_error=MySOCKET_INIT_ERROR;
return -1;
}
return Mysocket;
}
int MySocket::InitSocket(int st,int port,char *hostname)
{
address_family=AF_INET;
port_number=port;
if(st==SOCK_STREAM){
socket_type=SOCK_STREAM;
protocol_family=IPPROTO_TCP;
}
else if(st==SOCK_DGRAM){
socket_type=SOCK_DGRAM;
protocol_family=IPPROTO_UDP;
}
else{
socket_error=MySOCKET_SOCKETTYPE_ERROR;
return -1;
}
sin.sin_family=address_family;
if(hostname){
hostent *hostnm=gethostbyname(hostname);
if(hostnm==(struct hostent *)0){
socket_error=MySOCKET_HOSTNAME_ERROR;
return -1;
}
sin.sin_addr.s_addr=*((unsigned long *)hostnm ->h_addr);
}
else
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_port=htons(port_number);
if(Socket()<0){
socket_error=MySOCKET_INIT_ERROR;
return -1;
}
return Mysocket;
}
int MySocket::Bind() //实现套接字的绑定功能
{
int rv=bind(Mysocket,(struct sockaddr *)&sin, sizeof(sin));
if(rv>=0){
is_bound=1;
}
else{
socket_error=MySOCKET_BIND_ERROR;
is_bound=0;
}
return rv;
}
int MySocket::Connect() //实现套接字的连接操作
{
int rv=connect(Mysocket,(struct sockaddr *)&sin, sizeof(sin));
if(rv>=0){
is_connected=1;
}
else{
socket_error=MySOCKET_CONNECT_ERROR;
is_connected=0;
}
return rv;
}
int MySocket::ReadSelect(int s, int seconds, int useconds) //实现套接字的读操作的超时设定
{
struct timeval timeout;
fd_set fds;
FD_ZERO(&fds);
FD_SET(s,&fds);
timeout.tv_sec=seconds;
timeout.tv_usec=useconds;
return select(s+1, &fds, 0, 0, &timeout);
}
int MySocket::Recv(void *buf, int bytes, int flags) //实现套接字的读取操作
{
return Recv(Mysocket, buf, bytes, flags);
}
int MySocket::Recv(void *buf, int bytes, int seconds, int useconds,int flags)
{
return Recv(Mysocket, buf, bytes, seconds, useconds, flags);
}
int MySocket::Send(const void *buf, int bytes, int flags) //实现套接字的发送操作
{
return Send(Mysocket, buf, bytes, flags);
}
int MySocket::Recv(int s, void *buf, int bytes, int flags) //实现套接字接受操作,用于接受指定数目的字符
{
bytes_read=0;
int num_read=0;
int num_req =(int)bytes;
char *p=(char *)buf;
while(bytes_read<bytes){
if((num_read=recv(s,p,num_req-bytes_read,flags))>0){
bytes_read+=num_read;
p+=num_read;
}
if(num_read<0){
socket_error=MySOCKET_RECEIVE_ERROR;
return -1;
}
}
return bytes_read;
}
int MySocket::Recv(int s, void *buf, int bytes, int seconds, int useconds, int flags)
{
bytes_read=0;
int num_read=0;
int num_req =(int)bytes;
char *p=(char *)buf;
while(bytes_read<bytes){
if(!ReadSelect(s,seconds,useconds)){
socket_error=MySOCKET_REQUEST_TIMEOUT;
return -1;
}
if((num_read=recv(s,p,num_req-bytes_read,flags))>0){
bytes_read+=num_read;
p+=num_read;
}
if(num_read<0){
socket_error=MySOCKET_RECEIVE_ERROR;
return -1;
}
}
return bytes_read;
}
int MySocket::Send(int s, const void *buf,int bytes,int flags) //发送指定数目的字符
{
bytes_moved=0;
int num_moved=0;
int num_req=(int)bytes;
char *p=(char *)buf;
while(bytes_moved<bytes){
if(num_moved=send(s,p,num_req-bytes_moved,flags))>0){
bytes_moved+=num_moved;
p+=num_moved;
}
if(num_moved<0){
socket_error=MySOCKET_TRANSMIT_ERROR;
return -1;
}
}
return bytes_moved;
}
int MySocket::RemoteRecv(void *buf,int bytes,int seconds,int useconds,int flags) //接收指定数目的字符,用于客户端操作
{
return Recv(conn_socket, buf, bytes, seconds, useconds, flags);
}
int MySocket::RemoteRecv(void *buf,int bytes,int flags)
{
return Recv(conn_socket, buf, bytes, flags);
}
int MySocket::RemoteSend(const void *buf,int bytes,int flags) //客户端数据发送
{
return Send(conn_socket, buf, bytes, flags);
}
void MySocket::ShutDown(int how) //实现套接字关闭操作
{
bytes_moved=0;
bytes_read=0;
is_connected=0;
is_bound=0;
if(Mysocket!=-1) shutdown(Mysocket,how);
if(conn_socket !=-1) shutdown(conn_socket,how);
Mysocket=-1;
conn_socket=-1;
}
void MySocket::ShutDown(int &s, int how)
{
if(s!=-1) shutdown(s,how);
s=-1;
}
void MySocket::ShutDownSocket(int how)
{
if(Mysocket != -1) shutdown(Mysocket, how);
Mysocket=-1;
}
void MySocket::ShutDownRemoteSocket(int how)
{
if(conn_socket != -1) shutdown(conn_socket, how);
conn_socket=-1;
}
void MySocket::Close()
{
bytes_moved=0;
bytes_read=0;
is_connected=0;
is_bound=0;
if(Mysocket !=-1) close(Mysocket);
if(conn_socket !=-1) close(conn_socket);
}
void MySocket::Close(int &s)
{
if(s!=-1) close(s);
s=-1;
}
void MySocket::closeSocket()//Close the server side socket
{
if(Mysocket!=-1) close(Mysocket);
Mysocket=-1;
}
void MySocket::CloseRemoteSocket()
{
if(conn_socket!=-1) close(conn_socket);
conn_socket=-1;
}
int MySocket::Listen(int max_connections) //实现套接字侦听操作
{
int rv=listen(Mysocket,
max_connections);
if(rv<0) socket_error=MySOCKET_LISTEN_ERROR;
return rv;
}
int MySocket::Accept() //实现套接字接收连接操作
{
int addr_size=(int)sizeof(remote_sin);
conn_socket=accept(Mysocket,(struct sockaddr *)&remote_sin,&addr_size);
if(conn_socket<0)
{
socket_error=MySOCKET_ACCEPT_ERROR;
return -1;
}
return conn_socket;
}
int MySocket::GetSockName(int s, sockaddr_in *sa) //获得套接字的名字
{
int namelen=(int)sizeof(sockaddr_in);
int rv=getsockname(s,(struct sockaddr *)sa, &namelen);
if(rv<0) socket_error=MySOCKET_SOCKET_ERROR;
return rv;
}
int MySocket::GetSockName()
{
return GetSockName(Mysocket, &sin);
}
int MySocket::GetPeerName(int s, sockaddr_in *sa) //获得远程套接字的名字
{
int namelen=(int)sizeof(sockaddr_in);
int rv=getpeername(s, (struct sockaddr *)&sa, &namelen);
if(rv<0) socket_error=MySOCKET_PEERNAME_ERROR;
return rv;
}
int MySocket::GetPeerName()
{
return GetPeerName(conn_socket, &remote_sin);
}
int MySocket::GetSockOpt(int s, int level, int optName, void *optVal, unsigned *optLen) //获得套接字选项
{
int rv=getsockopt(s, level, optName, optVal, (int *)optLen);
if(rv<0) socket_error=MySOCKET_SETOPTION_ERROR;
return rv;
}
int MySocket::GetSockOpt(int level, int optName, void *optVal, unsigned *optLen)
{
return GetSockOpt(Mysocket, level, optName, optVal, optLen);
}
int MySocket::SetSockOpt(int s, int level, int optName, const void *optVal, unsigned *optLen) //设置套接字选项
{
int rv=setsockopt(s, level, optName, optVal, (int)optLen);
if(rv<0) socket_error=MySOCKET_SETOPTION_ERROR;
return rv;
}
int MySocket::SetSockOpt(int level, int optName, const void *optVal, unsigned optLen)
{
return SetSockOpt(Mysocket, level, optName, optVal, optLen);
}
servent *mySocket::GetServiceInformation(char *name, char *protocol) //获得服务信息
{
server *sp=getservbyname(name,protocol);
if(sp==0) return 0;
servent *buf=new servent;
if(!buf) return 0;
memmove(buf, sp,sizeof(servent));
return buf;
}
servent *MySocket::GetServiceInformation(int port,char *protocol)
{
servent *sp=getservbyport(port,protocol);
if(sp==0) return 0;
servent *buf=new servent;
if(!buf) return 0;
memmove(buf, sp, sizeof(servent));
return buf;
}
int MySocket::GetServByName(char *name, char *protocol) //通过名字获得服务信息
{
servent *sp=getservbyname(name,protocol);
if(sp==0){
socket_error=MySOCKET_PROTOCOL_ERROR;
return -1;
}
sin.sin_port=sp->s_port;
return 0;
}
int MySocket::GetServByPort(int port, char *protocol) //通过端口号获得服务信息
{
servent *sp=getservbyport(port, protocol);
if(sp==0){
socket_error=MySOCKET_PROTOCOL_ERROR;
return 0;
}
sin.sin_port=sp->s_port;
return 0;
}
int MySocket::GetPortNumber() //获得本地端口号
{
return ntohs(sin.sin_port);
}
int MySocket::GetRemotePortNumber() //获得远程端口号
{
return ntohs(remote_sin.sin_port);
}
sa_family_t MySocket::GetAddressFamily() //获得本地地址簇
{
return sin.sin_family;
}
sa_family_t MySocket::GetRemoteAddressFamily() //获得远程地址簇
{
return remote_sin.sin_family;
}
hostent *MySocket::GetHostInformation(char *hostname) //通过主机名获得主机信息
{
in_addr hostia;
hostent *hostinfo;
hostia.s_addr=inet_addr(hostname);
if(hostia.s_addr==NULL){
hostinfo=gethostbyname(hostname);
}
else{
hostinfo=gethostbyaddr((const char *)&hostia,
sizeof(in_addr),AF_INET);
}
if(hostinfo==(hostent *)0){
return 0;
}
hostent *buf=new hostent;
if(!buf) return 0;
memmove (buf, hostinfo, sizeof(hostent));
return buf;
}
int MySocket::GetHostName(char *sbuf) //获得主机名
{
if(!sbuf) sbuf=new char[MysMAX_NAME_LEN];
int rv=gethostname(sbuf, MysMAX_NAME_LEN);
if(rv<0) socket_error=MySOCKET_HOSTNAME_ERROR;
return rv;
}
int MySocket::GetIPAddress(char *sbuf) //获得IP地址
{
char hostname(MysMAX_NAME_LEN);
int rv=GetHostName(hostname);
if(rv<0) return rv;
in_addr *ialist;
hostent *hostinfo=GetHostInformation(hostname);
if(!hostinfo){
socket_error=MySOCKET_HOSTNAME_ERROR;
return -1;
}
ialist=(in_addr *)hostinfo->h_addr_list[0];
if(!sbuf)sbuf=new char[MysMAX_NAME_LEN];
strcpy(sbuf,inet_ntoa(* ialist));
delete hostinfo;
return 0;
}
int MySocket::GetDomainName(char *sbuf) //获得域名
{
char hostname[MysMAX_NAME_LEN];
int rv=GetHostName(hostname);
if(rv<0) return rv;
hostent *hostinfo=GetHostInformation(hostname);
if(!hostinfo){
socket_error=MySOCKET_HOSTNAME_ERROR;
return -1;
}
if(!sbuf) sbuf=new char[MysMAX_NAME_LEN];
strcpy(sbuf,hostinfo->h_name);
int i; int len=strlen(sbuf);
for(i=0;i<len;i++){
if(sbuf[1])=='.') break;
}
if(++i<len){
len-=i;
memmove(sbuf,sbuf+i,len);
sbuf[len]=0;
}
delete hostinfo;
return 0;
}
int MySocket::GetBoundIPAddress(char *sbuf) //获得绑定的IP地址
{
char *s=inet_ntoa(sin.sin_addr);
if(s==0){
socket_error=MySOCKET_HOSTNAME_ERROR;
return -1;
}
if(!sbuf) sbuf=new char[MysMAX_NAME_LEN];
strcpy(sbuf, s);
return 0;
}
int MySocket::GetRemoteHostName(char *sbuf) //获得远程主机名
{
char *s=inet_ntoa(remote_sin.sin_addr);
if(s==0){
socket_error=MySOCKET_HOSTNAME_ERROR;
return -1;
}
if(!sbuf) sbuf=new char[MysMAX_NAME_LEN];
strcpy(sbuf, s);
return 0;
}
void MySocket::GetClientInfo(char *client_name,int &r_port) //获得客户信息
{
int rv=GetRemoteHostName(client_name);
if(rv<0){
char *unc="UNKNOWN";
for(unsigned i=0;i<MysMAX_NAME_LEN;i++) client_name[i]='\0';
strcpy(client_name,unc);
}
r_port=GetRemotePortNumber();
}
int MySocket::RecvFrom(void *buf, int bytes, int flags) //数据报接收操作
{
return RecvFrom(Mysocket, &remote_sin, buf, bytes, flags);
}
int MySocket::SendTo(void *buf, int bytes, int flags) //数据报发送操作
{
return SendTo(Mysocket, &sin, buf, bytes, flags);
}
int MySocket::RecvFrom(int s, sockaddr_in *sa, void *buf,
int bytes, int seconds, int useconds, int flags) //数据报接收操作并具有超时返回功能
{
int addr_size=(int)sizeof(sockaddr_in);
bytes_read=0;
int num_read=0;
int num_req=(int)bytes;
char *p=(char *)buf;
while(bytes_read<bytes){
if(!ReadSelect(s, seconds, useconds)){
socket_error=MySOCKET_REQUEST_TIMEOUT;
return -1;
}
if((num_read=recvfrom( s, p, num_req-bytes_read, flags, (struct sockaddr *)sa, &addr_size))>0){
bytes_read+=num_read;
p+=num_read;
}
if(num_read<0){
socket_error=MySOCKET_RECEIVE_ERROR;
return -1;
}
}
return bytes_read;
}
int MySocket::RecvFrom(int s, sockaddr_in *sa, void *buf, int bytes, int flags) //接收指定数目的字符
{
int addr_size=(int)sizeof(sockaddr_in);
bytes_read=0;
int num_read=0;
int num_req=(int)bytes;
char *p=(char *)buf;
while(bytes_read<bytes){
if((num_read=recvfrom( s, p, num_req-bytes_read, flags, (struct sockaddr *)sa, &addr_size))>0){
bytes_read+=num_read;
p+=num_read;
}
if(num_read<0){
socket_error=MySOCKET_RECEIVE_ERROR;
return -1;
}
}
return bytes_read;
}
int MySocket::SendTo(int s, sockaddr_in *sa, void *buf, int bytes, int flags) //发送指定数目的字符
{
int addr_size=(int)sizeof(sockaddr_in);
bytes_moved=0;
int num_moved=0;
int num_req=(int)bytes;
char *p=(char *)buf;
while(bytes_moves<bytes){
if((num_moved=sendto( s, p, num_req-bytes_read, flags, (const struct sockaddr *)sa, addr_size))>0){
bytes_moved+=num_moved;
p+=num_moved;
}
if(num_moved<0){
socket_error=MySOCKET_TRANSMIT_ERROR;
return -1;
}
}
return bytes_moved;
}[/code:1] |
|