只是最简单的Web服务器
代码语言:javascript复制using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace 简单的Web服务器2
{ //改进服务器使的访问的之定义就返回请求的页面
//从请求中报文中读取,第一行信息就行了
class Program
{
static void Main(string[] args)
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//宿舍的大妈
serverSocket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 8089));//我这个程序监听的是8080端口
serverSocket.Listen(10);//启动监听
while (true)//男生去女生宿舍楼找女生
{
Console.WriteLine("Web服务器等待请求");
string firstLine;
Socket socket = serverSocket.Accept();//等待有人请求,这个是男女生通讯的通道
Console.WriteLine("请求来了");
using (NetworkStream netStream = new NetworkStream(socket))//接收套接字
using (StreamReader reader = new StreamReader(netStream))
{
//从浏览器中读取请求的套接字
firstLine = reader.ReadLine();//读取GET /1.html HTTP/1.1
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
if (line.Length <= 0)//浏览器发送了“write.Writerline()”//表示结束
{
break;//遇到空行之后代表客户端发送结束,因此开始要给浏览器返回内容了
}
}
string[] strs = firstLine.Split(' ');//以空格切割字符创
string url = strs[1];//拿到字符创切割的第二个部分 /1.html
using (NetworkStream nerStream = new NetworkStream(socket))
using (StreamWriter writer = new StreamWriter(nerStream))//相应报文写入套接字
{
string filePath = @"f:www" url;
Console.WriteLine("filePath" filePath);
if (File.Exists(filePath))
{
writer.WriteLine("HTTP/1.1 200 OK");
writer.WriteLine();//Http协议规定:服务器返回给浏览器的报文头和正文之间用一个空行分割
string html = File.ReadAllText(filePath);
Console.WriteLine(html);
}
else
{
writer.WriteLine("HTTP/1.1 404 NOT FOUND");
writer.WriteLine();
writer.WriteLine("NO That Html File");
}
}
socket.Disconnect(false);//男女生访问关闭 -----//服务器端返回完毕内容就断开了连接
}
}
}
}
}
第一个Web服务器,访问本机127.0.0.1
代码语言:javascript复制using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace 简单的Web服务器
{
class Program
{
static void Main(string[] args)
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//宿舍的大妈
serverSocket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 8081));//我这个程序监听的是8080端口
serverSocket.Listen(10);//启动监听
while (true)//男生去女生宿舍楼找女生
{
Console.WriteLine("等待请求");
using (Socket socket = serverSocket.Accept())//等待有人请求,这个是男女生通讯的通道
{
using(NetworkStream netStream=new NetworkStream(socket))//接收套接字
using(StreamReader reader=new StreamReader(netStream))
{
//从浏览器中读取请求的套接字
string line;
while ((line=reader.ReadLine())!=null)
{
Console.WriteLine(line);
if (line.Length<=0)//浏览器发送了“write.Writerline()”//表示结束
{
break;//遇到空行之后代表客户端发送结束,因此开始要给浏览器返回内容了
}
}
}
using(NetworkStream nerStream=new NetworkStream(socket))
using (StreamWriter writer = new StreamWriter(nerStream))//相应报文写入套接字
{
writer.WriteLine("HTTP/1.1 200 OK");
writer.WriteLine();//Http协议规定:服务器返回给浏览器的报文头和正文之间用一个空行分割
writer.WriteLine("Welcome to connect to me !");
}
socket.Disconnect(false);//男女生访问关闭 -----//服务器端返回完毕内容就断开了连接
}
}
}
}
}