簡體   English   中英

通過Windows套接字編程C開發Web服務器

[英]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: 100Bytes Received: 100顯示緩沖區已滿。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM