我正在编写一个小型服务器 http,它总是发送他生成的 html 页面(它丢弃了客户端请求)。这是一个更大程序的一部分
我想使用内容编码来最小化本地带宽的使用...
我从尝试这个例子开始:
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <zlib.h>
#define CHUNK 1048576
const char * restrict UrList[] = {"An", "example", "of", "random","ASCII string"};
void reponse(int newsockfd)
{
char * buffer=calloc(sizeof(char),4700);
srand(time(0));
sscanf(buffer,"<!DOCTYPE html>\ // hopfully this is a test as I don't hard code like this in real programs...
<html>\
<head>\
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\
<title color=\"white\">%s</title>\
<body align=\"center\" valign=\"center\" >\
<button onclick=\"launchFullscreen(document.body);\">%s!</button>\
</body>\
</html>\0",UrList[(rand()%3)]);
size_t inputLen = strlen(buffer);
uLong maxOutputLen = compressBound(inputLen);
char * destbuffer=malloc(inputLen);
compress2(destbuffer,&maxOutputLen,buffer,inputLen,Z_BEST_COMPRESSION);
sprintf(buffer,"200 OK\n\
Server: Serveur personalisé ayant pour but d'empêcher les accès au nom de domaines interdits de manière efficace\n\
X-Powered-By: GCC 4.8.1\n\
Cache-Control: must-revalidate, post-check=0, pre-check=0\n\
ETag: \"1390786010\"\n\
Content-Language: fr\n\
Vary: Accept-Encoding\n\
Content-Type: text/html; charset=UTF-8\n\
\n\
%s \0",destbuffer);
write(newsockfd,buffer,inputLen);
free((void *)buffer);
free((void *)destbuffer);
}
void main()
{
struct sockaddr_in6 client_address, server_address;
int clilen,server_handle=socket(PF_INET6, SOCK_STREAM, 0);
if (server_handle < 0)
perror("Unable to create socket.");
server_address.sin6_family = AF_INET6;
server_address.sin6_addr = in6addr_any;
server_address.sin6_port = 0x5000; // port 80
if ( bind(server_handle, (struct sockaddr *)&server_address, sizeof( server_address )) < 0)
perror("Unable to bind.");
listen(server_handle,1310730);
clilen=sizeof(client_address);
while(1)
{
// variables locales
pthread_t parallel;
int newsockfd=accept(server_handle, (struct sockaddr *) &client_address, &clilen);
pthread_create(¶llel,NULL,reponse,(void *)newsockfd);
}
}
这不起作用,因为当我启动时telnet 192.168.0.20 80
没有打印任何内容。
根据GDB中的逐步执行,调用write()时缓冲区具有正确的值
主要思想是我不能直接压缩文件,因为我需要对实际程序中的结果进行内存操作。