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的更多相关文章

  1. Creating A Simple Web Server With Golang

    原文:https://tutorialedge.net/post/golang/creating-simple-web-server-with-golang/ -------------------- ...

  2. Chapter 1: A Simple Web Server

    这算是一篇读书笔记,留着以后复习看看. Web Server又称为Http Server,因为它使用HTTP协议和客户端(一般是各种各样的浏览器)进行通信. 什么是HTTP协议呢? HTTP协议是基于 ...

  3. A Simple Web Server

    介绍 在过去20几年里,网络已经在各个方面改变了我们的生活,但是它的核心却几乎没有什么改变.多数的系统依然遵循着Tim Berners-Lee在上个世纪发布的规则.大多数的web服务器都在用同样的方式 ...

  4. 禁掉Apache web server签名 How to turn off server signature on Apache web server

    有的时候,我们为了从安全角度考虑,防止黑客恶意攻击.我们会隐藏掉server信息,比方,一般我们会发现例如以下信息. 我用的是centos (fedora, RHEL也一样) $ sudo vi /e ...

  5. 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 ...

  6. C#中自己动手创建一个Web Server(非Socket实现)

    目录 介绍 Web Server在Web架构系统中的作用 Web Server与Web网站程序的交互 HTTPListener与Socket两种方式的差异 附带Demo源码概述 Demo效果截图 总结 ...

  7. Nginx负载均衡:分布式/热备Web Server的搭建

    Nginx是一款轻量级的Web server/反向代理server及电子邮件(IMAP/POP3)代理server.并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开 ...

  8. 从零开始的ESP8266探索(1)-使用Server功能搭建Web Server

    https://blog.csdn.net/Naisu_kun/article/details/80398667 文件系统 https://blog.csdn.net/solar_Lan/articl ...

  9. What is the difference between application server and web server?

    http://stackoverflow.com/questions/936197/what-is-the-difference-between-application-server-and-web- ...

随机推荐

  1. 深入理解js——执行上下文

    什么是"执行上下文"?暂且不下定义,先看一段代码: 第一句报错,a未定义,很正常.第二句.第三句输出都是undefined,说明浏览器在执行console.log(a)时,已经知道 ...

  2. hdu 2896 字典树解法

    #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...

  3. Linux内核模块简介

    一. 摘要 这篇文章主要介绍了Linux内核模块的相关概念,以及简单的模块开发过程.主要从模块开发中的常用指令.内核模块程序的结构.模块使用计数以及模块的编译等角度对内核模块进行介绍.在Linux系统 ...

  4. Elasticsearch分布式搜索集群配置

    配置文件位于%ES_HOME%/config/elasticsearch.yml文件中,用Editplus打开它,你便可以进行配置.   所有的配置都可以使用环境变量,例如:node.rack: ${ ...

  5. xcode archive 一直是灰色的

    把配置选择为device才能选build and archive的,模拟器的肯定不能build and anchive

  6. 咏南WEB开发框架(FOR XE10.1 BERLIN)

    咏南WEB开发框架(FOR XE10.1 BERLIN) 1)支持最新的XE10.1 BERLIN开发WEB程序 2)如同开发VCL WIN32程序一样的速度 3)WEB框架通过咏南中间件和数据库打交 ...

  7. javascript学习第三课引用类型object

    主要内容: 1.object 是所有类型的基类 实例化对象: 1. var obj = new Object(); 2. var obj = {}; 设置对象属性和方法: obj.name = 'he ...

  8. Windows server 2003 WINS的配置和使用详解

    NetBios名称概述 网络中的一台计算机可以使用NETBIOS和DNS两种命名方式为其命名,在NETBIOS标准中,使用长度不超 过16个字符的名称来惟一标识每个网络资源,用于标识资源或服务类型.在 ...

  9. ICMP

    (一)ICMP IP是一个尽力的不可靠的协议,IP不能提供差错控制(如果数据在传播过程中出现错误了),这个时候ICMP就起作用了. ICMP提供两个功能:差错的报告,查询. ICMP的ICMP包分为两 ...

  10. 数据库开发及ADO.NET

    大部分数据库都需要数据库服务器才能运行. Catalog(分类)又叫做数据库DataBase Table(表)不同类型的东西放到不同的区域中,将这种区域叫做表. 列(Column)字段Field 主键 ...