zoukankan      html  css  js  c++  java
  • C# http服务器

    Http 服务器搭建

    1、新建一个C#控制台工程

    2、复制以下代码

     1 using System;  
     2 using System.Collections.Generic;  
     3 using System.Linq;  
     4 using System.Text;  
     5 using System.Net;  
     6 using System.Net.Sockets; 
     7 namespace HttpServer
     8 {
     9     class MainClass
    10     {
    11         static void Main(string[] args)  
    12         {  
    13             int len;  
    14             int port = 8080;//端口  
    15             byte[] buf = new byte[1024];  
    16             //IP是本地127.0.0.1  
    17             TcpListener server = new TcpListener(IPAddress.Any, port);  
    18             server.Start();  
    19 
    20             Console.WriteLine("服务运行在[{0}]端口", port);  
    21             while (true)  
    22             {  
    23                 Socket clent = server.AcceptSocket();  
    24                 len = clent.Receive(buf);  
    25                 Console.WriteLine("收到 [{0}] 数据", len);  
    26                 Console.WriteLine(Encoding.ASCII.GetString(buf));  
    27                 string ss = "HTTP/1.0 200 OK
    Content-Type:text/html
    
     Welcome!<br>Now Time:{0}";  
    28                 string ss1 = "<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>My Home</title></head><body><h2>Web Server System</h2></body></html>";  
    29                 DateTime dt = DateTime.Now;  
    30                 string nn = string.Format(ss, dt.ToString());  
    31                 len = clent.Send(Encoding.ASCII.GetBytes(nn));  
    32                 clent.Send(Encoding.ASCII.GetBytes(ss1));  
    33                 Console.WriteLine("发送 [{0}] 数据", len);  
    34                 Console.WriteLine(ss);  
    35                 clent.Close();  
    36                 clent = null;  
    37             }  
    38         }    
    39     }
    40 }

    3、运行控制台

    4、通过cmd ,输入ipconfig

    IPv4 地址就是你的网址

    5、打开自己电脑的浏览器输入127.0.0.1:8080或者 “IPv4 地址‘’:8080

    如果是其他电脑访问,只能用  “IPv4 地址‘’:8080

  • 相关阅读:
    lua pbc
    c保存lua函数
    c语言的lua库编写
    Gridcontrol新增行选中有关问题
    python3 练习题100例 (十五)
    python3 练习题100例 (十四)
    python3 练习题100例 (十三)
    python3 练习题100例 (十二)
    python3 练习题100例 (十一)
    python3 练习题100例 (十)
  • 原文地址:https://www.cnblogs.com/Jason-c/p/7453288.html
Copyright © 2011-2022 走看看