最简单的web服务器实例
1.新建一个控制台工程,代码如下
static void Main(string[] args)
{
// 定义IP地址
IPAddress address = IPAddress.Loopback;
IPEndPoint endPoint = new IPEndPoint(address, 50000);
// 创建socket对象
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// bind
socket.Bind(endPoint);
// listen
socket.Listen(10);
Console.WriteLine("开始监听,端口号:{0}", endPoint.Port);
while (true)
{
// accept
Socket clientSocket = socket.Accept();
Console.WriteLine("建立了一个连接,对方端点{0}", clientSocket.RemoteEndPoint);
// 开始接受对方的信息,并显示
byte[] buffer = new byte[4096];
int length = clientSocket.Receive(buffer, 4096, SocketFlags.None);
System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
string requestString = utf8.GetString(buffer, 0, length);
Console.WriteLine(requestString);
// 发送响应信息
string statusLine = "HTTP/1.1 200 OK\r\n";
byte[] statusLineBytes = utf8.GetBytes(statusLine); // 状态行
string responseBody = "<html><head><title>response server</title></head><body>hello world!</body></html>";
byte[] responseBodyBytes = utf8.GetBytes(responseBody);// 内容部分
string responseHeader = String.Format("Content-Type: text/html;charset=UTF-8\r\nContent-Length:{0}\r\n",
responseBody.Length);
byte[] responseHeaderBytes = utf8.GetBytes(responseHeader);// 回应头
// 发送消息
clientSocket.Send(statusLineBytes);
clientSocket.Send(responseHeaderBytes);
clientSocket.Send(new byte[] { 13, 10 });
clientSocket.Send(responseBodyBytes);
// 断开与客户端的连接
clientSocket.Close();
if (Console.KeyAvailable)
{
break;
}
}
socket.Close();
}
static void Main(string[] args)
{
// 定义IP地址 www.2cto.com
IPAddress address = IPAddress.Loopback;
IPEndPoint endPoint = new IPEndPoint(address, 50000);
// 创建socket对象
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// bind
socket.Bind(endPoint);
// listen
socket.Listen(10);
Console.WriteLine("开始监听,端口号:{0}", endPoint.Port);
while (true)
{
// accept
Socket clientSocket = socket.Accept();
Console.WriteLine("建立了一个连接,对方端点{0}", clientSocket.RemoteEndPoint);
// 开始接受对方的信息,并显示
byte[] buffer = new byte[4096];
int length = clientSocket.Receive(buffer, 4096, SocketFlags.None);
System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
string requestString = utf8.GetString(buffer, 0, length);
Console.WriteLine(requestString);
// 发送响应信息
string statusLine = "HTTP/1.1 200 OK\r\n";
byte[] statusLineBytes = utf8.GetBytes(statusLine); // 状态行
string responseBody = "<html><head><title>response server</title></head><body>hello world!</body></html>";
byte[] responseBodyBytes = utf8.GetBytes(responseBody);// 内容部分
string responseHeader = String.Format("Content-Type: text/html;charset=UTF-8\r\nContent-Length:{0}\r\n",
responseBody.Length);
byte[] responseHeaderBytes = utf8.GetBytes(responseHeader);// 回应头
// 发送消息
clientSocket.Send(statusLineBytes);
clientSocket.Send(responseHeaderBytes);
clientSocket.Send(new byte[] { 13, 10 });
clientSocket.Send(responseBodyBytes);
// 断开与客户端的连接
clientSocket.Close();
if (Console.KeyAvailable)
{
break;
}
}
socket.Close();
}
2.运行该工程
3.打开浏览器在地址栏中输入http://localhost:50000/ ,用httpwatch可大概了解浏览器与服务器的简要流程
摘自 hi_dzj的专栏
相关新闻>>
- 发表评论
-
- 最新评论 更多>>