Browse Source

添加tcp&udp

master
sc 1 year ago
parent
commit
785bacc182
  1. 6
      MainWindow.xaml.cs
  2. 120
      UserClass/HttpSERVER.cs
  3. 18
      UserClass/TCPUDPServer.cs
  4. 2
      formula_manage.csproj

6
MainWindow.xaml.cs

@ -75,7 +75,7 @@ namespace formula_manage
InitializeComponent();
this.Closing += Window_Closing; //添加窗口关闭事件
UserClass.HttpSERVER.Start(); //开启网路通讯
//UserClass.HttpSERVER.Start(); //开启网路通讯
USER.Text = App.USER_Purview;
RRODUCTdataTable.Columns.Add("ID", typeof(int));
@ -265,8 +265,8 @@ namespace formula_manage
System.IO.DirectoryInfo log = new System.IO.DirectoryInfo(@logpath);//生成日志文件目录
FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write);
StreamWriter wr = new StreamWriter(fs);//创建文件
wr.WriteLine(Log_time + "HttpSERVER_STOP");
UserClass.HttpSERVER.Stop(); //停止网络通信
// wr.WriteLine(Log_time + "HttpSERVER_STOP");
// UserClass.HttpSERVER.Stop(); //停止网络通信
wr.WriteLine(Log_time + "FORMULA_STOP");
wr.Close();
}

120
UserClass/HttpSERVER.cs

@ -1,120 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Net;
namespace formula_manage.UserClass
{
public class HttpSERVER
{
private static bool isExcute = true;
private static HttpListener listener = new HttpListener();
public static void Start()
{
System.Threading.ThreadPool.QueueUserWorkItem(w => Excute());//单独开启一个线程执行监听消息
}
private static void Excute()
{
if (HttpListener.IsSupported)
{
if (!listener.IsListening)
{
listener.Prefixes.Add("http://127.0.0.1:8888/"); //添加需要监听的url
listener.Start(); //开始监听端口,接收客户端请求
}
while (isExcute)
{
try
{
//阻塞主函数至接收到一个客户端请求为止 等待请求
HttpListenerContext context = listener.GetContext();
//解析请求
HttpListenerRequest request = context.Request;
//构造响应
HttpListenerResponse response = context.Response;
//http请求方式:get,post等等
string httpMethod = request.HttpMethod?.ToLower();
string rawUrl = request.RawUrl;//不包括IP和端口
var Url = request.Url;//全路径
if (httpMethod == "get")
{
//获取查询参数
var queryString = request.QueryString;
// 请求接口 test/method?id=5
//键值对方式 string val = queryString["key"];
//string val = queryString["id"];val的值是5
}
else if (httpMethod == "post")
{
//请求接口 test/postMethod 格式是json
//{
// "id":5,
// "name":"zs"
//}
//获取pots请求的请求体,json格式的字符串
var reader = new StreamReader(request.InputStream);
var questBody = reader.ReadToEnd();
if (!string.IsNullOrEmpty(rawUrl))
{
//这里可以直接用switch这个只是demo
if (rawUrl.Equals("/server/uploadgenconnected", StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrEmpty(questBody))
{
UploadGenConnectedModel model = JsonConvert.DeserializeObject<UploadGenConnectedModel>(questBody);
if (model != null)
{
// To Do
}
}
}
}
}
var responseString = string.Empty;
// 执行其他业务逻辑
//*****************
responseString = JsonConvert.SerializeObject(new { code = 1, msg = "发送成功" });
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
//对客户端输出相应信息.
response.ContentLength64 = buffer.Length;
//response.StatusCode = 200;
//response.ContentType = "text/plain";
//发送响应
using (System.IO.Stream output = response.OutputStream)
{
output.Write(buffer, 0, buffer.Length);
}
}
catch (Exception exceotion)
{
//Logger.Error("处理消息异常", exceotion);
string str = exceotion.Message;
}
}
}
else
{
//Logger.Info("系统不支持HttpListener");
}
}
public static void Stop()
{
isExcute = false;
if (listener.IsListening)
listener.Stop();
}
}
public class UploadGenConnectedModel
{
public bool GenConnected { get; set; }
}
}

18
UserClass/TCPUDPServer.cs

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Net;
namespace formula_manage.UserClass
{
public class TCPUDPServer
{
}
}

2
formula_manage.csproj

@ -266,7 +266,7 @@
<Compile Include="UserClass\CRCcheck16.cs" />
<Compile Include="UserClass\DataGridHelper.cs" />
<Compile Include="UserClass\HardwareSN.cs" />
<Compile Include="UserClass\HttpSERVER.cs" />
<Compile Include="UserClass\TCPUDPServer.cs" />
<Compile Include="UserClass\IniFile.cs" />
<Compile Include="UserClass\Log.cs" />
<Compile Include="UserClass\MD5check.cs" />

Loading…
Cancel
Save