Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在tty断开连接后,如何正确关闭连接以强制客户端断开连接?_C_Sockets_Tcp_Tty_Pty - Fatal编程技术网

C 在tty断开连接后,如何正确关闭连接以强制客户端断开连接?

C 在tty断开连接后,如何正确关闭连接以强制客户端断开连接?,c,sockets,tcp,tty,pty,C,Sockets,Tcp,Tty,Pty,我很抱歉这么长的时间,但我怀疑错误可能在错误处理中,并且有一些必要的网络样板 我开始以下单个文件分叉tcp服务器的实现 #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/select.h> #include <sys/socket.h> #include <sys/wait.h&g

我很抱歉这么长的时间,但我怀疑错误可能在错误处理中,并且有一些必要的网络样板

我开始以下单个文件分叉tcp服务器的实现

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <errno.h>
#include <pty.h>

#define PORT 9600
#define BUF_SIZE 128
#define SERVER_BACKLOG 10

void handle_conn(int conn_fd) {
  // Create a TTY / PTY pair and hook it up to the connection.
  int master_fd;
  char slave_name[100];
  pid_t pid = forkpty(&master_fd, slave_name, NULL, NULL);
  if (pid == -1) {
    fprintf(stderr, "Error on first fork: %d %s",  errno, strerror(errno));
    return;
  } else if (pid == 0) { // Child process attached to slave pty
    execl("/bin/bash", "bash", (char*) NULL);
  }

  // Set both fds to be non-blocking
  int flags = fcntl(master_fd, F_GETFL, 0);
  fcntl(master_fd, F_SETFL, flags | O_NONBLOCK);
  flags = fcntl(conn_fd, F_GETFL, 0);
  fcntl(conn_fd, F_SETFL, flags | O_NONBLOCK);

  // Parent continues here, shuffling data back and forth from the socket to
  // the master fd.
  int n;
  char c;
  while(1) {
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(conn_fd, &fds);
    FD_SET(master_fd, &fds);
    int numFds = select((conn_fd > master_fd ? conn_fd : master_fd) + 1, &fds, NULL, NULL, NULL);

    if (numFds == -1) {
      fprintf(stderr, "Error on select: %d %s",  errno, strerror(errno));
      return;
    }
    if (numFds == 0) {
      continue;
    }
    if (FD_ISSET(conn_fd, &fds)) {
      // Read from socket and write to tty.
      while ((n = read(conn_fd, &c, 1)) > 0) {
        write(master_fd, &c, 1);
      }

      if (n == 0) {
        // Reached EOF
        break;
      }
      // Presumed n is -1
      if (errno != EAGAIN || errno != EWOULDBLOCK) {
        fprintf(stderr, "Error reading socket %d %s\n", errno, strerror(errno));
        break;
      }
    }

    if (FD_ISSET(master_fd, &fds)) {
      // Read from tty and write to socket as long as things are still
      // readable.
      while ((n = read(master_fd, &c, 1)) > 0) {
        write(conn_fd, &c, 1);
      }

      if (n == 0) {
        // Reached EOF
        break;
      }

      // Presumed n is -1
      if (errno != EAGAIN && errno != EWOULDBLOCK) {
        fprintf(stderr, "Error reading master FD %d %s\n", errno, strerror(errno));
        break;
      }
    }
  }
  printf("Closing connection [%d]\n", conn_fd);
  int err = close(conn_fd);
  if (err != 0) {
    fprintf(stderr, "Error while closing connection [%d]\n", conn_fd);
  }

  err = close(master_fd);
  if (err != 0) {
    fprintf(stderr, "Error while closing master fd [%d]\n", conn_fd);
  }
}

int main(int argc, char *argv[]) {
  int server_fd = socket(PF_INET, SOCK_STREAM, 0);
  if (server_fd < 0) {
    fprintf(stderr, "Cannot create socket\n");
    return 1;
  }

  struct sockaddr_in server_addr;
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(PORT);
  int ok = inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
  if (!ok) {
    fprintf(stderr, "Cannot parse IP address\n");
    return 1;
  }

  // Allow reuse of port.
  int optval = 1;
  setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));

  int err = bind(server_fd, (struct sockaddr *) &server_addr, sizeof(server_addr));
  if (err != 0) {
    fprintf(stderr, "Cannot bind server: Error %d %s\n", errno, strerror(errno));
    return 1;
  }

  err = listen(server_fd, SERVER_BACKLOG);
  if (err != 0) {
    fprintf(stderr, "Cannot listen\n");
    return 1;
  }

  printf("Server listening on port %d\n", PORT);

  int client_fd;
  struct sockaddr_in client_addr;
  socklen_t client_addr_len;
  while (1) {
    client_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
    if (client_fd < 0) {
      fprintf(stderr, "Cannot accept: Error %d %s\n", errno, strerror(errno));
      return 1;
    }

    printf("New client [%d]\n", client_fd);

    // Using fork instead of threads here to avoid pitfalls involved with
    // mixing forking and threads, since this use case case of a tty host is
    // not expected to involve a great many requests.
    int pid = fork();
    if (pid == -1) {
      fprintf(stderr, "Error on forking to handle new connection: %d %s",  errno, strerror(errno));
      // Server does not need to die here.
      continue;
    } else if (pid == 0) { // Child will handle the connection and then return.
      handle_conn(client_fd);
      return 0;
    }
    // Parent continues to accept
  }
}
此时,我可以在带有
nc
的终端上键入
ls
cat
等命令

但是,当我键入
exit
时,服务器将打印
错误读取主FD 5输入/输出错误\n关闭连接[4]
,并且不再出现错误,因此我假设服务器已成功关闭连接,但客户端将继续挂起,直到我在客户端上按
Control-C


如何修改服务器代码以强制客户端断开连接?我发现了错误!结果是我忘了在父进程中关闭套接字。一旦我这样做了,close()在孩子身上的作用就如预期的那样

对上述代码进行如下简单修改,即可解决此问题

    ...
    // Parent continues to accept
    close(client_fd);

你好我无法将您的错误消息与您的客户端代码联系起来。您能澄清一下吗?@IharobAlAsimi第一行是
fprintf(stderr,“错误读取套接字%d%s\n”,errno,strerror(errno))。后一行是
printf(“关闭连接[%d]\n”,conn\u fd)。此外,我关心的实际“错误”是客户端挂起而不是正确断开连接。哦,这就是服务器代码中的输出。你不觉得你的客户机循环有一个非常奇怪的逻辑吗?我的意思是,似乎您希望在某个特定事件发生时或直到某个特定事件发生时循环。您可以尝试调用吗?如果这是唯一的错误
shutdown()
就会修复它。但事实并非如此,这显然不是唯一的错误。由于您尚未发布所有相关代码,因此任何人都无法进一步发表评论。@Marqueisoflorne我已发布了我正在运行的所有代码。你还想让我发什么?
nc
的源代码?
    ...
    // Parent continues to accept
    close(client_fd);