Add handshaking.
This commit is contained in:
@@ -7,12 +7,15 @@ const int WSA_MAJOR_VERSION = 1;
|
||||
const int WSA_MINOR_VERSION = 1;
|
||||
#define WSA_VERSION MAKEWORD(WSA_MAJOR_VERSION, WSA_MINOR_VERSION)
|
||||
#define SOCKADDR_LEN sizeof(SOCKADDR_IN)
|
||||
const char BufferHeader[4]={0x46,0x49,0x4E,0x53};
|
||||
const char HandShaking[]={0x46,0x49,0x4E,0x53,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
||||
|
||||
int CSo7_TCPIP::m_iReceiveMaxBufSize=1024;
|
||||
int CSo7_TCPIP::m_iSendMaxBufSize=1024;
|
||||
|
||||
SOCKET CSo7_TCPIP::m_Socket=INVALID_SOCKET;
|
||||
in_addr CSo7_TCPIP::m_SreverIPAddress;
|
||||
in_addr CSo7_TCPIP::m_ClientIPAddress;
|
||||
u_short CSo7_TCPIP::m_iServerPortNumber=static_cast<u_short>(0);
|
||||
|
||||
//================================================================
|
||||
@@ -85,6 +88,7 @@ TCPIP_RETURN_CODE CSo7_TCPIP::Connect(const HWND& _hWnd,const in_addr& _IPAddres
|
||||
|
||||
// 填写服务器地址信息
|
||||
sockaddr_in remote;
|
||||
m_SreverIPAddress = _IPAddress;
|
||||
remote.sin_addr = _IPAddress;
|
||||
remote.sin_family = AF_INET;
|
||||
remote.sin_port = htons(m_iServerPortNumber);
|
||||
@@ -95,6 +99,7 @@ TCPIP_RETURN_CODE CSo7_TCPIP::Connect(const HWND& _hWnd,const in_addr& _IPAddres
|
||||
errorCode = WSAGetLastError();
|
||||
rCode=(TCPIP_RETURN_CODE)errorCode;
|
||||
}
|
||||
GetHostIPAddr(m_ClientIPAddress);
|
||||
}
|
||||
return rCode;
|
||||
}
|
||||
@@ -112,41 +117,41 @@ TCPIP_RETURN_CODE CSo7_TCPIP::DisConnect()
|
||||
//================================================================
|
||||
TCPIP_RETURN_CODE CSo7_TCPIP::GetHostIPAddr(in_addr& _IPAddress)
|
||||
{
|
||||
if(m_Socket == INVALID_SOCKET)
|
||||
static int first = TRUE;
|
||||
if (first)
|
||||
{
|
||||
return TCPIP_INVAILD_SOCKET;
|
||||
if(m_Socket == INVALID_SOCKET)
|
||||
{
|
||||
return TCPIP_INVAILD_SOCKET;
|
||||
}
|
||||
char HostName[100];
|
||||
gethostname(HostName,sizeof(HostName));
|
||||
hostent *pAddr;
|
||||
pAddr=gethostbyname(HostName);
|
||||
memcpy(&m_ClientIPAddress,pAddr->h_addr_list[0],sizeof(in_addr));
|
||||
_IPAddress=m_ClientIPAddress;
|
||||
first = FALSE;
|
||||
}
|
||||
char HostName[100];
|
||||
gethostname(HostName,sizeof(HostName));
|
||||
hostent *pAddr;
|
||||
pAddr=gethostbyname(HostName);
|
||||
memcpy(&_IPAddress,pAddr->h_addr_list[0],sizeof(in_addr));
|
||||
_IPAddress=m_ClientIPAddress;
|
||||
return TCPIP_CONNECT_OK;
|
||||
}
|
||||
//================================================================
|
||||
TCPIP_RETURN_CODE CSo7_TCPIP::Send(int _Addr,int _Data)
|
||||
TCPIP_RETURN_CODE CSo7_TCPIP::Handshaking()
|
||||
{
|
||||
_Addr=0;
|
||||
_Data=0;
|
||||
if(m_Socket == INVALID_SOCKET)
|
||||
{
|
||||
return TCPIP_INVAILD_SOCKET;
|
||||
}
|
||||
int i, errorCode, numBytesSent;
|
||||
unsigned long numDataBytes;
|
||||
SOCKET s = m_Socket;
|
||||
m_iBytesSent = 0;
|
||||
m_iBytesToSend = sizeof(unsigned long) + 1;
|
||||
numDataBytes = htonl(m_iBytesToSend);
|
||||
for(i=0;i<sizeof(unsigned long);i++)
|
||||
m_SendBuf[i] = ((char*)(&numDataBytes))[i];
|
||||
m_SendBuf[sizeof(unsigned long)] = 3;
|
||||
|
||||
//send the header
|
||||
m_ClearSendBuf();
|
||||
m_iBytesToSend=sizeof(HandShaking);
|
||||
memcpy(m_SendBuf,HandShaking,m_iBytesToSend);
|
||||
m_SendBuf[m_iBytesToSend-1]=m_ClientIPAddress.S_un.S_un_b.s_b4;
|
||||
return SendBuffer();
|
||||
}
|
||||
//================================================================
|
||||
TCPIP_RETURN_CODE CSo7_TCPIP::SendBuffer()
|
||||
{
|
||||
int errorCode, numBytesSent;
|
||||
bool bInfinite=true;
|
||||
while(bInfinite)
|
||||
{
|
||||
numBytesSent = send(s, &(m_SendBuf[m_iBytesSent]), m_iBytesToSend-m_iBytesSent, 0);
|
||||
numBytesSent = send(m_Socket, &(m_SendBuf[m_iBytesSent]), m_iBytesToSend-m_iBytesSent, 0);
|
||||
if(numBytesSent == SOCKET_ERROR)
|
||||
{
|
||||
errorCode = WSAGetLastError();
|
||||
@@ -167,7 +172,18 @@ TCPIP_RETURN_CODE CSo7_TCPIP::Send(int _Addr,int _Data)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return TCPIP_CONNECT_OK;
|
||||
}
|
||||
|
||||
//================================================================
|
||||
TCPIP_RETURN_CODE CSo7_TCPIP::Send(int _Addr,int _Data)
|
||||
{
|
||||
_Addr=0;
|
||||
_Data=0;
|
||||
if(m_Socket == INVALID_SOCKET)
|
||||
{
|
||||
return TCPIP_INVAILD_SOCKET;
|
||||
}
|
||||
return TCPIP_CONNECT_OK;
|
||||
}
|
||||
//================================================================
|
||||
@@ -217,13 +233,15 @@ LRESULT CSo7_TCPIP::OnSocket(WPARAM wParam, LPARAM lParam)
|
||||
return 0;
|
||||
}
|
||||
//================================================================
|
||||
void CSo7_TCPIP::clearSendBuf(void)
|
||||
void CSo7_TCPIP::m_ClearSendBuf(void)
|
||||
{
|
||||
int i;
|
||||
for(i=0;i<m_iSendMaxBufSize;i++)
|
||||
{
|
||||
m_SendBuf[i] = 0x0;
|
||||
}
|
||||
m_iBytesSent=0;
|
||||
m_iBytesToSend=0;
|
||||
}
|
||||
|
||||
//================================================================
|
||||
@@ -258,7 +276,7 @@ void CSo7_TCPIP::m_ProcessSocketWriteEvent(void)
|
||||
if(m_iBytesSent>=m_iBytesToSend)
|
||||
{
|
||||
m_iBytesSent = 0;
|
||||
clearSendBuf();
|
||||
m_ClearSendBuf();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -267,86 +285,27 @@ void CSo7_TCPIP::m_ProcessSocketWriteEvent(void)
|
||||
void CSo7_TCPIP::m_ProcessSocketReadEvent(SOCKET s)
|
||||
{
|
||||
int bytesReceived=0;
|
||||
int headerSize = sizeof(unsigned long) + sizeof(char);
|
||||
int errorCode;
|
||||
if(true)
|
||||
{
|
||||
unsigned long *numBytesPtr;
|
||||
bytesReceived = recv(s, &(m_ReceiveBuf[m_iBytesReceived]), headerSize-m_iBytesReceived, 0);
|
||||
if(bytesReceived == SOCKET_ERROR)
|
||||
{
|
||||
errorCode = WSAGetLastError();
|
||||
if (errorCode == WSAEWOULDBLOCK)
|
||||
{
|
||||
//have to wait for the next receive event
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
//showSocketError(errorCode);
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_iBytesReceived += bytesReceived;
|
||||
if(m_iBytesReceived < sizeof(unsigned long) + sizeof(char))
|
||||
{//like this will ever happen... Have to wait for the next receive event
|
||||
return;
|
||||
}
|
||||
numBytesPtr = (unsigned long*) &(m_ReceiveBuf[0]);
|
||||
m_iBytesToReceive = ntohl(*numBytesPtr);
|
||||
m_iBytesReceived = 0;
|
||||
|
||||
//allocate memory for the character or the double array
|
||||
if(true)
|
||||
{
|
||||
if(m_dReceiveDBuf)
|
||||
{//clean memory because returning
|
||||
delete [] m_dReceiveDBuf;
|
||||
m_dReceiveDBuf = NULL;
|
||||
}
|
||||
m_iReceiveDBufSize = m_iBytesToReceive/(sizeof(double));
|
||||
m_dReceiveDBuf = new double[m_iReceiveDBufSize];
|
||||
memset (m_dReceiveDBuf,m_iReceiveDBufSize*sizeof(double),0); // PR#254048
|
||||
}
|
||||
}
|
||||
|
||||
//The header has been received. Now we need to get the data
|
||||
if(true)
|
||||
{
|
||||
bytesReceived = recv(s, &(((char*)m_dReceiveDBuf)[m_iBytesReceived]),
|
||||
m_iBytesToReceive - m_iBytesReceived, 0);
|
||||
}
|
||||
bytesReceived = recv(s, &(m_ReceiveBuf[0]), m_iReceiveMaxBufSize, 0);
|
||||
if(bytesReceived == SOCKET_ERROR)
|
||||
{
|
||||
errorCode = WSAGetLastError();
|
||||
|
||||
if (errorCode == WSAEWOULDBLOCK)
|
||||
{//have to wait for the next receive event
|
||||
{
|
||||
//have to wait for the next receive event
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(m_dReceiveDBuf)
|
||||
{//clean memory because returning
|
||||
delete [] m_dReceiveDBuf;
|
||||
m_dReceiveDBuf = NULL;
|
||||
m_iReceiveDBufSize = 0;
|
||||
}
|
||||
//showSocketError(errorCode);
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_iBytesReceived += bytesReceived;
|
||||
if(m_iBytesReceived >= m_iBytesToReceive)
|
||||
{
|
||||
//we are done, reset the variables for next data and clean up
|
||||
m_iBytesReceived = 0;
|
||||
m_iBytesToReceive = 0;
|
||||
if(m_dReceiveDBuf)
|
||||
{
|
||||
delete [] m_dReceiveDBuf;
|
||||
m_dReceiveDBuf = NULL;
|
||||
m_iReceiveDBufSize = 0;
|
||||
}
|
||||
if(m_iBytesReceived < sizeof(unsigned long) + sizeof(char))
|
||||
{//like this will ever happen... Have to wait for the next receive event
|
||||
return;
|
||||
}
|
||||
m_iBytesToReceive = m_iBytesReceived;
|
||||
m_iBytesReceived = 0;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ public:
|
||||
TCPIP_RETURN_CODE Recv(int _Addr,int& _Data,bool _bWait);
|
||||
TCPIP_RETURN_CODE DisConnect();
|
||||
TCPIP_RETURN_CODE GetHostIPAddr(in_addr& _IPAddress);
|
||||
TCPIP_RETURN_CODE Handshaking();
|
||||
|
||||
|
||||
LRESULT OnSocket(WPARAM wParam, LPARAM lParam);
|
||||
@@ -51,9 +52,11 @@ private:
|
||||
|
||||
static SOCKET m_Socket;
|
||||
static in_addr m_SreverIPAddress;
|
||||
static in_addr m_ClientIPAddress;
|
||||
static u_short m_iServerPortNumber;
|
||||
|
||||
void clearSendBuf();
|
||||
|
||||
TCPIP_RETURN_CODE SendBuffer();
|
||||
void m_ClearSendBuf();
|
||||
|
||||
int Init_Winsock();
|
||||
void m_ProcessSocketWriteEvent();
|
||||
|
||||
Reference in New Issue
Block a user