siminetd.cpp

来自「伯克利做的SFTP安全文件传输协议」· C++ 代码 · 共 63 行

CPP
63
字号
// 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 + =
减小字号Ctrl + -
显示快捷键?