20211010_CZPM_upperpc/网口升级/垂直剖面面浮标网口升级/Server.cs
2023-07-27 11:01:29 +08:00

290 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace
{
class Server
{
///******************服务端************************/
///**
////定义Socket对象
//private static Socket serverSocket = null;
////定义监听线程
//private static Thread listenThread;
////定义接受客户端数据线程
//private static Thread threadReceive;
////定义双方通信
//private static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);**/
//private static String str;
////服务端
//private static Socket ServerSocket = null;
////tcp客户端字典
//public static Dictionary<string, MySession> dic_ClientSocket = new Dictionary<string, MySession>();
////线程字典,每新增一个连接就添加一条线程
//private static Dictionary<string, Thread> dic_ClientThread = new Dictionary<string, Thread>();
////监听客户端连接的标志
//private static bool Flag_Listen = true;
///// <summary>
///// 启动服务
///// </summary>
///// <param name="port">端口号</param>
//public static bool OpenServer(int port)
//{
// try
// {
// Flag_Listen = true;
// // 创建负责监听的套接字,注意其中的参数;
// ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// // 创建包含ip和端口号的网络节点对象
// IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
// try
// {
// // 将负责监听的套接字绑定到唯一的ip和端口上
// ServerSocket.Bind(endPoint);
// }
// catch
// {
// return false;
// }
// // 设置监听队列的长度;
// ServerSocket.Listen(100);
// // 创建负责监听的线程;
// Thread Thread_ServerListen = new Thread(ListenConnecting);
// Thread_ServerListen.IsBackground = true;
// Thread_ServerListen.Start();
// return true;
// }
// catch
// {
// return false;
// }
//}
///// <summary>
///// 关闭服务
///// </summary>
//public static void CloseServer()
//{
// lock (dic_ClientSocket)
// {
// foreach (var item in dic_ClientSocket)
// {
// item.Value.Close();//关闭每一个连接
// }
// dic_ClientSocket.Clear();//清除字典
// }
// lock (dic_ClientThread)
// {
// foreach (var item in dic_ClientThread)
// {
// item.Value.Abort();//停止线程
// }
// dic_ClientThread.Clear();
// }
// Flag_Listen = false;
// //ServerSocket.Shutdown(SocketShutdown.Both);//服务端不能主动关闭连接,需要把监听到的连接逐个关闭
// if (ServerSocket != null)
// ServerSocket.Close();
//}
///// <summary>
///// 监听客户端请求的方法;
///// </summary>
//private static void ListenConnecting()
//{
// while (Flag_Listen) // 持续不断的监听客户端的连接请求;
// {
// try
// {
// Socket sokConnection = ServerSocket.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字;
// // 将与客户端连接的 套接字 对象添加到集合中;
// string str_EndPoint = sokConnection.RemoteEndPoint.ToString();
// MySession myTcpClient = new MySession() { TcpSocket = sokConnection };
// //创建线程接收数据
// Thread th_ReceiveData = new Thread(ReceiveData);
// th_ReceiveData.IsBackground = true;
// th_ReceiveData.Start(myTcpClient);
// //把线程及客户连接加入字典
// foreach (string key in dic_ClientThread.Keys)
// {
// dic_ClientThread[key].Abort();
// }
// dic_ClientThread.Clear();
// dic_ClientSocket.Clear();
// dic_ClientThread.Add(str_EndPoint, th_ReceiveData);
// dic_ClientSocket.Add(str_EndPoint.Split(':')[0], myTcpClient);
// Console.WriteLine(str_EndPoint);
// }
// catch (Exception err)
// {
// Console.WriteLine(err.Message);
// }
// Thread.Sleep(200);
// }
//}
///// <summary>
///// 接收数据
///// </summary>
///// <param name="sokConnectionparn"></param>
//private static void ReceiveData(object sokConnectionparn)
//{
// MySession tcpClient = sokConnectionparn as MySession;
// Socket socketClient = tcpClient.TcpSocket;
// bool Flag_Receive = true;
// while (Flag_Receive)
// {
// try
// {
// // 定义一个2M的缓存区
// byte[] arrMsgRec = new byte[1024 * 1024 * 2];
// // 将接受到的数据存入到输入 arrMsgRec中
// int length = -1;
// try
// {
// length = socketClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
// }
// catch (Exception err)
// {
// Console.WriteLine("断开连接时产生的信息:" + err.Message);
// Flag_Receive = false;
// // 从通信线程集合中删除被中断连接的通信线程对象;
// foreach (string key in dic_ClientThread.Keys)
// {
// dic_ClientSocket.Remove(key);//删除客户端字典中该socket
// if (dic_ClientThread[key].IsAlive)
// {
// dic_ClientThread[key].Abort();//关闭线程
// }
// dic_ClientThread.Remove(key);//删除字典中该线程
// }
// tcpClient = null;
// socketClient = null;
// CloseServer();//关闭服务器
// OpenServer(8899);
// break;
// }
// byte[] buf = new byte[length];
// Array.Copy(arrMsgRec, buf, length);
// lock (tcpClient.m_Buffer)
// {
// tcpClient.AddQueue(buf);
// }
// str = Encoding.Default.GetString(buf, 0, length);
// //数据解析
// //data_analysis(str);
// Form1 form1 = new Form1();
// form1.ParsingData(new List<byte>(buf));
// }
// catch (Exception err)
// {
// Console.WriteLine(err.Message);
// }
// Thread.Sleep(100);
// }
//}
///// <summary>
///// 发送数据给指定的客户端
///// </summary>
///// <param name="_endPoint">客户端套接字</param>
///// <param name="_buf">发送的数组</param>
///// <returns></returns>
//public static bool send_server(string _endPoint, byte[] buffer)
//{
// MySession myT = new MySession();
// if (dic_ClientSocket.TryGetValue(_endPoint, out myT))
// {
// myT.Send(buffer);
// return true;
// }
// else
// {
// return false;
// }
//}
//public class MySession
//{
// public Socket TcpSocket;//socket对象
// public List<byte> m_Buffer = new List<byte>();//数据缓存区
// public MySession()
// {
// }
// /// <summary>
// /// 发送数据
// /// </summary>
// /// <param name="buf"></param>
// public void Send(byte[] buf)
// {
// if (buf != null)
// {
// try
// {
// TcpSocket.Send(buf);
// }
// catch (Exception err)
// {
// Console.WriteLine("发送指令失败,当前设备未连接");
// }
// }
// }
// /// <summary>
// /// 获取连接的ip
// /// </summary>
// /// <returns></returns>
// public string GetIp()
// {
// IPEndPoint clientipe = (IPEndPoint)TcpSocket.RemoteEndPoint;
// string _ip = clientipe.Address.ToString();
// return _ip;
// }
// /// <summary>
// /// 关闭连接
// /// </summary>
// public void Close()
// {
// TcpSocket.Shutdown(SocketShutdown.Both);
// }
// /// <summary>
// /// 提取正确数据包
// /// </summary>
// public byte[] GetBuffer(int startIndex, int size)
// {
// byte[] buf = new byte[size];
// m_Buffer.CopyTo(startIndex, buf, 0, size);
// m_Buffer.RemoveRange(0, startIndex + size);
// return buf;
// }
// /// <summary>
// /// 添加队列数据
// /// </summary>
// /// <param name="buffer"></param>
// public void AddQueue(byte[] buffer)
// {
// m_Buffer.AddRange(buffer);
// }
// /// <summary>
// /// 清除缓存
// /// </summary>
// public void ClearQueue()
// {
// m_Buffer.Clear();
// }
//}
}
}