Server Develop (九) Simple Web Server
Simple Web Server
web服务器hello world!-----简单的socket通信实现。
HTTP
HTTP是Web浏览器与Web服务器之间通信的标准协议,HTTP指明了客户端如何与服务器建立连接,如果从服务器请求数据,服务器如何响应请求,关闭连接。HTTP是使用TCP/IP协议进行传输数据的,也就是传输层利用TCP进行连接,进行可靠连接的。
详情:点击
请求
一般格式,如:
GET /index.html HTTP/1.0
Accept:text/html,text/plain
User-Agent: Lyn
Host:www.server.com
我们主要需要的信息在第一行,共包括三个字段。
- 第一个字段(GET)为请求动作类型:
- GET代表请求的操作,表示要求服务器返回资源的表示;
- HEAD表示只需要文件的首部;
- PUT表示向服务器上传资源;
- POST主要是向服务器发送表单数据.
- 第二个字段(/index.html),标识服务器上所请求的资源的相对URL,必须要以"/"开头,Web浏览器在发送请求的时候会自动加上服务器的主机名。
- 第三个字段(HTTP/1.0),客户端理解的协议版本
注意:
每一个HTTP请求都要以两个回车换行结束(\r\n\r\n)
GET发送查询字符串主要直接将查询字符串附加到URL后面,如下表示:GET /index.html/user=XXX&Age HTTP/1.0
响应
一般格式,如
HTTP/1.1 200 OK
Date:Mon 15
Server:xxxxxx
Content-Type:text/html;
Content-length:xxx 代表文档的多少个字节,不包含首部字节数<html><head><title>Hello</title></head><body>Test</body></html>
我们需要大概构造这样的一个格式来回复浏览器。
所以,必须包含第一行的状态行、内容的格式、内容的长度和具体的内容。
实例
/*
* A Simple Web Server
*/ #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <error.h> #define PORT 9000 #define EOL "\r\n"
#define EOL_SIZE 2 int recv_new(int fd, char *buffer);
void send_new(int fd, char *msg); int main(int argc, char* argv[])
{
int serv_fd;
int client_fd; int ret; pid_t pid;
struct sockaddr_in serv_addr;
struct sockaddr_in client_addr;
int len = sizeof(struct sockaddr_in); serv_fd = socket(AF_INET, SOCK_STREAM, );
if(serv_fd < ){
perror("create socket fail !\n");
exit();
} bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORT); int on = ;
ret = setsockopt(serv_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));
if(ret < ){
perror("setsockopt fail !\n");
exit();
} ret = bind(serv_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if(ret < ){
perror("bind fail !\n");
exit();
} ret = listen(serv_fd, );
if(ret < ){
perror("listen fail !\n");
exit();
} while(){
client_fd = accept(serv_fd, (struct sockaddr*)&client_addr, &len);
if(client_fd < ){
perror("accept fail !\n");
continue;
}
char buffer[];
recv_new(client_fd, buffer);
printf("recv buffer: %s\n", buffer);
char content[] = "<head><head><title>index.html</title></head><body>hello world!</body>";
char response[];
sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", strlen(content), content);
send(client_fd, response, sizeof(response), );
close(client_fd);
} return ;
} int recv_new(int fd, char *buffer) {
char *p = buffer; // Use of a pointer to the buffer rather than dealing with the buffer directly
int eol_matched = ; // Use to check whether the recieved byte is matched with the buffer byte or not
while (recv(fd, p, , ) != ) // Start receiving 1 byte at a time
{
if (*p == EOL[eol_matched]) // if the byte matches with the first eol byte that is '\r'
{
++eol_matched;
if (eol_matched == EOL_SIZE) // if both the bytes matches with the EOL
{
*(p + - EOL_SIZE) = '\0'; // End the string
return (strlen(buffer)); // Return the bytes recieved
}
} else {
eol_matched = ;
}
p++; // Increment the pointer to receive next byte
}
return ();
}
参考
http://computerdragon.blog.51cto.com/6235984/1191176
http://www.cnblogs.com/lynch_world/archive/2011/04/24/2026413.html
http://css.dzone.com/articles/web-server-c
Server Develop (九) Simple Web Server的更多相关文章
- Creating A Simple Web Server With Golang
原文:https://tutorialedge.net/post/golang/creating-simple-web-server-with-golang/ -------------------- ...
- Chapter 1: A Simple Web Server
这算是一篇读书笔记,留着以后复习看看. Web Server又称为Http Server,因为它使用HTTP协议和客户端(一般是各种各样的浏览器)进行通信. 什么是HTTP协议呢? HTTP协议是基于 ...
- A Simple Web Server
介绍 在过去20几年里,网络已经在各个方面改变了我们的生活,但是它的核心却几乎没有什么改变.多数的系统依然遵循着Tim Berners-Lee在上个世纪发布的规则.大多数的web服务器都在用同样的方式 ...
- 禁掉Apache web server签名 How to turn off server signature on Apache web server
有的时候,我们为了从安全角度考虑,防止黑客恶意攻击.我们会隐藏掉server信息,比方,一般我们会发现例如以下信息. 我用的是centos (fedora, RHEL也一样) $ sudo vi /e ...
- Simple Web API Server in Golang (2)
In this challenge, I tried to implement a simple OAuth2 server basing on Simple Web API Server in [1 ...
- C#中自己动手创建一个Web Server(非Socket实现)
目录 介绍 Web Server在Web架构系统中的作用 Web Server与Web网站程序的交互 HTTPListener与Socket两种方式的差异 附带Demo源码概述 Demo效果截图 总结 ...
- Nginx负载均衡:分布式/热备Web Server的搭建
Nginx是一款轻量级的Web server/反向代理server及电子邮件(IMAP/POP3)代理server.并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开 ...
- 从零开始的ESP8266探索(1)-使用Server功能搭建Web Server
https://blog.csdn.net/Naisu_kun/article/details/80398667 文件系统 https://blog.csdn.net/solar_Lan/articl ...
- What is the difference between application server and web server?
http://stackoverflow.com/questions/936197/what-is-the-difference-between-application-server-and-web- ...
随机推荐
- [翻译]Shape comparison language[转]
link: http://www.cnblogs.com/yhlx125/p/3635623.html Shape comparison language 首先说说我遇到的一个问题: IR ...
- 从零开始搭建Docker Swarm集群
从零开始搭建Docker Swarm集群 检查节点Docker配置 1. 打开Docker配置文件(示例是centos 7)vim /etc/sysconfig/docker2. 添加-H tcp:/ ...
- C#版SQLHelper.cs类
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- stream_copy_to_stream的使用
stream_copy_to_stream - 在数据流之间进行复制操作 例子: <?php //读写方式 $stream = fopen('php://temp', 'w+'); //如果成功 ...
- php字符串赋值到js的坑
很早以前的一个比较坑的问题,今天又遇到了,记录一下,免得以后再次入坑. 把php赋值到view层时,如果不是直接渲染到页面,而是赋值给变量.字符如果有回车或者换行就会出现问题. 示例: <?ph ...
- .net获取IP和MAC地址
获取IP 解决request.UserHostAddress取不到真实IP private string GetClientIP() { string result = HttpConte ...
- hdu 5791 (DP) Two
hdu 5791 Two Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 【Lua】Debian环境下openresty的安装
OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项. OpenResty 通 ...
- TF-IDF 相关概念
概念 TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度. TF-IDF加权的各种形式常被搜索引擎应用,作为文件与用户查询之间相关程度的度量或评级. 词频( ...
- [转载] 散列表(Hash Table)从理论到实用(上)
转载自:白话算法(6) 散列表(Hash Table)从理论到实用(上) 处理实际问题的一般数学方法是,首先提炼出问题的本质元素,然后把它看作一个比现实无限宽广的可能性系统,这个系统中的实质关系可以通 ...