HTTP 服务器,接受连接后,我从 recv() 返回 -1

发布于 2024-08-11 20:18:57 字数 2044 浏览 5 评论 0原文

我必须为 C++ 中的类实现 HTTP 服务器,但在接受连接后,recv() 仅返回 -1。我该如何解决这个问题?我在下面发布了我的代码。

int main( int argc, char* argv[] )
{
  // Interpret the command line arguments
  unsigned short port = 8080;
  char* base_directory = NULL;
  base_directory = getcwd(base_directory, 100);

  if ( (argc != 1) && (argc != 3) && (argc != 5) ) {
    cerr << "Usage: " << argv[0];
    cerr << " -p <port number> -d <base directory>" << endl;
    return 1;
  }
  else {
    for (int i = 1; i < argc; ++i) {
      if (strcmp(argv[i], "-p") == 0)
        port = (unsigned short) atoi(argv[++i]);
      else if (strcmp(argv[i], "-d") == 0)
        base_directory = argv[++i];
    }
  }

  // Create TCP socket
  int tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
  if (tcp_sock < 0) {
    cerr << "Unable to create TCP socket." << endl;
    return 2;
  }

  // Create server socket
  sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_port = htons( port );
  server.sin_addr.s_addr = INADDR_ANY;

  // Bind the socket
  if (bind(tcp_sock, (sockaddr*)&server, sizeof(server)) < 0) {
    cerr << "Unable to bind TCP socket." << endl;
    return 3;
  }

  // Listen for a connection request on TCP port
  listen(tcp_sock, 5);

  // Create HTTP_Request object and start a while loop of accepting connections
  char recv_buffer[3000];
  int bytes_recv = 0;
  int recv_len = 0;
  while (true) {
    int acc_tcp_sock = accept(tcp_sock, NULL, NULL);
    if (acc_tcp_sock == -1) {
      cerr << "Unable to open TCP connection with client." << endl;
      return 4;
    }
    do {
      recv_len = recv( tcp_sock, recv_buffer + bytes_recv,
        3000 - bytes_recv, 0 );
      bytes_recv += recv_len;
      cout << bytes_recv << endl;
      break;
    } while (recv_len);
    bytes_recv = 0;
    HTTP_Request* request = HTTP_Request::Parse(recv_buffer, 3000);
    cout << recv_buffer << endl;
  }

  return 0;
}

I have to implement an HTTP server for a class in C++, but after a connection is accepted, recv() just returns -1. How can I fix this? I posted my code below.

int main( int argc, char* argv[] )
{
  // Interpret the command line arguments
  unsigned short port = 8080;
  char* base_directory = NULL;
  base_directory = getcwd(base_directory, 100);

  if ( (argc != 1) && (argc != 3) && (argc != 5) ) {
    cerr << "Usage: " << argv[0];
    cerr << " -p <port number> -d <base directory>" << endl;
    return 1;
  }
  else {
    for (int i = 1; i < argc; ++i) {
      if (strcmp(argv[i], "-p") == 0)
        port = (unsigned short) atoi(argv[++i]);
      else if (strcmp(argv[i], "-d") == 0)
        base_directory = argv[++i];
    }
  }

  // Create TCP socket
  int tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
  if (tcp_sock < 0) {
    cerr << "Unable to create TCP socket." << endl;
    return 2;
  }

  // Create server socket
  sockaddr_in server;
  server.sin_family = AF_INET;
  server.sin_port = htons( port );
  server.sin_addr.s_addr = INADDR_ANY;

  // Bind the socket
  if (bind(tcp_sock, (sockaddr*)&server, sizeof(server)) < 0) {
    cerr << "Unable to bind TCP socket." << endl;
    return 3;
  }

  // Listen for a connection request on TCP port
  listen(tcp_sock, 5);

  // Create HTTP_Request object and start a while loop of accepting connections
  char recv_buffer[3000];
  int bytes_recv = 0;
  int recv_len = 0;
  while (true) {
    int acc_tcp_sock = accept(tcp_sock, NULL, NULL);
    if (acc_tcp_sock == -1) {
      cerr << "Unable to open TCP connection with client." << endl;
      return 4;
    }
    do {
      recv_len = recv( tcp_sock, recv_buffer + bytes_recv,
        3000 - bytes_recv, 0 );
      bytes_recv += recv_len;
      cout << bytes_recv << endl;
      break;
    } while (recv_len);
    bytes_recv = 0;
    HTTP_Request* request = HTTP_Request::Parse(recv_buffer, 3000);
    cout << recv_buffer << endl;
  }

  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄想挽回 2024-08-18 20:18:57

你正在错误的插座上接收。
您正在使用 tcp_sock(这是您的侦听套接字),而不是接受调用返回的套接字。
将其更改为 acc_tcp_sock 就可以了:

  recv_len = recv( acc_tcp_sock, recv_buffer + bytes_recv,
    3000 - bytes_recv, 0 );

you are recv'ing on the wrong socket.
You are using tcp_sock(which is your listening socket), not the socket returned by the accept call.
Change it to acc_tcp_sock and you should be good to go:

  recv_len = recv( acc_tcp_sock, recv_buffer + bytes_recv,
    3000 - bytes_recv, 0 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文