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- ...
随机推荐
- RSA加密前端JS加密,后端asp.net解密,报异常
RSA加密前端JS加密,后端asp.net解密,报异常 参考引用:http://www.ohdave.com/rsa/的JS加密库 前端JS加密代码: function GetChangeStr() ...
- 禅道Linux一键安装版
禅道Linux一键安装版 Linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅道.从7.3版本开始,linux一键安装包分为32位和64位两个包,大家 ...
- hibernate执行session.createQuery(hql)时hql若有参数则报错
项目从Jboss换位Tomcat服务器,打开如下Hql都报错: SELECT COUNT(*) FROM SystemUser WHERE STATUS != -1 解决方法:在Lib中加入antlr ...
- RPC应用的java实现(转)
一.RPC介绍 什么是RPC?Remote Procedure Call,远程过程调用.也就是说,调用过程代码并不是在调用者本地运行,而是要实现调用者与被调用者二地之间的连接与通信.比较严格的定义是: ...
- Python全栈---5.1---装饰器
一.装饰器 执行outer函数,将index作为参数传递, 将outer函数的返回值,重新赋值给index 装饰器可以在函数执行前和执行后执行其他的附加功能,这种在代码运行期间动态增加功能的方式,称之 ...
- LVS三种工作方式八种算法
一.集群简介 什么是集群 计算机集群简称集群是一种计算机系统,它通过一组松散集成的计算机软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台计算机.集群系统中的单个计算 ...
- Python:迭代器
迭代器:可以被next()函数调用并不断返回下一个值的对象称为迭代器. 可迭代对象:可以直接作用于for循环的对象. 基本方法:iter()和next() 迭代器创建: 例1: list = ['a' ...
- hdu 5748(LIS) Bellovin
hdu 5748 Peter有一个序列a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an. 定义F(a1,a2,...,an)=(f1,f2,...,fn ...
- 剑指offer题目61-67
面试题61:把二叉树打印成多行 public class Solution { public ArrayList<ArrayList<Integer> > Print(Tree ...
- css3之background-clip与background-origin的区别
background-clip 规定背景的绘制区域. 3 background-origin 规定背景图片的定位区域. 3 background-size 规定背景图片的尺寸. 3 backgroun ...