简体   繁体   中英

Function recv from browser socket, but stores nothing in buffer

I need to receive a HTTP request from my browser, when I run localhost:8228 it works fine, I receive the header in the buffer and am able to write it to the console and even echo send it back to the browser. But when I try reading a request from a actual webpage, buffer is empty, it prints nothing.

I have a simple main that looks like this:

int main (int argc, char *argv[]) {
  char buffer[1024*1024] = {0};
  int port_number = 8228;

  if (argc == 1)
    std::cout << "Using default port number, 8228." << std::endl;
  else if (argc == 3) {
    port_number  = atoi(argv[2]);
  } else {
    std::cout << "::Error::" << std::endl;
    std::cout << "Wrong number of arguments." << std::endl;
    exit[0];
  }

  AppSocket app;
  app.Start((int)port_number);
  app.AcceptCall();
  int request_size = app.ReceiveRequest(buffer, sizeof(buffer));

  return 0;
}

My AppSocket functions would be:

void AppSocket::Start(int port) {
  // Create a socket
  listening_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listening_fd == -1) {
    std::cerr << "Could not create a socket." << std::endl;
    exit(-1);
  }
  app_hint.sin_family = AF_INET;
  app_hint.sin_port = htons(port); 
  inet_pton(AF_INET, "127.0.0.1", &app_hint.sin_addr);  
  if (bind(listening_fd, (sockaddr*)&app_hint, sizeof(app_hint))< 0) {
    std::cerr << "Cannot bind to IP/port." << std::endl;
    exit(-2);
  }
  std::cout << "Socket has been bound." << std::endl;

  if (listen(listening_fd, SOMAXCONN) == -1) {
    std::cerr << "Cannot listen." << std::endl;
    exit(-3);
  }

  std::cout << "Listening to port " << port << std::endl;
  std::cout << "Your socket is: " << listening_fd << std::endl;
}

void AppSocket::AcceptCall() {
  client_size = sizeof(client_addr);
  client_fd =
         accept(listening_fd, (sockaddr *)&client_addr, &client_size);

  if (client_fd < 0) {
    std::cerr << "Error connecting to client." << std::endl;
    exit(-4);
  }

  std::cout << inet_ntoa(client_addr.sin_addr)
            << " connected to port "
            << ntohs(client_addr.sin_port) << std::endl;

  close(listening_fd);
}

int AppSocket::ReceiveRequest(char *buffer, int max) {
  std::cout << "Client is: " << client_fd << std::endl;
  memset(buffer, 0, buff_size);    //clear buffer

  int n = recv(client_fd, buffer, buff_size, 0);
  if (n < 0)
  std::cerr << "A connection issue has occured." << std::endl;

  if (n == 0)
    std::cout << "Client disconected." << std::endl;

  std::cout << "recv return " << n << std::endl;
  std::cout << buffer << std::endl;

  return n;
}

When I run and access a webpage I get this:

Using default port number, 8228.
Socket has been bound.
Listening to port 8228
Your socket is: 3
127.0.0.1 connected to port 37522
Client is: 4
recv return 3

None of the questions I've read seem to work for me...

edit: sorry one of the lines in the main code wasn't copied. How can I receive repeatedly? A while loop? I tried that and just kept receiving nothing.

The code works, what was happening is that the firefox proxy settings were wrong, when I ran localhosts it worked fine because it was actually working as the server due to the port it was using but when trying to access a "real" website it didn't. After configuring it correctly it did just what it's supposed to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM