// 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