Question

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. Why?

Was it helpful?

Solution 2

Try to read with a small delay between recv calls. You are not guaranteed to receive all data in one go. You have to wait for all the data.

OTHER TIPS

You broke a very important rule: Always check the return value of API calls.

In particular, you don't check the return value of write, you just assume it succeeds. In reality, it often only sends part of the message, so you need a loop and error checking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top