socket封装类-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 132774
  • 博文数量: 48
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 38
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-22 17:03
文章分类
文章存档

2016年(1)

2015年(6)

2014年(12)

2013年(27)

2012年(2)

我的朋友
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: c/c

2016-03-11 14:20:05

原文地址: 作者:

// tcp.h: 基本网络通讯封装类
//
//////////////////////////////////////////////////////////////////////
#ifndef __tcp_h__
#define __tcp_h__
#include
#pragma comment(lib, "wsock32.lib")  // search for wsock32 lib at compile time
#define wm_tcp  wm_app 100
class ctcp 
{
public:
 ctcp();
 ~ctcp();

 //extern int sd_connect, sd_bind, sd_accept;
 int wsa_ok ;
 static void peek_message(void);
 static int get_local_ip(char *ip);
 
 int init();
 int exit();
 int bind_listen(int port,sockaddr_in& addr,long &err);
 
 int status(int  sd, char *type, int timeout=5);
 int bind(hwnd hwnd, int port);
 int bind2(hwnd hwnd, char *ip, int port);
 int accept(int sd, int timeout);
 int connect(char *hostname, int port, int timeout, int f_noblock);
 int connect2(char *bind_ip, char *hostname, int port, int timeout, int f_noblock);
 void disconnect(int sd);
 void close(int& sd);
 
 int send(int sd, char *buf, int len, int timeout=10);
 int recv(int sd, char *buf, int len, int timeout=10);
 int gethostnamebyip(char *ip, char *name);
 
 unsigned short htons(unsigned short);
 unsigned short ntohs(unsigned short);
 unsigned long int htonl(unsigned long int);
 unsigned long int ntohl(unsigned long int);
 unsigned __int64 ntohh(unsigned __int64);
 unsigned __int64 htonh(unsigned __int64);
 
 float htonf(float f);
 float ntohf(float f);
 double htond(double d);
 double ntohd(double d);
 
 char *get_remote_ip(int sd, char *ip);
 
};
#endif
------------------------------------------------------
// tcp.cpp: implementation of the ctcp class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include
#include
#include
#include
#include "tcp.h"
extern writelog(char *file_name, char *format, ...);
extern writestat(char *format, ...);
 
//////////////////////////////////////////////////////////////////////
// construction/destruction
//////////////////////////////////////////////////////////////////////
ctcp::ctcp()
{
 wsa_ok =0;
}
ctcp::~ctcp()
{
}
 
 
int ctcp::init()
{
 if(!wsa_ok)
 {
  wsadata wsa;
  //sd_bind =sd_connect =sd_accept =-1;
  if(wsastartup(makeword(1, 1), &wsa) !=0)
   return -1;
  wsa_ok =1;
 }
 return 0;
}
int ctcp::exit()
{
  if(wsa_ok) wsacleanup();
  return 0;
}
int ctcp::status(int  sd, char *type, int timeout)
{
  fd_set rset, wset, eset;
  fd_set far *prset =null, *pwset =null, *peset =null;
  struct timeval tval;
  int i, status, err_no =0;
  time_t t1, t2;
  msg msg;
  tval.tv_sec =0;
  tval.tv_usec =1;
  time(&t1);
  t2 =t1;
  while(t2-t1 < timeout)
  {
    fd_zero(&rset);
    fd_zero(&wset);
    fd_zero(&eset);
    for(i =0; i<(int)strlen(type); i )
    {
      if(type[i] =='r') { fd_set(sd, &rset); prset =&rset; }
      if(type[i] =='w') { fd_set(sd, &wset); pwset =&wset; }
      if(type[i] =='e') { fd_set(sd, &eset); peset =&eset; }
    }
    status =select(-1, prset, pwset, peset, &tval);
 err_no =wsagetlasterror();
 int err=getlasterror();
 //writestat("select err_no=%d, err=%d", err_no, err);
    time(&t2);
 if(status ==0)
    {
      if(peekmessage(&msg, 0, null, null, pm_remove))
      {
        translatemessage(&msg);
        dispatchmessage(&msg);
        if(msg.message ==wm_quit)
  {
   //wsasetlasterror(wsaetimeout);
   return -1;
  }
      }
      if(t2-t1       else
      {
        if(prset) fd_clr((uint)sd,&rset);
        if(pwset) fd_clr((uint)sd,&wset);
        if(peset) fd_clr((uint)sd,&eset);
        wsasetlasterror(wsaewouldblock);
        return -10;
      }
    }
    if(peset && fd_isset(sd, peset))
    {
      if(prset !=null) fd_clr((uint)sd,&rset);
      if(pwset !=null) fd_clr((uint)sd,&wset);
      if(peset !=null) fd_clr((uint)sd,&eset);
      //err_no =wsagetlasterror();
      /*
      len =sizeof(errno);
      getsockopt(sd, sol_socket, so_error, (char *)&errno, &len);
      */
   wsasetlasterror(err_no);
      return -1;
    }
    if((prset && fd_isset(sd, prset)) || (pwset && fd_isset(sd, pwset)))
    {
      //err_no =wsagetlasterror();
      /*
      len =sizeof(errno);
      getsockopt(sd, sol_socket, so_error, (char *)&errno, &len);
      */
    }
    if(prset !=null) fd_clr((uint)sd,&rset);
    if(pwset !=null) fd_clr((uint)sd,&wset);
    if(peset !=null) fd_clr((uint)sd,&eset);
    //if(status <0)
      //err_no =wsagetlasterror();
 wsasetlasterror(err_no);
    if(err_no ==wsaeintr)
 {
  return wsaeintr;
 }
    if(err_no)
    {
      return -1;
    }
    else break;
  }
  return 0;
}
int ctcp::bind(hwnd hwnd, int port)
{
 struct sockaddr_in addr;
    char temp[200];
    int sd;
    sd =-1;
 if((sd =socket(af_inet, sock_stream, ipproto_tcp)) <0)
 {
  sprintf(temp, "socket failed! errno:%d", wsagetlasterror());
  return -1;
 }
 memset(&addr, 0, sizeof(addr));
 addr.sin_family =af_inet;
 addr.sin_port =htons((unsigned short)port);
    int l =1;
    setsockopt(sd, sol_socket, so_reuseaddr, (char *)&l, sizeof(l));
    /*setsockopt(sd, sol_socket, so_reuseport, (char *)&l, sizeof(l));*/
 if(::bind(sd, (struct sockaddr *)&addr, sizeof(addr)) <0)
 {
      sprintf(temp, "bind failed! errno:%d", wsagetlasterror());
   closesocket(sd);
      return -1;
 }
 if(hwnd && wsaasyncselect(sd, hwnd, wm_tcp, fd_accept) !=0)
    {
      sprintf(temp, "tcp_bind:wsaasyncselect failed!");
      closesocket(sd);
      return -2;
    }
    //sd_bind =sd;
    listen(sd, 5);
    return sd;
}
int ctcp::bind2(hwnd hwnd, char *ip, int port)
{
 struct sockaddr_in addr;
    char temp[200];
    int sd;
    sd =-1;
 if((sd =socket(af_inet, sock_stream, ipproto_tcp)) <0)
 {
  sprintf(temp, "socket failed! errno:%d", wsagetlasterror());
  return -1;
 }
 if(ip && *ip)
 {
  memset(&addr, 0, sizeof(addr));
  ulong ul =inet_addr(ip);
  if(ul ==0xffffffff)
  {
   closesocket(sd);
   return -1;
  }
  else addr.sin_addr.s_addr=ul;
 }
 
 memset(&addr, 0, sizeof(addr));
 addr.sin_family =af_inet;
 addr.sin_port =htons((unsigned short)port);
    int l =1;
    setsockopt(sd, sol_socket, so_reuseaddr, (char *)&l, sizeof(l));
    /*setsockopt(sd, sol_socket, so_reuseport, (char *)&l, sizeof(l));*/
 if(::bind(sd, (struct sockaddr *)&addr, sizeof(addr)) <0)
 {
      sprintf(temp, "bind failed! errno:%d", wsagetlasterror());
   closesocket(sd);
      return -1;
 }
 if(hwnd && wsaasyncselect(sd, hwnd, wm_tcp, fd_accept) !=0)
    {
      sprintf(temp, "tcp_bind:wsaasyncselect failed!");
      closesocket(sd);
      return -2;
    }
    //sd_bind =sd;
    listen(sd, 5);
    return sd;
}
int ctcp::accept(int sd, int timeout)
{
  int sd_acc =-1;
  struct sockaddr_in sa;
  int len;
  /*unsigned long l;*/
  if(status(sd, "rw", timeout) <0)
    return -1;
  len =sizeof(sa);
  if((sd_acc =::accept(sd, (struct sockaddr *)&sa, &len)) <0)
    return -1;
  //sd_accept =sd_acc;
  /*  l =1;
    if(ioctlsocket(sd_acc, fionbio, &l) <0)
    {
      closesocket(sd);
      return -1;
    }*/
  return sd_acc;
}
int ctcp::connect(char *hostname, int port, int timeout, int f_noblock)
{
    struct hostent *hp;
    struct sockaddr_in addr;
    char temp[200];
    unsigned long ul;
    long l;
    int sd =-1, ret;
    time_t t1, t2;
    sd =-1;
 if((sd =::socket(af_inet, sock_stream, ipproto_tcp)) <0)
 {
  sprintf(temp, "socket failed! errno:%d", wsagetlasterror());
  return -1;
 }
 memset(&addr, 0, sizeof(addr));
    ul =inet_addr(hostname);
    if(ul ==0xffffffff)
    {
   if((hp =gethostbyname(hostname)) ==null)
   {
        sprintf(temp, "gethostbyname and inet_addr failed! errno:%d", wsagetlasterror());
     closesocket(sd);
        return -1;
      }
   memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
 }
    else  addr.sin_addr.s_addr=ul;
 addr.sin_family =af_inet;
 addr.sin_port =htons((unsigned short)port);
    l =1;
    if(/*f_noblock && */ioctlsocket(sd, fionbio, (unsigned long *)&l) <0)
    {
      closesocket(sd);
      return -1;
    }
    /*if(setsockopt(sd, ipproto_tcp, tcp_nodelay, (char *)&l, sizeof(l)) <0)
    {
      closesocket(sd);
      return -1;
    }*/
    time(&t1);
 int counter=0;
    while((ret =::connect(sd, (struct sockaddr *)&addr, sizeof(addr))) !=0)
    {
  
      time(&t2);
      if((t2 -t1) > timeout)
      {
        closesocket(sd);
        return -1;
      }
      peek_message();
      int err_no =wsagetlasterror();
   if(ret ==socket_error)
   {
    if(counter >10)
    {
     sleep(100);
     counter=0;
    }
    if(err_no ==wsaeisconn) break;
   }
   else if(err_no ==wsaewouldblock /*|| err_no ==wsaeinprogress*/ || err_no ==wsaealready)
   {
          continue;
   }
          else
   {
  closesocket(sd);
     return -1;
   }
 }
    if(status(sd, "we", timeout) <0)
    {
      sprintf(temp, "status:ᬽó·þîñæ÷꧰ü!\n¼ì²é·þîñæ÷¶ë³ìðòêç·ñôëðð\nçòö÷»úµøö·êç·ñ%s, ¶ë¿úêç·ñ%d\n"
                    "errno=%d",
            hostname, port, wsagetlasterror());
      closesocket(sd);
      return -1;
    }
    //sd_connect =sd;
    return sd;
}
int ctcp::connect2(char *bind_ip, char *hostname, int port, int timeout, int f_noblock)
{
    struct hostent *hp;
    struct sockaddr_in addr;
    char temp[200];
    unsigned long ul;
    long l;
    int sd =-1, ret;
    time_t t1, t2;
    sd =-1;
 if((sd =socket(af_inet, sock_stream, ipproto_tcp)) <0)
 {
  sprintf(temp, "socket failed! errno:%d", wsagetlasterror());
  return -1;
 }
 if(bind_ip && *bind_ip)
 {
  memset(&addr, 0, sizeof(addr));
  addr.sin_family =af_inet;
  ul =inet_addr(bind_ip);
  if(ul ==0xffffffff)
  {
   closesocket(sd);
   return -1;
  }
  else addr.sin_addr.s_addr=ul;
  if(::bind(sd, (struct sockaddr *)&addr, sizeof(addr)) <0)
  {
    sprintf(temp, "tcp_connect: bind failed! errno:%d", wsagetlasterror());
    closesocket(sd);
    return -1;
  }
 }
 memset(&addr, 0, sizeof(addr));
    ul =inet_addr(hostname);
    if(ul ==0xffffffff)
    {
   if((hp =gethostbyname(hostname)) ==null)
   {
        sprintf(temp, "gethostbyname and inet_addr failed! errno:%d", wsagetlasterror());
     closesocket(sd);
        return -1;
      }
   memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
 }
    else  addr.sin_addr.s_addr=ul;
 addr.sin_family =af_inet;
 addr.sin_port =htons((unsigned short)port);
    l =1;
    if(/*f_noblock && */ioctlsocket(sd, fionbio, (unsigned long *)&l) <0)
    {
      closesocket(sd);
      return -1;
    }
    /*if(setsockopt(sd, ipproto_tcp, tcp_nodelay, (char *)&l, sizeof(l)) <0)
    {
      closesocket(sd);
      return -1;
    }*/
    time(&t1);
 int counter=0;
    while((ret =::connect(sd, (struct sockaddr *)&addr, sizeof(addr))) !=0)
    {
      time(&t2);
      if((t2 -t1) > timeout)
      {
        closesocket(sd);
        return -1;
      }
      peek_message();
      int err_no =wsagetlasterror();
   if(ret ==socket_error)
   {
    if(counter >10)
    {
     sleep(100);
     counter=0;
    }
    if(err_no ==wsaeisconn) break;
   }
   else if(err_no ==wsaewouldblock /*|| err_no ==wsaeinprogress*/ || err_no ==wsaealready)
   {
             continue;
   }
          else
   {
  closesocket(sd);
     return -1;
   }
 }
    if(status(sd, "we", timeout) <0)
    {
      sprintf(temp, "status:ᬽó·þîñæ÷꧰ü!\n¼ì²é·þîñæ÷¶ë³ìðòêç·ñôëðð\nçòö÷»úµøö·êç·ñ%s, ¶ë¿úêç·ñ%d\n"
                    "errno=%d",
            hostname, port, wsagetlasterror());
      closesocket(sd);
      return -1;
    }
    //sd_connect =sd;
    return sd;
}
 
void ctcp::disconnect(int sd)
{
  if(sd >0)
  {
    closesocket(sd);
    //if(sd ==sd_connect) sd_connect =-1;
    //if(sd ==sd_accept) sd_accept =-1;
    //if(sd ==sd_bind) sd_bind =-1;
  }
}
int ctcp::send(int sd, char *buf, int len, int timeout)
{
  int len1, len_send =0;
  time_t t1, t2;
  len_send =0;
  time(&t1);
  t2 =t1;
 int counter=0;
  while(len_send   {
    if(t2-t1 >timeout)
      return len_send;
    if(status(sd, "w", timeout-(t2-t1)) <0)
 {
      return len_send;
 }
    if((len1 =::send(sd, &buf[len_send], len-len_send, 0)) <=0)
    {
      if(len1 ==socket_error && getlasterror() ==wsaewouldblock)
      {
        time(&t2);
 
  if(counter >100)
    {
     sleep(100);
     counter=0;
    }
        continue;
      }
      return len_send;
    }
    len_send =len1;
    time(&t2);
  }
  return len_send;
}
int ctcp::recv(int sd, char *buf, int len, int timeout)
{
  int len1, len_recv;
  time_t t2, t1;
  len_recv =0;
  time(&t1);
  t2 =t1;
  int err =0;
  int counter=0;
  while(len_recv   {
    if(t2 -t1 >timeout)
 {
  wsasetlasterror(err);
  return len_recv;
 }
    if(status(sd, "r", timeout-(t2-t1)) <0)
    {
  err =wsagetlasterror();
  //writestat("tcp_status err=%d", wsagetlasterror());
  wsasetlasterror(err);
  return len_recv;
 }
 if((len1 =::recv(sd, &buf[len_recv], len-len_recv, 0)) <=0)
     {
  err =wsagetlasterror();
  //writestat("recv err=%d, len1=%d", err, len1);
       if(timeout ==0) break;
       if(len1 ==socket_error && err ==wsaewouldblock)
       {
         time(&t2);
  if(counter >100)
    {
     sleep(100);
     counter=0;
    }
  continue;
       }
    wsasetlasterror(err);
       return len_recv;
    }
    len_recv =len1;
    time(&t2);
  }
  wsasetlasterror(err);
  return len_recv;
}
int ctcp::gethostnamebyip(char *ip, char *host)
{
  struct hostent *hp;
  struct in_addr ul;
  host[0] =0;
  ul.s_un.s_addr =inet_addr(ip);
  if(ul.s_un.s_addr ==0xffffffff) return -1; // ip error or ip is hostname
  hp =gethostbyaddr((char *)&ul, 4, af_inet);
  if(hp ==null) return -1;  // can not get hostname
  strcpy(host, hp->h_name);
  return 0;
}
int ctcp::get_local_ip(char *ip)
{
  struct hostent *hp;
  char host[50], *p;
  if(gethostname(host, sizeof(host)) <0) return -1;
  hp =gethostbyname(host);
  if(hp ==null) return -1;
  p =(char *)hp->h_addr;
  wsprintfa(ip, "%d.%d.%d.%d", (int)p[0]&0xff, (int)p[1]&0xff, (int)p[2]&0xff, (int)p[3]&0xff);
  return 0;
}
char* ctcp::get_remote_ip(int sd, char *ip)
{
  struct sockaddr_in addr_in;
  int len =sizeof(addr_in);
  char *p1;
  if(sd <0) return  null;
  if(getpeername(sd, (struct sockaddr *)&addr_in, &len) <0)
    return null;
  p1 =(char *)&addr_in.sin_addr;
  sprintf(ip, "%d.%d.%d.%d", ((int)p1[0]) &0xff, ((int)p1[1]) &0xff, (int)p1[2] &0xff, (int)p1[3]&0xff);
 
  return ip;
}
unsigned short ctcp::htons(unsigned short s)
{
 return ::htons(s);
}
unsigned short ctcp::ntohs(unsigned short s)
{
 return ::ntohs(s);
}
unsigned long int ctcp::htonl(unsigned long int l)
{
 return ::htonl(l);
}
unsigned long int ctcp::ntohl(unsigned long int l)
{
 return ::ntohl(l);
}
float ctcp::htonf(float f)
{
  unsigned char *p, p0, p1;
  if(htons(1) ==1) return f;
  p =(unsigned char *)&f;
  p0 =p[0];
  p1 =p[1];
  p[0] =p[3];
  p[3] =p0;
  p[1] =p[2];
  p[2] =p1;
  return f;
}
float ctcp::ntohf(float f)
{
  unsigned char *p, p0, p1;
  if(ntohs(1) ==1) return f;
  p =(unsigned char *)&f;
  p0 =p[0];
  p1 =p[1];
  p[0] =p[3];
  p[3] =p0;
  p[1] =p[2];
  p[2] =p1;
  return f;
}
double ctcp::htond(double d)
{
  unsigned char *p, p0, p1, p2, p3;
  if(htons(1) ==1) return d;
  p =(unsigned char *)&d;
  p0 =p[0];
  p1 =p[1];
  p2 =p[2];
  p3 =p[3];
  p[0] =p[7];
  p[7] =p0;
  p[1] =p[6];
  p[6] =p1;
  p[2] =p[5];
  p[5] =p2;
  p[3] =p[4];
  p[4] =p3;
  return d;
}
double ctcp::ntohd(double d)
{
  unsigned char *p, p0, p1, p2, p3;
  if(ntohs(1) ==1) return d;
  p =(unsigned char *)&d;
  p0 =p[0];
  p1 =p[1];
  p2 =p[2];
  p3 =p[3];
  p[0] =p[7];
  p[7] =p0;
  p[1] =p[6];
  p[6] =p1;
  p[2] =p[5];
  p[5] =p2;
  p[3] =p[4];
  p[4] =p3;
  return d;
}
unsigned __int64 ctcp::htonh(unsigned __int64 d)
{
  unsigned char *p, p0, p1, p2, p3;
  if(htons(1) ==1) return d;
  p =(unsigned char *)&d;
  p0 =p[0];
  p1 =p[1];
  p2 =p[2];
  p3 =p[3];
  p[0] =p[7];
  p[7] =p0;
  p[1] =p[6];
  p[6] =p1;
  p[2] =p[5];
  p[5] =p2;
  p[3] =p[4];
  p[4] =p3;
  return d;
}
unsigned __int64 ctcp::ntohh(unsigned __int64 d)
{
  unsigned char *p, p0, p1, p2, p3;
  if(ntohs(1) ==1) return d;
  p =(unsigned char *)&d;
  p0 =p[0];
  p1 =p[1];
  p2 =p[2];
  p3 =p[3];
  p[0] =p[7];
  p[7] =p0;
  p[1] =p[6];
  p[6] =p1;
  p[2] =p[5];
  p[5] =p2;
  p[3] =p[4];
  p[4] =p3;
  return d;
}
void ctcp::peek_message(void)
{
  msg msg;
  if(peekmessage(&msg, null, 0, 0, pm_remove))
  {
    translatemessage(&msg);
    dispatchmessage(&msg);
  }
}
int ctcp::bind_listen(int port,sockaddr_in& addr,long &err)
{
 int sock;
 
 
 sock = socket(af_inet, sock_stream, 0);   // create socket
 
 addr.sin_family = af_inet;      // address family internet
 addr.sin_port = htons (port);     // assign port 'port' to this socket
 addr.sin_addr.s_addr = htonl (inaddr_any);  // no destination
 
 if (::bind (sock, (lpsockaddr) &addr, sizeof (addr)) == socket_error)
 {
  closesocket (sock);
  err = wsagetlasterror ();
  return -1;
 }
 
 if (::listen (sock, 10) == socket_error)
 {
  closesocket (sock);
  err = wsagetlasterror ();
  return -1;
 }
 return sock;
}
void ctcp::close(int &sd)
{
 shutdown(sd,0);
 closesocket(sd);
 sd = invalid_socket;
}

 
阅读(1224) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图