Small Web server, failure when uploading files.

Hi!

I've been trying to code a small web server. And everything works quite good until now, when I tested to send a file through my browser.

When I'm sending a file through my browser it seems like the variable 'buffer' runs out of memory and causes something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int portno = 80; //Default port is 80


int main(int argc,char *argv[]) {
        int sockfd,u_sockfd;    //SOCKETS
        socklen_t client_len;   //CLIENT LENGTH
        char buffer[1024];
        struct sockaddr_in serv_addr, cli_addr; //STRUCTS



        if(argc > 1)
                portno = atoi(argv[1]); //SET USER DEFINED PORT


        //SET UP SERVER
        sockfd = socket(AF_INET, SOCK_STREAM, 0);       //CREATE SOCKET
        bzero((char *) &serv_addr, sizeof(serv_addr)); //CLEAN, JUST IN CASE
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = INADDR_ANY;
        serv_addr.sin_port = htons(portno);             //htons() - Host to short
        if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { //BIND SERVER TO ADDRESS
                perror("Unable to bind address!\n");
                exit(1);
        }else
                printf("LARW Server running on port %i\n",portno);
        listen(sockfd,5);
        //SET UP SRV END

        client_len = sizeof(cli_addr);
        //RUN SERVER
        while(1) {
                u_sockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&client_len); //CLIENT SOCKET
                bzero(buffer,sizeof(buffer)); //CLEAR

                read(u_sockfd,buffer,sizeof(buffer)); //READ REQUEST

                printf("I recived:\n%s\n",buffer);

                //RETURN
                std::string ret= "HTTP/1.1 200 OK\nServer: LARW\nContent-Type: text/html\n\n";
                ret += "<html>\
<head><title>LARW is up and running</title></head>\
<body>\
<h2>Hey it seems like it works</h2>\
Congratulations, your LARW server is up and running!\
<form action='' method='post' enctype='multipart/form-data'>\
<input type='file' name='my_file'>\
<input type='submit'>\
</form>\
</body></html>";
                write(u_sockfd,ret.c_str(),strlen(ret.c_str()));

                close(u_sockfd);
        }
        close(sockfd);
        //END SERVER
        return 0;
}


I've tried to give 'buffer' more memory but with little success. 2 000 000 was apparently not enough...

Any tips on how I could solve this?

EDIT: It seems like there is an abort character in the file i'm trying to upload. It's a jpg.

//ozzle
Last edited on
read(u_sockfd,buffer,sizeof(buffer)); //READ REQUEST This doesn't necessarily read the requested amount. If fact, it's only likely to do so on a direct connection with no routers. You need to check the return code and read the remaining fragments.

The same goes for the write.

Being a stream oriented protocol, you need to inset your own end sequence, for HTML, that's q blank line. You don't respect that sequence in your read or write.
How do I check the return code and read the remaining fragments?
Thanks!
Topic archived. No new replies allowed.