[英]Developing a Web Server by Windows Socket Programming C
我是C语言的初学者,一个学校的作业要求我编写一个Web服务器,该服务器可以接受来自Web浏览器(即IE,firefox ...等)的请求,并给出适当的响应。 我使用Visual Studio C ++ 2010 Express编写代码。
这是我的代码:
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEFAULT_PORT 5019
int main(int argc, char **argv){
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<doctype !html><html><head><title>Hello World</title></head>"
"<body><h1>Hello world!</h1></body></html>\r\n";
char szBuff[100];
int msg_len;
int addr_len;
struct sockaddr_in local, client_addr;
SOCKET sock, msg_sock;
WSADATA wsaData;
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR){
// stderr: standard error are printed to the screen.
fprintf(stderr, "WSAStartup failed with error %d\n", WSAGetLastError());
//WSACleanup function terminates use of the Windows Sockets DLL.
WSACleanup();
return -1;
}
// Fill in the address structure
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(DEFAULT_PORT);
sock = socket(AF_INET,SOCK_STREAM, 0); //TCp socket
if (sock == INVALID_SOCKET){
fprintf(stderr, "socket() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
if (bind(sock, (struct sockaddr *)&local, sizeof(local)) == SOCKET_ERROR){
fprintf(stderr, "bind() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
//waiting the connection
if (listen(sock, 5) == SOCKET_ERROR){
fprintf(stderr, "listen() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("Waiting the connection........\n");
while(1){
addr_len = sizeof(client_addr);
msg_sock = accept(sock, (struct sockaddr*)&client_addr, &addr_len);
if (msg_sock == INVALID_SOCKET){
fprintf(stderr, "accept() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
if (msg_sock == -1){
perror("Unable to accept connection.");
continue;
}
printf("accepted connection from %s, port %d\n",
inet_ntoa(client_addr.sin_addr),
htons(client_addr.sin_port));
msg_len = recv(msg_sock, szBuff, sizeof(szBuff), 0);
printf("Bytes Received: %d, message: %s from %s\n", msg_len, szBuff, inet_ntoa (client_addr.sin_addr));
msg_len = send(msg_sock, response, sizeof(response)-1 , 0);
if (msg_len == 0){
printf("Client closed connection\n");
closesocket(msg_sock);
return -1;
}
if (msg_len == SOCKET_ERROR){
fprintf(stderr, "recv() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
if (msg_len == 0){
printf("Client closed connection\n");
closesocket(msg_sock);
return -1;
}
closesocket(msg_sock);
}
WSACleanup();
}
该项目不需要我的Web服务器可以从其他计算机上进行响应,因此我只在运行Web服务器的同一台计算机上使用IE。 我的Web服务器似乎可以接收来自浏览器的请求,但是Web浏览器无法显示任何内容。 这是我的Web服务器从命令行输出的内容:
Waiting for connection......
accepted connection from 127.0.0.1, port 53108
Bytes Received: 100, message: GET / HTTP/1.1
Accept-Language: zh-HK
User-Agent: M烫烫烫烫HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
<doctype !html><html><head><title>Hello World</title></head><body><h1>Hello world!</h1></body></html>
from 127.0.0.1
服务器响应浏览器时,似乎会出现一些随机字符。 发生了什么事,我该如何解决? 顺便说一下,我正在使用中文操作系统(Window7)。
缓冲区的末尾没有空字符,因此printf
写出过去的内容。
Bytes Received: 100
的Bytes Received: 100
显示缓冲区已满。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.