WinSock programming Addendum

Source: Internet
Author: User

1. The listen function is not required for UDP.

Sockets that are connection oriented, those of Type sock_stream for example, are usedListen.

 

2.
The data structure of sockaddr_in is equivalent to that of sockaddr_in.
Sockaddr applies to any protocol family
Sockaddr_in only appliesAf_inet

 

3. initialize sockaddr_in
DeST. sin_family = af_inet;
DeST. sin_port = htons (50000 );
DeST. sin_addr.S_addr= Inet_addr ("127.0.0.1"); // s_addr is a macro.

4. Obtain the address from the machine name
Lphostent lphost = gethostbyname ("www.baidu.com ");

// Setup destination socket address
Sadest. sin_addr.s_addr = *(Pint) Lphost->H_addr; // H_addr is a macro.
Sadest. sin_family = af_inet;
Sadest. sin_port = 0;

 

5. IP address and String Conversion
Inet_ntoa (sadest. sin_addr) // convert it to a string
Inet_addr ("127.0.0.1") // convert to an IP address

6. Obtain the host name.
Gethostname

 

7. Determine whether the object is readable
Int issocketreadible (socket & S)
{
Struct timeval timeout;
Fd_set readfds;

Readfds. fd_count = 1;
Readfds. fd_array [0] = s;
Timeout. TV _sec = 1;
Timeout. TV _usec = 0;

Return (select (1, & readfds, null, null, & timeout ));
}

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.