Home > Software engineering >  Using MFC to set up a client SOCKET communication, how to transmit sensor data sent to the server.
Using MFC to set up a client SOCKET communication, how to transmit sensor data sent to the server.

Time:09-25

 # include "stdafx. H" 
# include "six dimensional force acquisition demo. H"
# include "six dimensional force acquisition demoDlg. H"
# include "afxdialogex. H"
# include "MySocketC. H"



# ifdef _DEBUG
# define new DEBUG_NEW
# endif


//C six dimensional force acquisition demoDlg dialog

The SOCKET sockConn;
DWORD WINAPI Socket_ListenProc (LPVOID lpParam);
DWORD WINAPI Socket_RecvProc (LPVOID lpParam);

BOOL m_bListen;//listen for sign
Cstrings IP;//record the client IP address

DWORD WINAPI Socket_ListenProc (LPVOID lpParam)
{
The SOCKET sock=((LISTENPARAM *) lpParam) - & gt; The sock.//the native socket
HWND HWND=((LISTENPARAM *) lpParam) - & gt; The HWND;//handle dialog

SOCKADDR_IN addrClient;
Int nLen=sizeof (SOCKADDR);

While (m_bListen)//listening identifier
{
SockConn=accept (the sock, (SOCKADDR *) & amp; AddrClient, & amp; NLen);

If (INVALID_SOCKET!=sockConn)
{
Cstrings STR.
STR. The Format (_T (" % s "), (cstrings) inet_ntoa (addrClient. Sin_addr));

RECVPARAM RECVPARAM;
RecvParam. HWND=HWND;
RecvParam. The sock=sockConn;
RecvParam. IP=STR;
IP=STR;
//a receiving data processing threads used to receive data
HANDLE hThread=CreateThread (NULL, 0, Socket_RecvProc, (LPVOID) & amp; RecvParam, 0, NULL);

}
}
return 0;
}

DWORD WINAPI Socket_RecvProc (LPVOID lpParam)
{
The SOCKET sock=((RECVPARAM *) lpParam) - & gt; The sock.
HWND HWND=((RECVPARAM *) lpParam) - & gt; The HWND;

Char recvBuf [100]={0};
Int nRetVal;
While (TRUE)
{
NRetVal=recv (sock, recvBuf, 8, 0).//the third argument for the length of the receiving data, the sensor fixed receive size of 8

If ((INVALID_SOCKET==nRetVal) | | (0==nRetVal) | | (nRetVal==WSAECONNRESET))
break;

//if successful receive 8 bytes to send and receive messages, call the corresponding receiving processing function for data processing
If (nRetVal==8)
: : SendMessage (HWND WM_RECVDATA, (WPARAM) 0, (LPARAM), recvBuf);
}
return 0;
}

Six dimensional force acquisition demoDlg: C: C six dimensional force acquisition demoDlg (pParent/*=NULL CWnd * */)
Six dimensional force acquisition demoDlg: CDialogEx (C: : IDD, pParent)
, m_Port (6001)
, m_DataReceive ()
, m_DataSend (_T (" "))
{
M_hIcon=AfxGetApp () - & gt; LoadIcon (IDR_MAINFRAME);
M_FX=0.0 f;
M_FY=0.0 f;
M_FZ=0.0 f;
M_MX=0.0 f;
M_MY=0.0 f;
M_MZ=0.0 f;
M_FrameNo=0;
M_IP=_T (" ");
FreNum=0;

}

Void six dimensional force acquisition demoDlg: C: DoDataExchange (CDataExchange * symbol)
{
CDialogEx: : DoDataExchange (symbol);
DDX_Text (symbol, IDC_FX m_FX);
DDX_Text (symbol, IDC_FY m_FY);
DDX_Text (symbol, IDC_FZ m_FZ);
DDX_Text (symbol, IDC_MX m_MX);
DDX_Text (symbol, IDC_MY m_MY);
DDX_Text (symbol, IDC_MZ m_MZ);
DDX_Text (symbol, IDC_Frame m_FrameNo);
DDX_Text (symbol, IDC_IP m_IP);
DDX_Text (symbol, IDC_Port m_Port);
DDX_Control (symbol, IDC_Connect m_ConPC);
DDX_Control (symbol, IDC_IPADDRESS1 m_ipaddress);
DDX_Text (symbol, IDC_DataSend m_DataSend);

}

Six dimensional force acquisition demoDlg BEGIN_MESSAGE_MAP (C, CDialogEx)
ON_WM_PAINT ()
ON_WM_QUERYDRAGICON ()
ON_MESSAGE (WM_RECVDATA OnRecvData)
ON_BN_CLICKED (IDC_BTNCONNECT, & amp; Six dimensional force acquisition demoDlg: C: OnBnClickedBtnconnect)
ON_BN_CLICKED (IDCANCEL, & amp; Six dimensional force acquisition demoDlg: C: OnBnClickedCancel)
ON_BN_CLICKED (IDC_Begin, & amp; Six dimensional force acquisition demoDlg: C: OnBnClickedBegin)
ON_BN_CLICKED (IDC_Stop, & amp; Six dimensional force acquisition demoDlg: C: OnBnClickedStop)
ON_WM_TIMER ()
ON_BN_CLICKED (IDC_Connect, & amp; Six dimensional force acquisition demoDlg: C: OnBnClickedConnect)
ON_BN_CLICKED (IDC_SendData, & amp; Six dimensional force acquisition demoDlg: C: OnBnClickedSenddata)
ON_BN_CLICKED (IDC_Change, & amp; Six dimensional force acquisition demoDlg: C: OnBnClickedChange)
END_MESSAGE_MAP ()


//C six dimensional force acquisition demoDlg message handler

BOOL C demoDlg: six dimensional force acquisition: OnInitDialog ()
{
CDialogEx: : OnInitDialog ();
M_connect=false;
M_ipaddress. SetWindowTextW (_T (" 192.168.1.197 "));
//set this dialog ICONS, when the application is the main window is not a dialog, the framework will automatically
//do this
SetIcon (m_hIcon, TRUE);//set the large icon
SetIcon (m_hIcon, FALSE);//set the small icon

//TODO: add additional initialization code in the
//below is triggering a SOCKET,
//to create a socket after preparing
WORD wVersionRequested;
WSADATA wsaData;
int err;
WVersionRequested=MAKEWORD (1, 1);
Err=WSAStartup (wVersionRequested, & amp; WsaData);
//because the dialog box is closed, so will return FALSE to exit the application,
//rather than start the application's message pump,

If (err==0)
{
AfxMessageBox (_T (" trigger SOCKET success "));
}
The else
{
AfxMessageBox (_T (" trigger SOCKET failure "));
}

//set the timer data in a real-time refresh dialog
The SetTimer (1100, NULL);
return TRUE;//unless set focus to the control, return TRUE
}




LRESULT six dimensional force acquisition demoDlg: C: OnRecvData (WPARAM WPARAM, LPARAM LPARAM)
{
//cstrings IP=_T (" 0 ");
Char * pRecvData;
LParam pRecvData=https://bbs.csdn.net/topics/(char *);
RecvData (IP, pRecvData);
return 0;
}

//receive handler
//this function to receive data, namely six channels are obtained by receiving the raw data, the specific value (analog voltage value, using available for subsequent development)
//data processing algorithm is fixed (according to the mechanism of a machine to send data below)
//each frame of data points on the half and half for processing, using FIRST identified in your program
//ONE mark for the first time, according to the collected button, the first collection
Void six dimensional force acquisition demoDlg: C: RecvData (cstrings IP, char * pData)
{

M_IP=IP;//save the client's IP address
If (ONE & amp; & ! FIRST)
{
ONE=FALSE;
The FIRST=TRUE; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related