basic web server blocking problem

Hi,
I write basic web server with c++ (some code read on internet.).

I test with apache benchmark tool, 32744 request complated after blocking.
How can design non blocking ?
I using multi thread arthictecture.

Test result http://paste.ubuntu.com/11820177/

Source Code http://paste.ubuntu.com/11820165/

I using debian jessie. I am beginner for c++;

Thank for replies.
You don't close client_fd or delete cstr. And that was just a quick look at the code.

The problem is most likely you've run out of sockets. You really should close those client sockets.

I've copied the code here for posterity.
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <stdlib.h>
#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 <iostream>
#include <fstream>
#include <strings.h>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <sstream>
#include <pthread.h>
using std::string;

string request_handler(char request_buffer[1024]){
	return "seckin.html";
}


string get_response(string filex){

	char *cstr = new char[filex.length() + 1];
	std::strcpy(cstr, filex.c_str());

	std::ifstream ifs (filex.c_str());
	string cikti;
	if (ifs.is_open()) {

   std::ostringstream contents;
    contents << ifs.rdbuf();
    ifs.close();
    cikti = contents.str();


            ifs >> cikti;
	    cikti = "HTTP/1.1 200 OK\r\n"
		    "Content-Type: text/html; charset=UTF-8\r\n\r\n"+cikti;
	    return cikti;
    }else{
	    return "HTTP/1.1 404 Not Found\r\n"
		"Content-Type: text/html; charset=UTF-8\r\n"
		"\r\n"
		"404 Not Found\r\n";
    
    }

}

void *write_socket(int socket, string r){

}


struct thread_data{
   int  thread_id;
   string message;
};

void *PrintHello(void *threadarg)
{
   struct thread_data *my_data;

   my_data = (struct thread_data *) threadarg;


    char *cstr = new char[my_data->message.length() + 1];
    std::strcpy(cstr, my_data->message.c_str());
    write( my_data->thread_id, cstr,my_data->message.length()); /*-1:'\0'*/
    close( my_data->thread_id);
    free(cstr);
    pthread_exit(NULL);
}




int main()
{
  int one = 1, client_fd;
  struct sockaddr_in svr_addr, cli_addr;
  socklen_t sin_len = 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, 1024);
  char buffer[1024];
  int n;
  int rc;
  string f;
  string r;
  pthread_t threads[100000];
  struct thread_data td[100000];
  int i = 0;
  while (1) {
    client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);

    bzero(buffer,1024);
    n = read(client_fd,buffer,1024);
    f = request_handler(buffer);
    printf("%s", buffer);

    r = get_response(f);
 
    if (client_fd == -1) {
      perror("Can't accept");
      continue;
    }
 
    td[i].thread_id = client_fd;
    td[i].message = r;
    rc = pthread_create(&threads[i], NULL,PrintHello, (void *)&td[i]);
    i++;

    if(i>100000-3){
    	i = 0;
    }

  }
}
Last edited on
Registered users can post here. Sign in or register to post.