|
地板

楼主 |
发表于 2009-7-10 20:15:15
|
只看该作者
绑定套接字
<span style=\"font-family: courier new,courier,monospace;\">bind() 函数用来将一个套接字和某个端口绑定到一块,bind() 函数原型如下:<br /></span><span style=\"color: rgb(0, 136, 0);\"> </span><span style=\"color: rgb(0, 136, 0);\">#include <sys/types.h></span><br /> <span style=\"color: rgb(0, 136, 0);\">#include <sys/socket.h></span><br /> <br /> <span style=\"color: rgb(0, 187, 0); font-weight: bold;\">int</span> <span style=\"color: rgb(0, 0, 0);\">bind</span>(<span style=\"color: rgb(0, 187, 0); font-weight: bold;\">int</span> <span style=\"color: rgb(0, 0, 0);\">sockfd</span><span style=\"color: rgb(0, 0, 0);\">,</span> <span style=\"color: rgb(170, 34, 255); font-weight: bold;\">struct</span> <span style=\"color: rgb(0, 0, 0);\">sockaddr</span> <span style=\"color: rgb(102, 102, 102);\">*</span><span style=\"color: rgb(0, 0, 0);\">my_addr</span><span style=\"color: rgb(0, 0, 0);\">,</span> <span style=\"color: rgb(0, 0, 0);\">socklen_t</span> <span style=\"color: rgb(0, 0, 0);\">addrlen</span>); <br /><br /><span style=\"font-family: courier new,courier,monospace;\">socket() 只是创建了一个套接字,这个套接字将工作在哪个端口上,程序并没有指定。<br />在客户机/服务器模型中,服务器端的 IP 地址和端口号一般是固定的,因此在服务器端的程序中,使用 bind()函数将一个套接字和某个端口绑定到一起,该函数一般只有服务器端的程序调用。<br /><br /><span style=\"color: rgb(255, 0, 204);\">my_addr</span> 指定了 sockfd 将绑定到的本地地址,可以将参数 my_addr 的 sin_addr 设置为 <span style=\"font-weight: bold;\">INADDR_ANY</span> 而不是某个确定的 IP 地址,这样就可以绑定到任何网络接口。对于只有一个 IP 地址的计算机,<span style=\"font-weight: bold;\">INADDR_ANY</span> 对应的就是它的 IP 地址;对于多宿主主机(拥有多张网卡),INADDR_ANY 表示本服务器将处理来自所有网络接口上相应端口的连接请求。<br /><br />函数执行成功返回 0,有错误返回 -1,错误代码存入 errno 中。<br /><br /><span style=\"font-weight: bold;\">函数常见用法如下</span>:<br /></span><span style=\"color: rgb(170, 34, 255); font-weight: bold;\"> </span><span style=\"color: rgb(170, 34, 255); font-weight: bold;\">struct</span> <span style=\"color: rgb(0, 0, 0);\">sockaddr_in</span> <span style=\"color: rgb(0, 0, 0);\">serv_addr</span><br /> <span style=\"color: rgb(0, 0, 0);\">memset</span>(<span style=\"color: rgb(102, 102, 102);\">&</span><span style=\"color: rgb(0, 0, 0);\">serv_addr</span><span style=\"color: rgb(0, 0, 0);\">,</span> <span style=\"color: rgb(102, 102, 102);\">0</span><span style=\"color: rgb(0, 0, 0);\">,</span> <span style=\"color: rgb(170, 34, 255); font-weight: bold;\">sizeof</span>(<span style=\"color: rgb(170, 34, 255); font-weight: bold;\">struct</span> <span style=\"color: rgb(0, 0, 0);\">sockaddr_in</span>));<br /> <span style=\"color: rgb(0, 0, 0);\">serv_adr</span><span style=\"color: rgb(0, 0, 0);\">.</span><span style=\"color: rgb(0, 0, 0);\">sin_family</span> <span style=\"color: rgb(102, 102, 102);\">=</span> <span style=\"color: rgb(0, 0, 0);\">AF_INET</span><br /> <span style=\"color: rgb(0, 0, 0);\">serv_addr</span><span style=\"color: rgb(0, 0, 0);\">.</span><span style=\"color: rgb(0, 0, 0);\">sin_port</span> <span style=\"color: rgb(102, 102, 102);\">=</span> <span style=\"color: rgb(0, 0, 0);\">htons</span>(<span style=\"color: rgb(102, 102, 102);\">80</span>);<br /> <span style=\"color: rgb(0, 0, 0);\">serv_addr</span><span style=\"color: rgb(0, 0, 0);\">.</span><span style=\"color: rgb(0, 0, 0);\">sin_addr</span><span style=\"color: rgb(0, 0, 0);\">.</span><span style=\"color: rgb(0, 0, 0);\">s_addr</span> <span style=\"color: rgb(102, 102, 102);\">=</span> <span style=\"color: rgb(0, 0, 0);\">htonl</span>(<span style=\"color: rgb(0, 0, 0);\">INADDR_ANY</span>);<br /> <br /> <span style=\"color: rgb(170, 34, 255); font-weight: bold;\">if</span> (<span style=\"color: rgb(0, 0, 0);\">bind</span>(<span style=\"color: rgb(0, 0, 0);\">sock_fd</span><span style=\"color: rgb(0, 0, 0);\">,</span> (<span style=\"color: rgb(170, 34, 255); font-weight: bold;\">struct</span> <span style=\"color: rgb(0, 0, 0);\">sockaddr</span> <span style=\"color: rgb(102, 102, 102);\">*</span>)<span style=\"color: rgb(102, 102, 102);\">&</span><span style=\"color: rgb(0, 0, 0);\">serv_addr</span><span style=\"color: rgb(0, 0, 0);\">,</span> <span style=\"color: rgb(170, 34, 255); font-weight: bold;\">sizeof</span>(<span style=\"color: rgb(170, 34, 255); font-weight: bold;\">struct</span> <span style=\"color: rgb(0, 0, 0);\">sockaddr_in</span>) <span style=\"color: rgb(102, 102, 102);\"><</span> <span style=\"color: rgb(102, 102, 102);\">0</span>) <span style=\"color: rgb(0, 0, 0);\">{</span><br /> <span style=\"color: rgb(0, 0, 0);\">perror</span>(<span style=\"color: rgb(187, 68, 68);\">"bind"</span>);<br /> <span style=\"color: rgb(0, 0, 0);\">exit</span>(<span style=\"color: rgb(102, 102, 102);\">1</span>);<br /> <span style=\"color: rgb(0, 0, 0);\">}</span> <br /> |
|