TCPTransport.cpp
#include "StdAfx.h"
#include "TCPTransport.h"
#include "LogFile.h"
int g_port = 0;
CString g_IPadress;
extern CString g_logpath;
extern void ShowWindowLog(CString logContent);
int QryConnect(SOCKET& sockfd,struct sockaddr_in addrSrv,int overtime)
{
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) <= 0)
{
return 1;//create socket fd failed
}
int timeout = overtime*1000;
struct timeval select_timeout={0};
select_timeout.tv_sec = overtime; //lbs change timeout->overtime
if(setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout,sizeof(timeout)) != 0)
{
QryCloseSocket(sockfd);
return 2;
}
unsigned long flags;
flags=1;
if( ioctlsocket(sockfd,FIONBIO,&flags) != 0)
{
QryCloseSocket(sockfd);
return 3;
}
fd_set fs;
if(connect(sockfd,(struct sockaddr *)&addrSrv,sizeof(struct sockaddr_in)) < 0)
{
int ret = 0;
FD_ZERO(&fs);
FD_SET(sockfd,&fs);
ret = select(sockfd+1,NULL,&fs,NULL,&select_timeout);
if(ret == 0)//over time
{
QryCloseSocket(sockfd);
return 4;
}
else if(ret < 0)///error
{
QryCloseSocket(sockfd);
return 5;
}
}
return 0;
}
void InitSocket(SOCKET &sockClient)
{
struct sockaddr_in addrSrv;
memset(&addrSrv,0,sizeof(struct sockaddr_in));
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(g_port);
addrSrv.sin_addr.s_addr = inet_addr(g_IPadress.GetBuffer(g_IPadress.GetLength()));
sockClient = -1;
int codeConn = QryConnect(sockClient,addrSrv,5); //set timeout value
switch(codeConn)
{
case 1:
AfxMessageBox("ERROR CODE01_1");
return ;
break;
case 2:
AfxMessageBox("ERROR CODE02_1");
return ;
break;
case 3:
AfxMessageBox("ERROR CODE03_1");
return ;
break;
case 4:
//AfxMessageBox("OVER TIME !");
WriteLog(g_logpath, "Connect Server Err: Over Time!");
ShowWindowLog("Connect Server Err: Over Time!\n");
return ;
break;
case 5:
//AfxMessageBox("Link error ");
WriteLog(g_logpath, "Link Server Err!");
ShowWindowLog("Link Server Err!\n");
return ;
break;
default:
break;
}
}
int SendData(IN const char *inputData, IN int dataLen, OUT SOCKET &sockClient)
{
if(dataLen <= 0)
return 0;
InitSocket(sockClient);
int times = 0; //lbs change: reconnect 3->0 times
int sendLen = 0;
while((sendLen=send(sockClient,inputData,dataLen,0)) <= 0)
{
//lbs question: send returns value < len in noblock
QryCloseSocket(sockClient);
if(times <= 0)
{
//AfxMessageBox("link fail ");
WriteLog(g_logpath, "SendData Err!");
//ShowWindowLog("SendData Err!\n");
return 0;
}
InitSocket(sockClient);
times--;
continue;
}
return sendLen;
}
int RecvData(IN SOCKET sockClient, IN int bufLen, OUT char* recvBuf)
{
if(bufLen <= 0)
return 0;
//set block mode
unsigned long flags=0;
if(ioctlsocket(sockClient,FIONBIO,&flags) != 0)
{
//AfxMessageBox("ioctlsocket error1!");
//ShowWindowLog("RecvData Err: ioctlsocket error!\n");
WriteLog(g_logpath, "ioctlsocket error1!");
AfxMessageBox("Fail To Connect Server! Try Once more!");
return 0;
}
int ret = recv(sockClient,recvBuf,bufLen,0);
return ret;
}
bool IsSocketClosed(SOCKET sockfd)
{
bool ret = false;
HANDLE closeEvent = WSACreateEvent();
WSAEventSelect(sockfd, closeEvent, FD_CLOSE);
DWORD dwRet = WaitForSingleObject(closeEvent, 0);
if(dwRet == WSA_WAIT_EVENT_0)
ret = true;
else if(dwRet == WSA_WAIT_TIMEOUT)
ret = false;
WSACloseEvent(closeEvent);
return ret;
}
void QryCloseSocket(SOCKET& sockfd)
{
closesocket(sockfd);
sockfd = -1;
}
TCPTransport.h
#ifndef TCPTRANSPORT_H
#define TCPTRANSPORT_H
#include <Winsock2.h>
#include <afxinet.h>
int QryConnect(SOCKET& sockfd,struct sockaddr_in addrSrv,int overtime);
bool IsSocketClosed(SOCKET sockfd);
void QryCloseSocket(SOCKET& sockfd);
//lbs add
void InitSocket(SOCKET &sockClient);
int SendData(IN const char *inputData, IN int dataLen, OUT SOCKET &sockClient);
int RecvData(IN SOCKET sockClient, IN int bufLen, OUT char* recvBuf);
#endif
使用
//Send data
SOCKET sock;
if(SendData((char *)pEncodeBuf, encodeBufLen, sock) <= 0)
{
free(sentBuf);
free(pEncodeBuf);
QryCloseSocket(sock);
return false;
}
//RecvData
int decodeBufLen = 0;
int times = 0 ; //change 3 -> 0
char *pDecodeBuf = (char *)malloc(MAX_SIZE_RECV);
memset(pDecodeBuf, 0, MAX_SIZE_RECV);
while( (decodeBufLen = RecvData(sock,MAX_SIZE_RECV,pDecodeBuf))<=0 && times>0 )
{
QryCloseSocket(sock);
SendData((char *)pEncodeBuf, encodeBufLen, sock);
times--;
}
QryCloseSocket(sock);
pDecodeBuf[decodeBufLen] = '\0';
free(sentBuf);
free(recvBuf);