187 lines
6.4 KiB
C#
187 lines
6.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Net;
|
||
using System.Net.Sockets;
|
||
using System.Threading;
|
||
|
||
namespace Nanji_Island
|
||
{
|
||
class Server
|
||
{
|
||
/******************服务端************************/
|
||
//定义Socket对象
|
||
public static Socket serverSocket;
|
||
//定义监听线程
|
||
public static Thread listenThread;
|
||
//定义接受客户端数据线程
|
||
public static Thread threadReceive;
|
||
//定义双方通信
|
||
public static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
public static String str;
|
||
|
||
|
||
|
||
//开启服务端监听线程
|
||
public static void open_server(string ipaddress, string port)
|
||
{
|
||
IPAddress ip = IPAddress.Parse(ipaddress.Trim());
|
||
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
try
|
||
{
|
||
//绑定ip和端口号
|
||
serverSocket.Bind(new IPEndPoint(ip, Convert.ToInt32(port.Trim())));
|
||
//设置最多10个排队连接请求
|
||
serverSocket.Listen(10);
|
||
//开启线程循环监听
|
||
listenThread = new Thread(ListenClientConnect);
|
||
listenThread.Start();
|
||
Console.WriteLine("监听开启");
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
Console.WriteLine(err.Message);
|
||
}
|
||
}
|
||
|
||
//监听
|
||
private static void ListenClientConnect()
|
||
{
|
||
while (true)
|
||
{
|
||
//监听到客户端的连接,获取双方通信socket
|
||
socket = serverSocket.Accept();
|
||
//创建线程循环接收客户端发送的数据
|
||
threadReceive = new Thread(Receive);
|
||
//传入双方通信socket
|
||
threadReceive.Start(socket);
|
||
}
|
||
}
|
||
|
||
|
||
//数据接收
|
||
private static void Receive(object socket)
|
||
{
|
||
try
|
||
{
|
||
Socket myClientSocket = (Socket)socket;
|
||
Form1 form1 = new Form1();
|
||
while (true)
|
||
{
|
||
byte[] buff = new byte[20000];
|
||
int r = myClientSocket.Receive(buff);
|
||
str = Encoding.Default.GetString(buff, 0, r);
|
||
//数据解析
|
||
data_analysis(str);
|
||
}
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
Console.WriteLine(err.Message);
|
||
}
|
||
}
|
||
|
||
//关闭
|
||
public static void close_server()
|
||
{
|
||
try
|
||
{
|
||
//socket关闭
|
||
serverSocket.Close();
|
||
//线程关闭
|
||
listenThread.Abort();
|
||
}
|
||
catch (Exception err) { }
|
||
try
|
||
{
|
||
//send_server("1");
|
||
threadReceive.Abort();
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
Console.WriteLine(err.Message);
|
||
}
|
||
}
|
||
|
||
//发送
|
||
public static void send_server(string message)
|
||
{
|
||
try
|
||
{
|
||
string strMsg = message.Trim();
|
||
byte[] buffer = new byte[20000];
|
||
buffer = Encoding.Default.GetBytes(strMsg);
|
||
socket.Send(buffer);
|
||
}
|
||
catch (Exception err)
|
||
{
|
||
Console.WriteLine(err.Message);
|
||
}
|
||
}
|
||
|
||
//数据解析部分
|
||
public static void data_analysis(string str)
|
||
{
|
||
string[] rec = null;
|
||
//按照逗号分隔,存入rec list内
|
||
rec = str.Split(',');
|
||
//去除字符串中的所有空格
|
||
/*for (int i = 0; i < rec.Length; i++)
|
||
{
|
||
rec[i] = rec[i].Replace(" ", "");
|
||
Console.WriteLine(rec[i]);
|
||
}*/
|
||
|
||
//解析
|
||
//检验首位设备型号
|
||
if (rec[0].Equals("#ZTTHY- 1"))
|
||
{
|
||
//确定数据功能码
|
||
if (rec[2].Equals("data_trans"))//数据传输
|
||
{
|
||
//进行数据和校验
|
||
if (tool.check_sum(rec[5], rec[6]))
|
||
{
|
||
if (rec[3].Equals("par_sensor"))//多参数传感器
|
||
{
|
||
//定义长度为13的数组接收,顺序为温度、电导率、盐度、浊度、总固体悬浮物、溶解氧百分比、溶解氧、叶绿素、红藻类、ph、刷子位置、压力、浊度
|
||
double[] Multi_parameter_EXO2 = tool.Multi_parameter_EXO2(rec[6]);
|
||
}
|
||
else if (rec[3].Equals("wea_sensor"))//气象传感器
|
||
{
|
||
//定义长度为6数组接收,顺序为气温、湿度、风速、风向、气压、雨量
|
||
double[] Weather_WXT536 = tool.Weather_WXT536(rec[6]);
|
||
}
|
||
else if (rec[3].Equals("gps_sensor"))//GPS传感器
|
||
{
|
||
//定义长度为2数组接收,顺序为纬度、经度
|
||
double[] GPS = tool.GPS(rec[6]);
|
||
}
|
||
}
|
||
}
|
||
//开关信息
|
||
else if (rec[2].Equals("switch_inf"))
|
||
{
|
||
//进行数据和校验
|
||
}
|
||
//参数设置
|
||
else if (rec[2].Equals("parame_set"))
|
||
{
|
||
|
||
}
|
||
//先进行时间戳校验
|
||
//获取当前系统时间戳 示例1604327302表示时间2020-11-02 22:28:22
|
||
long local_time = Convert.ToInt64((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
|
||
long get_time = long.Parse(rec[4]);
|
||
//如果时间戳校验误差大于5秒则发送当前系统时间进行校验 可修改为其他数值
|
||
if (TimeSpan.FromSeconds(local_time - get_time).TotalSeconds > 5)
|
||
{
|
||
send_server("#ZTTHY- 1,Server,data_trans, time," + local_time.ToString() + ",End");
|
||
};
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|