socket常用的几个函数

来源:互联网 发布:淘宝店铺号怎么隐藏 编辑:程序博客网 时间:2024/05/17 08:14

下面是几个常用的socket函数,当然建立server和client的基本流程中使用的那几个就不写了。

 

1,htonl,The htonl function converts a u_long from host to TCP/IP network byte order (which is big endian).
htons, ntohs, ntohl功能也类似。

2,getservbyname,得到name(s),service number以及协议和端口号。

3,gethostbyname,根据host name字符串得到IPV4的地址等host信息。
gethostname是根据地址等信息得到host name的字符串。

4,getsockname,根据自己机器的一个socket handle得到全部的该socket信息,包括address family,port等。
而getpeername,根据peer机器来的socket得到该socket的信息
这两个函数,只有在面向连接的协议中使用(TCP)。

5,getsockopt,setsockopt设置socket的option

 

6,书上一直说阻塞socket和非阻塞socket,直到现在才知道如何做到阻塞和非阻塞。就是这个函数:ioctlsocket。根据MSDN:controls the I/O mode of a socket。

比如:
//-------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled;
// If iMode != 0, non-blocking mode is enabled.
u_long iMode = 0;
ioctlsocket(m_socket, FIONBIO, &iMode);

原创粉丝点击