C/C++ 實(shí)現(xiàn)簡易HTTP服務(wù)器的示例
更新時(shí)間:2020年10月19日 09:57:44 作者:lyshark
這篇文章主要介紹了C/C++ 實(shí)現(xiàn)簡易HTTP服務(wù)器的示例,幫助大家更好的理解和學(xué)習(xí)C/C++編程,感興趣的朋友可以了解下
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 2048
#define BUF_SMALL 100
void Send_404(SOCKET sock)
{
char Protocol[] = "HTTP/1.0 404 Bad Request\r\n";
send(sock, Protocol, strlen(Protocol),0);
closesocket(sock);
}
void SendData(SOCKET sock, char *filename)
{
char Protocol[] = "HTTP/1.1 200 OK\r\n";
char ServerType[] = "Server:MyWebServer\r\n";
char ContentLen[] = "Content-length:2048\r\n";
char ContentType[] = "Content-type:text/html\r\n";
char buffer[BUF_SIZE] = {0};
FILE *fp;
if ((fp = fopen(filename, "r")) != NULL)
{
// 傳輸頭數(shù)據(jù)
send(sock, Protocol, strlen(Protocol), 0);
send(sock, ServerType, strlen(ServerType), 0);
send(sock, ContentLen, strlen(ContentLen), 0);
send(sock, ContentType, strlen(ContentType), 0);
// 傳輸數(shù)據(jù)
while (fgets(buffer, BUF_SIZE, fp) != NULL)
send(sock, buffer, strlen(buffer), 0);
closesocket(sock);
}
}
unsigned WINAPI RequestHandle(void *argv)
{
SOCKET hClntSock = (SOCKET)argv;
char Buffer[BUF_SIZE] = { 0 };
char Method[BUF_SMALL] = { 0 };
char FileName[BUF_SMALL] = { 0 };
recv(hClntSock, Buffer, BUF_SIZE, 0);
// 尋找HTTP請求頭 如果不為空則繼續(xù)
if (strstr(Buffer, "HTTP/1") != NULL)
{
// 接著判斷是否為GET請求方式
strcpy(Method, strtok(Buffer, "/"));
if (strcmp(Method, "GET") != 0)
{
strcpy(FileName, strtok(0, "/"));
printf("請求方式: %s 請求文件: %s \n", Method,FileName);
SendData(hClntSock, FileName);
closesocket(hClntSock);
return 0;
}
}
Send_404(hClntSock);
closesocket(hClntSock);
return -1;
}
int main(int argc,char * argv[])
{
WSADATA wsaData;
SOCKET ServerSock, ClientSock;
SOCKADDR_IN ServerAddr, ClientAddr;
WSAStartup(MAKEWORD(2, 2), &wsaData);
ServerSock = socket(PF_INET, SOCK_STREAM, 0);
memset(&ServerAddr, 0, sizeof(ServerAddr));
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
ServerAddr.sin_port = htons(80);
bind(ServerSock, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr));
listen(ServerSock, 10);
while (1)
{
HANDLE hThread;
DWORD dwThreadID;
int ClientAddrSize;
ClientAddrSize = sizeof(ClientAddr);
ClientSock = accept(ServerSock, (SOCKADDR *)&ClientAddr, &ClientAddrSize);
printf("請求客戶端 IP: %s --> 端口: %d \n", inet_ntoa(ClientAddr.sin_addr), ntohs(ClientAddr.sin_port));
hThread = (HANDLE)_beginthreadex(0, 0, RequestHandle, (void *)ClientSock, 0, (unsigned *)&dwThreadID);
}
closesocket(ServerSock);
WSACleanup();
return 0;
}
以上就是C/C++ 實(shí)現(xiàn)簡易HTTP服務(wù)器的示例的詳細(xì)內(nèi)容,更多關(guān)于C/C++ 實(shí)現(xiàn)簡易HTTP服務(wù)器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++中臨時(shí)對象的常見產(chǎn)生情況及其解決的方案
這篇文章主要是探討常見的臨時(shí)對象產(chǎn)生的情況,及其如何避免和解決這種臨時(shí)對象產(chǎn)生的方式。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
C++語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu) 串的基本操作實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
C++缺省參數(shù)與重載函數(shù)(超詳細(xì)!)
無論使用什么語言函數(shù)都是代碼段中必不可少的部分,因此我們有必要深入認(rèn)識一下C++中函數(shù)的兩種特殊用法,缺省參數(shù),函數(shù)重載,這篇文章主要給大家介紹了關(guān)于C++缺省參數(shù)與重載函數(shù)的相關(guān)資料,需要的朋友可以參考下2024-06-06
C++?std::copy與memcpy區(qū)別小結(jié)
本文主要介紹了C++?std::copy與memcpy區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05

