CTcp基本网络通讯封装类


// Tcp.h: 基本网络通讯封装类
//
//

#ifndef __TCP_H__
#define __TCP_H__

#include <winsock.h>
#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 <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#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 <timeout) continue;
      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((UI

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值