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();
|
||||
|
||||
@@ -1043,8 +1043,8 @@ FONT 8, "MS Shell Dlg", 400, 0, 0x1
|
||||
BEGIN
|
||||
PUSHBUTTON "Exit",IDCANCEL,292,230,39,14
|
||||
GROUPBOX "TCP/IP Control",IDC_STATIC,17,12,87,81
|
||||
PUSHBUTTON "Connect",IDC_BUTTON_PLC_TCPIP_CONNECT,29,29,63,18
|
||||
PUSHBUTTON "Disconnect",IDC_BUTTON_PLC_TCPIP_DISCONNECT,29,64,63,18
|
||||
PUSHBUTTON "Connect",IDC_BUTTON_PLC_TCPIP_CONNECT,28,28,63,18
|
||||
PUSHBUTTON "Disconnect",IDC_BUTTON_PLC_TCPIP_DISCONNECT,28,70,63,18
|
||||
EDITTEXT IDC_EDIT_MSG,19,97,265,130,ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN | WS_VSCROLL
|
||||
PUSHBUTTON "Clear",IDC_BUTTON_PLC_TCPIP_CLEAR_MSG,289,156,39,14
|
||||
PUSHBUTTON "Read Data",IDC_BUTTON_PLC_TCPIP_READ,203,30,63,18
|
||||
@@ -1054,6 +1054,7 @@ BEGIN
|
||||
LTEXT "Addr",IDC_STATIC,116,35,16,8
|
||||
EDITTEXT IDC_EDIT_PLC_TCPIP_Data,141,61,40,14,ES_AUTOHSCROLL
|
||||
LTEXT "Data",IDC_STATIC,116,64,16,8
|
||||
PUSHBUTTON "Handshaking",IDC_BUTTON_PLC_TCPIP_HANDSHAKING,28,49,63,18
|
||||
END
|
||||
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ BEGIN_MESSAGE_MAP(CSo7_Util_PLC_TCPIP, CDialog)
|
||||
ON_BN_CLICKED(IDCANCEL, &CSo7_Util_PLC_TCPIP::OnBnClickedCancel)
|
||||
ON_MESSAGE(WM_SOCKET,&CSo7_Util_PLC_TCPIP::OnSocket)
|
||||
ON_BN_CLICKED(IDCANCEL, &CSo7_Util_PLC_TCPIP::OnBnClickedCancel)
|
||||
ON_BN_CLICKED(IDC_BUTTON_PLC_TCPIP_HANDSHAKING, &CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipHandshaking)
|
||||
END_MESSAGE_MAP()
|
||||
|
||||
|
||||
@@ -74,12 +75,18 @@ void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipRead()
|
||||
;
|
||||
}
|
||||
|
||||
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipHandshaking()
|
||||
{
|
||||
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Handshaking();
|
||||
m_csMSG.Format(_T("Handshaking return code:%d."),rCode);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
}
|
||||
|
||||
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipWrite()
|
||||
{
|
||||
//TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Send(1001,0);
|
||||
//m_csMSG.Format(_T("Connect return code:%d."),rCode);
|
||||
//OutputWithScroll(m_csMSG,m_edMSG);
|
||||
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Send(1001,0);
|
||||
m_csMSG.Format(_T("Write return code:%d."),rCode);
|
||||
OutputWithScroll(m_csMSG,m_edMSG);
|
||||
}
|
||||
|
||||
|
||||
@@ -119,11 +126,8 @@ void CSo7_Util_PLC_TCPIP::OutputWithScroll(const CString &strNewText,CEdit &edtO
|
||||
LRESULT CSo7_Util_PLC_TCPIP::OnSocket(WPARAM w, LPARAM p)
|
||||
{
|
||||
LRESULT lResult=0;
|
||||
UNREFERENCED_PARAMETER(w);
|
||||
UNREFERENCED_PARAMETER(p);
|
||||
/*
|
||||
if( g_pSo7_TCPIP )
|
||||
lResult = g_pSo7_TCPIP->OnSocket(w,p);
|
||||
*/
|
||||
|
||||
return( lResult );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,4 +28,5 @@ public:
|
||||
afx_msg void OnBnClickedButtonPlcTcpipClearMsg();
|
||||
afx_msg void OnBnClickedCancel();
|
||||
afx_msg LRESULT OnSocket(WPARAM w, LPARAM p);
|
||||
afx_msg void OnBnClickedButtonPlcTcpipHandshaking();
|
||||
};
|
||||
|
||||
@@ -1053,8 +1053,9 @@
|
||||
#define IDC_BUTTON_PLC_TCPIP_WRITE 1886
|
||||
#define IDC_EDIT_PLC_TCPIP_ADDR 1887
|
||||
#define IDC_EDIT_MSG 1888
|
||||
#define IDC_EDIT_PLC_TCPIP_ADDR2 1889
|
||||
#define IDC_EDIT_PLC_TCPIP_Data 1889
|
||||
#define IDC_BUTTON_PLC_TCPIP_CONNECT2 1890
|
||||
#define IDC_BUTTON_PLC_TCPIP_HANDSHAKING 1890
|
||||
#define IDC_BUTTON_DIY_EXIT_BUTTON 32740
|
||||
#define ID_EDIT_SO7_CONFIG_MOTION 32741
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user