get host ip address.

This commit is contained in:
TAO Cheng
2014-10-29 21:05:58 +08:00
parent 02f1c7e503
commit 32f9acc866
7 changed files with 283 additions and 71 deletions
@@ -8,21 +8,39 @@ const int WSA_MINOR_VERSION = 1;
#define WSA_VERSION MAKEWORD(WSA_MAJOR_VERSION, WSA_MINOR_VERSION)
#define SOCKADDR_LEN sizeof(SOCKADDR_IN)
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;
u_short CSo7_TCPIP::m_iServerPortNumber=static_cast<u_short>(0);
//================================================================
CSo7_TCPIP::CSo7_TCPIP()
{
m_socket=INVALID_SOCKET;
m_pszNetworkAddr="127.0.0.1";
m_nPortNumber=8000;
m_bDisableTimeout=false;
m_hMsgWnd=NULL;
m_ReceiveBuf=NULL;
m_SendBuf=NULL;
m_iBytesToReceive = 0;
m_iBytesReceived = 0;
m_iBytesToSend = 0;
m_iBytesSent = 0;
m_ReceiveBuf = new char[m_iReceiveMaxBufSize];
m_SendBuf = new char[m_iSendMaxBufSize];
}
//================================================================
CSo7_TCPIP::~CSo7_TCPIP()
{
//DisConnect();
DisConnect();
if(m_SendBuf)
delete [] m_SendBuf;
m_SendBuf=NULL;
if(m_ReceiveBuf)
delete [] m_ReceiveBuf;
m_ReceiveBuf=NULL;
}
//================================================================
int CSo7_TCPIP::Init_Winsock()
@@ -41,11 +59,13 @@ int CSo7_TCPIP::Init_Winsock()
}
return TRUE;
}
//================================================================
TCPIP_RETURN_CODE CSo7_TCPIP::Connect()
TCPIP_RETURN_CODE CSo7_TCPIP::Connect(const HWND& _hWnd,const in_addr& _IPAddress,const u_short& _nPortNumber)
{
TCPIP_RETURN_CODE rCode=TCPIP_CONNECT_OK;
//DisConnect();
m_hMsgWnd=_hWnd;
m_iServerPortNumber=_nPortNumber;
int retVal(0), errorCode(0);
retVal=Init_Winsock();
if(!retVal)
@@ -53,42 +73,23 @@ TCPIP_RETURN_CODE CSo7_TCPIP::Connect()
return TCPIP_INIT_WINSOCK_ERROR;
}
// 创建套节字
m_socket = socket(AF_INET, SOCK_STREAM, 0);
if(m_socket == INVALID_SOCKET)
m_Socket = socket(AF_INET, SOCK_STREAM, 0);
if(m_Socket == INVALID_SOCKET)
{
rCode=TCPIP_INVAILD_SOCKET;
}
else
{
// 设置socket为窗口通知消息类型
::WSAAsyncSelect(m_socket, m_hMsgWnd,
WM_SOCKET, FD_CONNECT | FD_CLOSE | FD_WRITE | FD_READ);
// 假定szAddr是IP地址
ULONG uAddr = inet_addr(m_pszNetworkAddr);
if(uAddr == INADDR_NONE)
{
// 不是IP地址,就认为这是主机名称
// 从主机名取得IP地址
hostent* pHost = ::gethostbyname(m_pszNetworkAddr);
if(pHost == NULL)
{
closesocket(m_socket);
m_socket = INVALID_SOCKET;
return TCPIP_INVAILD_SOCKET;
}
// 得到以网络字节顺序排列的IP地址
uAddr = ((struct in_addr*)*(pHost->h_addr_list))->s_addr;
}
::WSAAsyncSelect(m_Socket, m_hMsgWnd,WM_SOCKET, FD_CONNECT | FD_CLOSE | FD_WRITE | FD_READ);
// 填写服务器地址信息
sockaddr_in remote;
remote.sin_addr.S_un.S_addr = uAddr;
remote.sin_addr = _IPAddress;
remote.sin_family = AF_INET;
remote.sin_port = htons(m_nPortNumber);
remote.sin_port = htons(m_iServerPortNumber);
// 连接到远程机
retVal=connect(m_socket, (sockaddr*)&remote, sizeof(sockaddr));
retVal=connect(m_Socket, (sockaddr*)&remote, sizeof(sockaddr));
if(retVal)
{
errorCode = WSAGetLastError();
@@ -100,33 +101,82 @@ TCPIP_RETURN_CODE CSo7_TCPIP::Connect()
//================================================================
TCPIP_RETURN_CODE CSo7_TCPIP::DisConnect()
{
if(m_socket == INVALID_SOCKET)
if(m_Socket == INVALID_SOCKET)
{
return TCPIP_INVAILD_SOCKET;
}
::closesocket(m_socket);
m_socket = INVALID_SOCKET;
::closesocket(m_Socket);
m_Socket = INVALID_SOCKET;
return TCPIP_CONNECT_OK;
}
//================================================================
TCPIP_RETURN_CODE CSo7_TCPIP::Send()
TCPIP_RETURN_CODE CSo7_TCPIP::GetHostIPAddr(in_addr& _IPAddress)
{
if(m_socket == INVALID_SOCKET)
if(m_Socket == INVALID_SOCKET)
{
return TCPIP_INVAILD_SOCKET;
}
BYTE bSendData[20]={1,2,3,4,5};
int iSendDataLength=20;
if(::send(m_socket,(const char*)(bSendData),iSendDataLength,0) != -1)
{
return TCPIP_CONNECT_OK;
}
char HostName[100];
gethostname(HostName,sizeof(HostName));
hostent *pAddr;
pAddr=gethostbyname(HostName);
memcpy(&_IPAddress,pAddr->h_addr_list[0],sizeof(in_addr));
return TCPIP_CONNECT_OK;
}
//================================================================
TCPIP_RETURN_CODE CSo7_TCPIP::Recv()
TCPIP_RETURN_CODE CSo7_TCPIP::Send(int _Addr,int _Data)
{
if(m_socket == INVALID_SOCKET)
_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
bool bInfinite=true;
while(bInfinite)
{
numBytesSent = send(s, &(m_SendBuf[m_iBytesSent]), m_iBytesToSend-m_iBytesSent, 0);
if(numBytesSent == SOCKET_ERROR)
{
errorCode = WSAGetLastError();
if(errorCode == WSAEWOULDBLOCK)
{
return (TCPIP_RETURN_CODE)errorCode; //Should get a FD_WRITE event if this happens. Send the rest from there.
}
return (TCPIP_RETURN_CODE)errorCode; //should check for other errors here, and set an error code or something.
}
else
{
m_iBytesSent += numBytesSent;
}
if(m_iBytesSent>=m_iBytesToSend)
{
m_iBytesToSend = 0;
m_iBytesSent = 0;
break;
}
}
return TCPIP_CONNECT_OK;
}
//================================================================
TCPIP_RETURN_CODE CSo7_TCPIP::Recv(int _Addr,int& _Data,bool _bWait)
{
_Addr=0;
_Data=0;
_bWait=false;
if(m_Socket == INVALID_SOCKET)
{
return TCPIP_INVAILD_SOCKET;
}
@@ -152,18 +202,151 @@ LRESULT CSo7_TCPIP::OnSocket(WPARAM wParam, LPARAM lParam)
break;
case FD_READ:
{
char szText[1024] = { 0 };
::recv(s, szText, 1024, 0);
Recv();
m_ProcessSocketReadEvent(s);
}
break;
case FD_WRITE:
//This event happens when the tcp buffers are ready for another send.
//So if there is not enough room for the first write the event will happen
//and then the buffers are ready for more.
m_ProcessSocketWriteEvent();
break;
case FD_CLOSE:
break;
}
return 0;
}
}
//================================================================
void CSo7_TCPIP::clearSendBuf(void)
{
int i;
for(i=0;i<m_iSendMaxBufSize;i++)
{
m_SendBuf[i] = 0x0;
}
}
//================================================================
void CSo7_TCPIP::m_ProcessSocketWriteEvent(void)
{
int errorCode, numBytesSent;
SOCKET s = m_Socket;
if(m_iBytesToSend <= 0)
return;
char* sendBuf = m_SendBuf;
while(1)
{
numBytesSent = send(s, &(sendBuf[m_iBytesSent]), m_iBytesToSend-m_iBytesSent, 0);
if(numBytesSent == SOCKET_ERROR)
{
errorCode = WSAGetLastError();
if(errorCode == WSAEWOULDBLOCK)
{
return; //Should get a FD_WRITE event if this happens. Send the rest from there.
}
else
{
//showSocketError(errorCode);
return;
}
}
else
{
m_iBytesSent += numBytesSent;
}
if(m_iBytesSent>=m_iBytesToSend)
{
m_iBytesSent = 0;
clearSendBuf();
break;
}
}
}
//================================================================
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);
}
if(bytesReceived == SOCKET_ERROR)
{
errorCode = WSAGetLastError();
if (errorCode == WSAEWOULDBLOCK)
{//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;
}
}
}
@@ -1,5 +1,3 @@
// protocol for control SevenOcean's Machine
//
//////////////////////////////////////////////////////////////////////
#ifndef SO7_TCPIP_H_INCLUDED_
#define SO7_TCPIP_H_INCLUDED_
@@ -31,18 +29,35 @@ public:
CSo7_TCPIP();
virtual ~CSo7_TCPIP();
TCPIP_RETURN_CODE Connect();
TCPIP_RETURN_CODE Send();
TCPIP_RETURN_CODE Recv();
TCPIP_RETURN_CODE Connect(const HWND& _hWnd,const in_addr& _IPAddress,const u_short& _nPortNumber);
TCPIP_RETURN_CODE Send(int _Addr,int _Data);
TCPIP_RETURN_CODE Recv(int _Addr,int& _Data,bool _bWait);
TCPIP_RETURN_CODE DisConnect();
HWND m_hMsgWnd;
TCPIP_RETURN_CODE GetHostIPAddr(in_addr& _IPAddress);
LRESULT OnSocket(WPARAM wParam, LPARAM lParam);
private:
SOCKET m_socket;
LPCSTR m_pszNetworkAddr;
u_short m_nPortNumber;
bool m_bDisableTimeout;
int Init_Winsock();
HWND m_hMsgWnd;
char *m_ReceiveBuf;
char *m_SendBuf;
double *m_dReceiveDBuf;
int m_iReceiveDBufSize;
int m_iBytesToReceive,m_iBytesReceived;
int m_iBytesToSend,m_iBytesSent;
static int m_iReceiveMaxBufSize;
static int m_iSendMaxBufSize;
static SOCKET m_Socket;
static in_addr m_SreverIPAddress;
static u_short m_iServerPortNumber;
void clearSendBuf();
int Init_Winsock();
void m_ProcessSocketWriteEvent();
void m_ProcessSocketReadEvent(SOCKET s);
};
#endif
@@ -1050,8 +1050,10 @@ BEGIN
PUSHBUTTON "Read Data",IDC_BUTTON_PLC_TCPIP_READ,203,30,63,18
PUSHBUTTON "Write Data",IDC_BUTTON_PLC_TCPIP_WRITE,203,62,63,18
GROUPBOX "TCP/IP Control",IDC_STATIC,110,12,174,81
EDITTEXT IDC_EDIT_PLC_TCPIP_ADDR,139,47,40,14,ES_AUTOHSCROLL
LTEXT "Addr",IDC_STATIC,116,50,16,8
EDITTEXT IDC_EDIT_PLC_TCPIP_ADDR,141,32,40,14,ES_AUTOHSCROLL
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
END
@@ -140,13 +140,13 @@ BOOL CSo7_Option::OnInitDialog()
((CButton *)GetDlgItem(IDC_RADIO_VIDEOCARD_TC4000))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_VIDEOCARD_MV900))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_SO7_IP_CAMERA))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_CONTROLLER))->SetCheck(TRUE);
((CButton *)GetDlgItem(IDC_RADIO_CONTROLLER))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO__SO7_RS232))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_KEYENCE_LASER))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_SO7_VERIFICATION_ALGORITHM))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_TEST_IMAGE_DLL))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_ART_PCI8622))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_SO7_OMRON_PLC))->SetCheck(FALSE);
((CButton *)GetDlgItem(IDC_RADIO_SO7_OMRON_PLC))->SetCheck(TRUE);
#ifdef _RELEASE_SO7_CONTROLER_ONLY
((CButton *)GetDlgItem(IDC_RADIO_VIDEOCARD_SDK3000))->EnableWindow(FALSE);
@@ -48,9 +48,15 @@ END_MESSAGE_MAP()
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipConnect()
{
g_pSo7_TCPIP->m_hMsgWnd=m_hWnd;
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Connect();
m_csMSG.Format(_T("Connect return code:%d."),rCode);
in_addr IPAddress;
IPAddress.S_un.S_addr=inet_addr("192.168.0.5");
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Connect(m_hWnd,IPAddress,9600);
m_csMSG.Format(_T("Server:192.168.0.5[9600] Connect return code:%d."),rCode);
OutputWithScroll(m_csMSG,m_edMSG);
rCode=g_pSo7_TCPIP->GetHostIPAddr(IPAddress);
m_csMSG.Format(_T("Client:%d.%d.%d.%d return code:%d."),
IPAddress.S_un.S_un_b.s_b1,IPAddress.S_un.S_un_b.s_b2,
IPAddress.S_un.S_un_b.s_b3,IPAddress.S_un.S_un_b.s_b4,rCode);
OutputWithScroll(m_csMSG,m_edMSG);
}
@@ -58,7 +64,7 @@ void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipConnect()
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipDisconnect()
{
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->DisConnect();
m_csMSG.Format(_T("Connect return code:%d."),rCode);
m_csMSG.Format(_T("DisConnect return code:%d."),rCode);
OutputWithScroll(m_csMSG,m_edMSG);
}
@@ -71,9 +77,9 @@ void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipRead()
void CSo7_Util_PLC_TCPIP::OnBnClickedButtonPlcTcpipWrite()
{
TCPIP_RETURN_CODE rCode=g_pSo7_TCPIP->Send();
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("Connect return code:%d."),rCode);
//OutputWithScroll(m_csMSG,m_edMSG);
}
@@ -113,7 +119,11 @@ 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 );
}
@@ -1053,6 +1053,8 @@
#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_DIY_EXIT_BUTTON 32740
#define ID_EDIT_SO7_CONFIG_MOTION 32741