📄 siminetd.cpp
字号:
// siminetd.cpp// program to simulate the way inetd starts a network server// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include "sockutil.h" // listen_socket, etc.#include <stdio.h> // perror#define FD_STDIN 0#define FD_STDOUT 1int main(){ // listen for a connection SOCKET listener = listen_socket(5555); // wait for incoming, and make a new socket for it when it arrives fprintf(stderr, "listening for incoming connection...\n"); SOCKET conn = accept_socket(listener); fprintf(stderr, "received connection, echoing\n"); // replace *my* stdin and stdout with the new connection if (dup2(conn, FD_STDIN) < 0 || dup2(conn, FD_STDOUT) < 0) { perror("dup2"); return 2; } // do what the echo server does int charCount = 0; for (;; charCount++) { // read a character char ch; int len = recv(FD_STDIN, &ch, 1, 0); if (len == 0) { // eof break; } if (len < 0) { perror("recv"); return 2; } // write that character len = send(FD_STDOUT, &ch, 1, 0); if (len != 1) { perror("recv"); } } // close both sockets if (close(FD_STDIN) < 0 || close(FD_STDOUT) < 0) { perror("close"); return 2; } fprintf(stderr, "done, echoed %d characters\n", charCount); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -