我用C语言实现了基本的Web服务器,它提供来自客户端的GET请求.问题是它不发送HTML页面.
while(1){
if((temp_sock[id] = accept(sockfd, (t_sockaddr*)address, &address_size))==-1){
perror("Accepting Failure: ");
pthread_mutex_lock(&lock);
fprintf(logfile, "Accepting error: %d\n", localfd);
pthread_mutex_unlock(&lock);
exit(1);
}
else{
pthread_mutex_lock(&lock);
fprintf(logfile, "\nAccepting Success");
pthread_mutex_unlock(&lock);
}
if (temp_sock[id]> 0){
pthread_mutex_lock(&lock);
fprintf(logfile, "\nClient is connected");
pthread_mutex_unlock(&lock);
}
recv(temp_sock[id] , buf, buffsize, 0);
pthread_mutex_lock(&lock);
fprintf(logfile, "\nyou recieved : %s\n ", buf);
pthread_mutex_unlock(&lock);
if((resource=analyze(buf))==NULL){ /*analyze - implements basic security and searches files on a disc*/
printf("\nres is NULL");
write(temp_sock[id], "HTTP/1.1 200 OK\n", 16);
write(temp_sock[id], "Content-length: 50\n", 19);
write(temp_sock[id], "Content-Type: text/html\n\n", 25);
write(temp_sock[id], "<html><body><H1>404 …Run Code Online (Sandbox Code Playgroud)