http服务端接收客户端的数据请求基于C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;

using System.Reflection;
using System.Net.Sockets;
using System.Web;


using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
 public void Start()
        {
            if (isRunning)
                return;
            int len = 0;//端口  
            byte[] buf = new byte[1024];
            //创建服务端Socket
            Socket serverSocket = new Socket(AddressFamily.InterNetwork,            SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(IPAddress.Parse(ServerIP), ServerPort));
            serverSocket.Listen(10);
            isRunning = true;

            //输出服务器状态
            Console.WriteLine("Sever is running at http://{0}:{1}/.", ServerIP, ServerPort);

            //连接客户端
           
                Socket clent = serverSocket.Accept();                
                len = clent.Receive(buf);
                Console.WriteLine("收到 [{0}] 数据", len);
                Console.WriteLine(Encoding.ASCII.GetString(buf));
            string ss = "HTTP/1.0 200 OK\nContent-Type:text/html\n\n Welcome!<br>Now Time:{0}";
            string ss1 = "nihao";    
                            DateTime dt = DateTime.Now;
                            string nn = string.Format(ss, dt.ToString());
                            len = clent.Send(Encoding.ASCII.GetBytes(nn));
                            clent.Send(Encoding.ASCII.GetBytes(ss1));
                            Console.WriteLine("发送 [{0}] 数据", len);
                            Console.WriteLine(ss);
                            clent.Close();
           
        }

HTTP/1.0 200 OK\nContent-Type:text/html\n\n
HTTP/1.0是协议版本
200是http statusCode(状态码)成功
nContent-Type 是header关键字-数据类型

使用post-man工具测试
使用post方式发送数据
得到服务器的返回结果
如下图标号3的结果
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值