Use WinSock for connectionless communication

Source: Internet
Author: User

 

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
  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.