WinSock provides support for UDP (User Datagram Protocol). Through UDP, we can send data to the host with the specified IP address, or receive data from the host with the specified IP address, the sender and receiver are in the same status. It is easy to use CSocket to manipulate connectionless data transmission. First, a local interface (SOCK_DGRAM mark must be specified) is generated, and then
Int CAsyncSocket: SendTo (const void * lpBuf, int nBufLen, UINT nHostPort, LPCTSTR lpszHostAddress = NULL, int nFlags = 0) to send data,
Int CAsyncSocket: ReceiveFrom (void * lpBuf, int nBufLen, CString & rSocketAddress, UINT & rSocketPort, int nFlags = 0) receives data. Function call Sequence.
ExploitationUDP can send and receive data in two ways, that is, any host can send and receive data. However, the UDP protocol is connectionless, so the sent data may not be received, and the receiving sequence may be different from the sending sequence. The following code is used:
/*
The sender is on the portSend data to the receiver port 6800 on 6801
*/
// Sender code:
BOOL CMy62_s1_clientDlg: OnInitDialog ()
{
CDialog: OnInitDialog ();
// Create a local Interface
M_sockSend.Create (6800, SOCK_DGRAM, NULL );
// Bind a local Interface
M_sockSend.Bind (6800, "127.0.0.1 ");
// Create a timer for timed sending
SetTimer (1,3000, NULL );
...
}
Void CMy62_s1_clientDlg: OnTimer (UINT nIDEvent)
{
Static iIndex = 0;
Char szSend [20];
Sprintf (szSend, "% 010d", iIndex ++ );
// Send UDP data