//web Server
#include<stdio.h>
#include<stdbool.h>
#include<WinSock2.h>//包含网络编程的头文件,引入静态库
#pragma comment(lib,"ws2_32.lib")
bool isok;
int merror(int redata,int error,char* showinfo)
{
if (redata == error)
{
perror(showinfo);
getchar();
return -1;
}
return 0;
}
void sendhtml(SOCKET s, char* filename);
int main(void)
{
printf("weclome to my WebServer\n");
WSADATA wsdata;
WSAStartup(MAKEWORD(2,2),&wsdata);//确定socket版本信息
//short两个字节2.2
merror(isok,WSAEINVAL,"申请socket失败");
//第一个参数-协议族,决定socket的地址类型
//第二个参数-传输类型,SOCK_STREAM流传输
//第三个参数-指定的传输协议,tcp
SOCKET server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//使用af-inet,ipv4地址
merror(server, INVALID_SOCKET, "创建socker失败");
struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;//和创建的时候一样,使用了Ipv4
seraddr.sin_port = htons(80);//注意网络中的数据和电脑上的数据存储是有区别的,网络是大端存储,pc是小端存储
seraddr.sin_addr.s_addr = INADDR_ANY;//监听任意的地址
isok = bind(server,&seraddr,sizeof(seraddr));
merror(isok, SOCKET_ERROR, "绑定失败...\n");
isok = listen(server, 5);
merror(isok, SOCKET_ERROR, "监听失败...\n");
struct sockaddr_in claddr;
int cllen = sizeof(claddr);
while (1)
{
SOCKET client = accept(server, &claddr, &cllen);//谁连进来了,发了多少数据
merror(client, INVALID_SOCKET, "连接失败...\n");
char revdata[1024] = "";
recv(client,revdata,1024,0);
printf("%s 共接收到%d字节数据\n", revdata,strlen(revdata));
//如果下面这两行显示文字,测试发送成功。
char sendata[1024] = "<h1 style =\" color:pink;\">hello,i'm sb</h1>";
send(client,sendata,strlen(sendata),0);
char* filename = "/";//填入文件名称xxx.html
void sendhtml(client, filenama);
closesocket(client);
}
closesocket(server);
WSACleanup();
getchar();
return 0;
}
//打开文件-网页
//将文件放入项目文件夹下
void sendhtml(SOCKET s, char* filename)
{
FILE* pfile = fopen(filename, "r");
if (pfile == NULL)
{
printf("文件打开失败");
return;
}
char temp[1024] = "";
do
{
fgets(temp, 1024, pfile);
send(s, temp, strlen(temp), 0);
} while (!feof(pfile));
}