简体   繁体   English

C ++套接字不能接收超过21845个字节

[英]C++ sockets can't receive more than 21845 bytes

I have simple webserver: 我有简单的网络服务器:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>
#include <string.h>
#include <boost/regex.hpp>
#include <boost/thread.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string.hpp>
#include "print_r.h"
#include "Pop.h"
#include "Headers.h"
//#include<thread>


char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Connection: keep-alive\r\n"
"Server: michal\r\n"
"Set-Cookie: nazwa=test\r\n"
"Date: Mon, 24 Feb 2014 11:39:26 GMT\r\n"
"Vary: Accept-Encoding\r\n\r\n"
"<html><body><h1>It works!</h1>"
"<p>This is the default web page for this server.</p>"
"<p>The web server software is running but no content has been added, yet.</p>"
"<form method='post' action='/' enctype='multipart/form-data' ><input type='file' name='pliczek1'/><input type='submit' name='sub' value='sender' /><input type='checkbox' name='add[]' value='100001_used' ><input type='hidden' name='hidd' value='testowy hiddenik' /><input type='checkbox' name='add[]' value='100002_used' ><textarea name='txtform'>tekstowe poleąś</textarea></form>"
"</body></html>\r\n\r\n";
void app(int client_fd)
{
    int buffSize = 512;

    char buff[buffSize];
    std::string headers = "";
    int i = 0;
    int npos = 0;
    int a = 0;
    while (i = recv(client_fd, buff, buffSize, 0))
    {
       std::cout << i << "\n";
       bzero(buff, buffSize);
       a++;
       if (i < buffSize) break;
    }

    write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
    close(client_fd);
}

int main()
{
  int one = 1, client_fd;
  struct sockaddr_in svr_addr, cli_addr;
  socklen_t sin_len = sizeof(cli_addr);
    std::cout << sizeof(cli_addr);
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    err(1, "can't open socket");

  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

  int port = 8080;
  svr_addr.sin_family = AF_INET;
  svr_addr.sin_addr.s_addr = INADDR_ANY;
  svr_addr.sin_port = htons(port);

  if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
    close(sock);
    err(1, "Can't bind");
  }

  listen(sock, 5);

  while (1) {
    client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);
    std::cout << "\n\n+++++++++++++  NEW CLIENT +++++++++++++++\n\n\n";
    if (client_fd == -1) {
      std::cout << ("Can't accept\n");
      break;
    }
    app(client_fd);

    }
}

I am trying to send an attachment via the web browser. 我正在尝试通过网络浏览器发送附件。 With files smaller than 21kB it works fine but I can't send more than 21845 bytes. 对于小于21kB的文件,它可以正常工作,但我发送的字节数不能超过21845字节。 Why? 为什么?

You broke a very important rule: Always check the return value of API calls. 您打破了一条非常重要的规则: 始终检查API调用的返回值。

In particular, you don't check the return value of write , you just assume it succeeds. 特别是,您无需检查write的返回值,只需假设它成功即可。 In reality, it often only sends part of the message, so you need a loop and error checking. 实际上,它通常仅发送部分消息,因此您需要进行循环和错误检查。

Try to read with a small delay between recv calls. 尝试阅读两次recv调用之间的延迟。 You are not guaranteed to receive all data in one go. 您不能保证一口气收到所有数据。 You have to wait for all the data. 您必须等待所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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