*/ if (FD_ISSET(s, &;amp;in)) { int new_sock; new_sock = accept(s, NULL, NULL);
//如果监听端口可读,则有新的连接请求进入,通过 accept 接受,此时返回的是已连接 的 socket 我们再回到代码结构4中,继续。 close(sock_listen);return sock_accept; 关闭监听的 sock_listen,然后返回创建的已连接的 socket 此时,创建连接等工作结束,下面要进行读、写
工作。 if (netcat_mode == NETCAT_LISTEN) {if (opt_exec) { ncprint(NCPRINT_VERB2, ncexec(&;amp;listen_sock); } core_readwrite(&;amp;listen_sock, &;amp;stdio_sock); _("Passing control to the specified program")); /* this won't return */
主要的函数是: int core_readwrite(nc_sock_t *nc_main, nc_sock_t *nc_slave)。 传入的是监听 socket 和标准输入、输出 socket。 assert(nc_main &;amp;&;amp; nc_slave); //验证输入的参数是否有效/* set the actual input and output fds and find out the max fd + 1 */ fd_sock = nc_main-&;gt;fd; socket) if //将传入的 nc_main 的 fd 值赋给 fd_sock(fd 为已连接的
(nc_slave-&;gt;domain == PF_UNSPEC) { //传入