Error 10045 in UDP Communication

Source: Internet
Author: User

During UDP communication, the server binds the IP address and port:

M_stlocaladdr.sin_family = af_inet;
M_stlocaladdr.sin_addr.s_addr = htonl (inaddr_any );

M_stlocaladdr.sin_port = htons (6000 );

M_hsocket = socket (pf_inet, sock_dgram, 0 );

If (socket_error = BIND (m_hsocket, (struct sockaddr *) (& m_stlocaladdr), sizeof (m_stlocaladdr )))
Return false;

 

Send data:

Sockaddr_in m_stserviceaddr;
M_stserviceaddr.sin_family = af_inet;
M_stserviceaddr.sin_port = htons (6000 );

Long nip = inet_addr ("192.168.1.1 ");
M_stserviceaddr.sin_addr.s_un.s_addr = htonl (NIP );

 

Receive data

Nbuflen = recvfrom (m_hsocket, (char *) lpdatabuf, nbuflen, 0, (struct sockaddr *) (& stremoteaddr), & addr_size );

 

-1 is always returned, and the socket error is 10054,

 

It is actually your own mistake,

M_stserviceaddr.sin_addr.s_un.s_addr = htonl (NIP );

It should be

M_stserviceaddr.sin_addr.s_un.s_addr = NIP;

Not careful!

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.