socket常用的几个函数

下面是几个常用的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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值