Home > Software engineering >  Questions about MFC server data transmission, transfer the COM
Questions about MFC server data transmission, transfer the COM

Time:10-04

A post on the problem description not clear
Want to write a simple server receives the client sends data, and then sent through the COM,
Receiver, receiving data from the COM through the server to the client, is only for hexadecimal transmission,
Using CSocket class writing server, refer to: http://jingyan.baidu.com/article/676629974557c254d51b84da.html? Qq - pf - to=PCQQ. C2c
Using MSCOMM control, reference: http://wenku.baidu.com/link? Url=6 c1yqkrn79eemrwhzq3kwlyxt3stltpynkbuyd_ygsjktdag35qdzhcoubhzqepafjncqcq391jobjz6konuqlklln12qnunxh544zytayq

Now encountered two problems
1. Send data hexadecimal, 0 x00 0 xaa can appear abnormal, 0 x00 empty directly, xaa to 0 0 x3f
2. Receive COM back transmission of data, will be segmented,

Question 1, do the test, the client sends data to the server returned data should be the same, as shown in figure problems
Send 1
Receive 1
0 x00 data broken

Send 2
Receive 2
0 xaa into 0 x3f

According to the @ zgl7903 ASCII visible part of the 0 x20 ~ 0 x7f
Found this problem
Send 3
Receive 3
Send 3 0 xaa, only the last a 0 xaa disappeared, puzzling,

Question 2, as shown in figure
Send data [55 08 30 01 01 2 a 64 AA]
The client accept the data as shown in figure
Divided into two sections to accept
The following part of the code

The server to accept the client sends data forwarding to the client the COM
 
Void CSocketDlg: : RecvData (CServerSocket * pSocket)
{
BYTE * pData=https://bbs.csdn.net/topics/NULL;
PData=https://bbs.csdn.net/topics/new BYTE [1024];
Memset (pData, 0, sizeof (BYTE) * 1024);

Cstrings STR.
If (pSocket - & gt; The Receive (pData, 1024, 0).=SOCKET_ERROR)
{

STR=pData;
SendMSG (STR);//forwarding data to all users, including users to send data
M_mscom. Put_Output (COleVariant (STR));//send the hexadecimal data
}
The delete pData;
PData=https://bbs.csdn.net/topics/NULL;
}


Forwarded to the client's function
 
Void CSocketDlg: : SendMSG (cstrings STR)
{
//generated protocol head
Char * pSend=new char [STR. GetLength ()];
Memset (pSend, 0, STR. GetLength () * sizeof (char));
if (! WChar2MByte (STR) GetBuffer (0), pSend, STR., GetLength ()))
{
AfxMessageBox (_T (" character conversion failure "));
The delete pSend;
return;

}

The POSITION of nPos=m_clientList. GetHeadPosition ();
While (nPos)
{
CServerSocket * pTemp=(CServerSocket *) m_clientList GetNext (nPos);
PTemp - & gt; Send (pSend, STR GetLength ());
}

The delete pSend;

}


CSocket class accept data function
 
Int CSocket: : Receive (void * lpBuf, int nBufLen, int nFlags)
{
If (m_pbBlocking!=NULL)
{
WSASetLastError (WSAEINPROGRESS);
Return FALSE;
}
Int nResult;
While ((nResult=CAsyncSocket: : Receive (lpBuf, nBufLen nFlags))==SOCKET_ERROR)
{
If (GetLastError ()==WSAEWOULDBLOCK)
{
if (! PumpMessages (FD_READ))
Return SOCKET_ERROR;
}
The else
Return SOCKET_ERROR;
}
Return nResult;
}


MSCOMM control data receiving the COM message response function
 
Void CSocketDlg: : OnCommMscomm1 ()
{

If (m_mscom get_CommEvent ()==2)
{
Char STR [1024]={0};
Long k;
Cstrings strtemp;
The VARIANT InputData=https://bbs.csdn.net/topics/m_mscom.get_Input ();//read buffer
COleSafeArray fs.
Fs=InputData;//the VARIANT type variable converting a quantity COleSafeArray type variable
For (k=0; kFs. GetElement (& amp; K, STR + k);//type into a BYTE array
Strtemp +=STR;//receive the edit box inside
SendMSG (strtemp);

}
}

CodePudding user response:

Question 1:
Send 1 & amp; Receive 1:
SendMSG (cstrings STR), middle 00 STR content, cause cstrings when calculating the length, the ending until 00, so truncate; If you want to send the content of the intermediate belt 00, it is better to pass in the form of byte array, instead of string,
Send 2/3 & amp; Receive two-thirds:
WChar2MByte function conversion process has a problem,

Question 2:
Is not problem, is likely to block received ah,

CodePudding user response:

It is recommended to use portmon auxiliary software debugging serial communication program,

CodePudding user response:

reference 1st floor ddszhan response:
question 1:
Send 1 & amp; Receive 1:
SendMSG (cstrings STR), middle 00 STR content, cause cstrings when calculating the length, the ending until 00, so truncate; If you want to send the content of the intermediate belt 00, it is better to pass in the form of byte array, instead of string,
Send 2/3 & amp; Receive two-thirds:
WChar2MByte function conversion process has a problem,

Question 2:
Is not the problem, is likely to block received ah,

Because I received data from the com is a need to do processing period, if segment, the packet is wrong, will fail, what can solve do,,?

CodePudding user response:

He set a receive buffer and unpacking,

CodePudding user response:

Look, I don't think the main problem is to determine the communication protocol
Socket communication protocol determines the server and client to send and receive, buffer, sticky package deal with these problems
Com port communication protocol and equipment related to
  • Related