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 ));
}