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- ...
随机推荐
- avalon2学习教程15指令总结
avalon的指令在上一节已经全部介绍完毕,当然有的语焉不详,如ms-js.本节主要总结我对这方面的思考与探索. MVVM的成功很大一语分是来自于其指令,或叫绑定.让操作视图的功能交由形形式式的指令来 ...
- 关于read only file system问题解决方案
切换到超级用户sudo -sadb kill-serveradb rebootadb remount
- win7,win8.1下hosts文件无法修改的快速解决办法
一,找到C:\Windows\System32\drivers\etc,下hosts文件复制一份到桌面: 二,使用notepad++或其他编辑器修改桌面复制出来的那份HOSTS: 三,将修改后的文件复 ...
- 《C++编程规范:101条规则、准则与最佳实践》学习笔记
转载:http://dsqiu.iteye.com/blog/1688217 组织和策略问题 0. 不要为小事斤斤计较.(或者说是:知道什么东西不需要标准化) 无需在多个项目或者整个公司范围内强制实施 ...
- 当前JS文件中加入其他js文件
注意:在html文件导入a.js时,应该把script></script写在/body>后面,否则 document.write()方法有问题. 在载入页面后,浏览器输出流自动关闭: ...
- ViewPager图片轮转带点的
布局页面设置: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:and ...
- apache端口的修改
apache 这个web服务默认在80端口监听...如果你访问一个网站 http://www.baidu.com 则默认一个端口是80 1. 一台机器可以有 1-65535 号端口 2. ...
- SD-WAN技术分析
1.概述 转载须注明来自 SDNLAB并附上本文链接. 本文链接:http://www.sdnlab.com/17810.html 宽带接入以及Internet骨干网容量的持续提升,促使企业WAN技术 ...
- 【.net】关于RegexOptions中的各个枚举值的含义
Member name Description Compiled Specifies that the regular expression is compiled to an assembl ...
- select练习1
1. 查询Student表中的所有记录的Sname.Ssex和Class列. select t.sname ,t.ssex , t.sclass from student t 2. 查询教师所有的单位 ...