百度网站推广外包个人电脑 wordpress
百度网站推广外包,个人电脑 wordpress,下载app到手机,建站公司前景这里的“通讯”加上了引号#xff0c;是因为实际上所有的socket都有通讯的功能#xff0c;只是在我们的例子中#xff0c;之前那个socket只负责listen#xff0c;而这个socket负责接受信息并echo回去。我们现看看这个函数#xff1a;boolTcpServer::isAccept() { unsi…这里的“通讯”加上了引号是因为实际上所有的socket都有通讯的功能只是在我们的例子中之前那个socket只负责listen而这个socket负责接受信息并echo回去。我们现看看这个函数bool TcpServer::isAccept() { unsigned int clntAddrLen sizeof(clntAddr); if ( (communicationSock accept(listenSock, (sockaddr*)clntAddr, clntAddrLen)) 0 ) { return false; } else { std::cout Client(IP: inet_ntoa(clntAddr.sin_addr) ) connected.\n; return true; } }用accept()创建新的socket在我们的例子中communicationSock实际上是用函数accept()创建的。int accept(int socket, struct sockaddr* clientAddress, unsigned int* addressLength);在Linux中的实现为/* Await a connection on socket FD. When a connection arrives, open a new socket to communicate with it, set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting peer and *ADDR_LEN to the addresss actual length, and return the new sockets descriptor, or -1 for errors. This function is a cancellation point and therefore not marked with __THROW. */ extern int accept (int __fd, __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len);这个函数实际上起着构造socket作用的仅仅只有第一个参数另外还有一个不在这个函数内表现出来的因素后面会讨论到后面两个指针都有副作用在socket创建后会将客户端sockaddr的数据以及结构体的大小传回。当程序调用accept()的时候程序有可能就停下来等accept()的结果。这就是我们前一小节说到的block阻塞。这如同我们调用std::cin的时候系统会等待输入直到回车一样。accept()是一个有可能引起block的函数。请注意我说的是“有可能”这是因为accept()的block与否实际上决定与第一个参数socket的属性。这个文件描述符如果是block的accept()就block否则就不block。默认情况下socket的属性是“可读可写”并且是阻塞的。所以我们不修改socket属性的时候accept()是阻塞的。accept()的另一面connect()accept()只是在server端被动的等待它所响应的是client端connect()函数int connect(int socket, struct sockaddr* foreignAddress, unsigned int addressLength);虽然我们这里不打算详细说明这个client端的函数但是我们可以看出来这个函数与之前我们介绍的bind()有几分相似特别在Linux的实现中/* Open a connection on socket FD to peer at ADDR (which LEN bytes long). For connectionless socket types, just set the default address to send to and the only address from which to accept transmissions. Return 0 on success, -1 for errors. This function is a cancellation point and therefore not marked with __THROW. */ extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);connect() 也使用了const的sockaddr只不过是远程电脑上的而非bind()的本机。accept()在server端表面上是通过listen socket创建了新的socket实际上这种行为是在接受对方客户机程序中connect()函数的请求后发生的。综合起看被创建的新socket实际上包含了listen socket的信息以及客户端connect()请求中所包含的信息——客户端的sockaddr地址。新socket与sockaddr的关系accept()创建的新socket我们例子中的communicationSock这里我们简单用newSock来带指首先包含了listen socket的信息所以newSock具有本机sockaddr的信息其次因为它响应于client端connect()函数的请求所以它还包含了clinet端sockaddr的信息。我们说过stream流形式的TCP协议实际上是建立起一个“可来可去”的通道。用于listen的通道远程机的目标地址是不确定的但是newSock却是有指定的本机地址和远程机地址所以这个socket才是我们真正用于TCP“通讯”的socket。inet_ntoa()#include arpa/inet.h /* Convert Internet number in IN to ASCII representation. The return value is a pointer to an internal array containing the string. */ extern char *inet_ntoa (struct in_addr __in) __THROW;对于这个函数我们可以作为一种将IP地址由in_addr结构转换为可读的ASCII形式的固定用法。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/90109.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!