【发布时间】:2017-08-10 09:20:23
【问题描述】:
我想在 C 语言中为服务器(套接字编程)编写一个脚本,该脚本将处于侦听模式,并从客户端读取 HTTP 请求并对该请求执行一些操作。
例如: 假设我得到的请求如下:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><line2:connectionClearedEvent><cause>normalClearing</cause></line2:connectionClearedEvent></SOAP-ENV:Body></SOAP-ENV:Envelope>
我想检查一下,如果请求有“connectionClearedEvent”字符串,那么计数器加一。
问题:
第一个想法 假设服务器有负载,如果实现将所有来自socket的请求写入文件,那么由于频繁的写入操作,它正在跳过一些请求。
第二个想法 假设服务器有负载,如果实现只是读取传入的请求并搜索该关键字,那么由于频繁的读取操作,它也会跳过一些请求。
当有大量请求到达服务器时,任何人都可以建议一种方法来执行上述操作。
编辑:相同的代码 connection_handler() 将处理每个客户端的连接,但我现在面临的问题是处理文件。
#include<stdio.h>
#include<string.h> //strlen
#include<stdlib.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
#include<pthread.h> //for threading , link with lpthread
//the thread function
void *connection_handler(void *);
int main(int argc , char *argv[])
{
printf("Program name %s\n", argv[0]);
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("Server Port not provided..exiting \n");
// scanf("%s",&argv[1]);
return 1;
}
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
static char* ok_response =
"HTTP/1.1 200 OK\r\n"
"Content-Length: 101\r\n"
"Content-type: text/xml; charset=utf-8\r\n"
"\r\n"
"<html>\n"
" <body>\n"
" <h1>Bad Request</h1>\n"
" <p>This server understand your request.</p>\n"
" </body>\n"
"</html>\n";
static char* body =
"<html>\n"
" <body>\n"
" <h1>Bad Request</h1>\n"
" <p>This server understand your request.</p>\n"
" </body>\n"
"</html>\n";
printf("content length : %d\n",strlen(body));
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
puts(client_message);
write(sock , ok_response , strlen(ok_response));
memset (client_message,'\0',2000);
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
//Free the socket pointer
free(socket_desc);
return 0;
}
【问题讨论】:
-
它是“跳过一些请求”怎么办?错误?客户端错误?服务器错误?症状?题?而且您需要在一段时间内关闭您接受的套接字,而不是仅仅泄漏它们。
-
'new_sock = malloc(1);'太小。请从具有良好代码的站点复制您的代码。
-
'fputs(client_message, fp);'在不保证 NUL 终止的数组上使用需要 NUL 终止的 char 数组的调用。
-
'write(sock , ok_response , strlen(ok_response));'每次 recv() 返回数据时,都会将完整的响应写回客户端,即使它只返回读取了一个字节。这可能导致一个请求有多个响应。
-
'memset (client_message,'\0',2000);'对于第一个 recv() 返回来说为时已晚,如果缓冲区通过 recv() 加载了完整的 2000 个字符,将无济于事,而且无论如何都没有必要,因为你有'read_size',它会告诉你把 NUL 放在哪里,(假设 NUL 是有用的,即您将始终以“文本”形式接收数据,即没有嵌入 NUL)。