diff --git a/DianBiao/.vs/DianBiao/FileContentIndex/a7d2c089-388e-47f3-b2a4-4dc003835f26.vsidx b/DianBiao/.vs/DianBiao/FileContentIndex/a7d2c089-388e-47f3-b2a4-4dc003835f26.vsidx new file mode 100644 index 0000000..65e31f7 Binary files /dev/null and b/DianBiao/.vs/DianBiao/FileContentIndex/a7d2c089-388e-47f3-b2a4-4dc003835f26.vsidx differ diff --git a/DianBiao/.vs/DianBiao/FileContentIndex/d103a658-d514-439b-bad5-c4fbb0c5e7b7.vsidx b/DianBiao/.vs/DianBiao/FileContentIndex/d103a658-d514-439b-bad5-c4fbb0c5e7b7.vsidx new file mode 100644 index 0000000..760d7a6 Binary files /dev/null and b/DianBiao/.vs/DianBiao/FileContentIndex/d103a658-d514-439b-bad5-c4fbb0c5e7b7.vsidx differ diff --git a/DianBiao/.vs/DianBiao/FileContentIndex/read.lock b/DianBiao/.vs/DianBiao/FileContentIndex/read.lock new file mode 100644 index 0000000..e69de29 diff --git a/DianBiao/.vs/DianBiao/v17/.suo b/DianBiao/.vs/DianBiao/v17/.suo new file mode 100644 index 0000000..6a3b1a0 Binary files /dev/null and b/DianBiao/.vs/DianBiao/v17/.suo differ diff --git a/DianBiao/ToolKit.cs b/DianBiao/ToolKit.cs index cac3d3b..7a0a72d 100644 --- a/DianBiao/ToolKit.cs +++ b/DianBiao/ToolKit.cs @@ -165,14 +165,25 @@ namespace ToolKitlib /// byte数组转16进制字符串 /// /// + /// 是否添加空格 /// - public static string byteArrayToHexString(byte[] data) + public static string byteArrayToHexString(byte[] data, bool isSpace = true) { StringBuilder builder = new StringBuilder(); - for (int i = 0; i < data.Length; i++) + if (isSpace) { - builder.Append(string.Format("{0:X2} ", data[i])); + for (int i = 0; i < data.Length; i++) + { + builder.Append(string.Format("{0:X2} ", data[i])); + } } + else + { + for (int i = 0; i < data.Length; i++) + { + builder.Append(string.Format("{0:X2}", data[i])); + } + } return builder.ToString().Trim(); } @@ -200,16 +211,16 @@ namespace ToolKitlib /// /// /// - public static byte[] hexStringToByteArray(string data) + public static byte[] hexStringToByteArray(string v) { - string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - byte[] returnBytes = new byte[chars.Length]; - //逐个字符变为16进制字节数据 - for (int i = 0; i < chars.Length; i++) + if (v.Length % 2 != 0) v = "0" + v; + v.Replace(" ", ""); + byte[] bytes = new byte[v.Length / 2]; + for (int i = 0; i <= v.Length - 2; i += 2) { - returnBytes[i] = Convert.ToByte(chars[i], 16); + bytes[i / 2] = (byte)(Convert.ToInt64(v[i].ToString() + v[i + 1].ToString(), 16)); } - return returnBytes; + return bytes; } /// @@ -377,6 +388,20 @@ namespace ToolKitlib } + /// + /// 16进制 转小端 byte[] + /// + /// + /// + public static dynamic ToLittleEndian(string message) + { + byte[] bytes= stringToByteArray(message, 16); + byte[] byteNew = bytes; + Array.Reverse(byteNew); + return byteNew;//返回byte[] + + //return byteArrayToHexString(byteNew, false);//返回string + } } #endregion } diff --git a/DianBiao/dianbiao.cs b/DianBiao/dianbiao.cs index 0d833d0..259a42c 100644 --- a/DianBiao/dianbiao.cs +++ b/DianBiao/dianbiao.cs @@ -7,6 +7,7 @@ using System.Threading; using ToolKitlib; using System.Net.Sockets; using System.Net; +using System.Runtime.Remoting.Metadata.W3cXsd2001; namespace DianBiao { @@ -14,15 +15,22 @@ namespace DianBiao { static log4net.ILog log;//日志插件 - public static Socket udpServer;//udp服务器 - public static bool memberReply;//是否回复消息 - public static int length = 0;//接收数据长度 + public static Socket serverSocket; + /// + /// 在线用户列表 + /// + public static List idOnLine = new List(); + /// + /// 存储IP及Socket--便于服务器与指定客户端通信--Socket.Send () + /// + public static Dictionary OnLineDic = new Dictionary(); - public static EndPoint serverEnd; //服务端 - public static IPEndPoint ipEnd; //发送消息服务端 - public static Socket socket; //目标socket - public static Thread connectThread; //连接线程 - public static byte[] sendData;//发送的数据 + /// + /// 循环获取数据时间 + /// + public static int loopTime = 10; + + public static Encoding encoding = Encoding.Default; static void Main(string[] args) { @@ -32,55 +40,131 @@ namespace DianBiao init(); while (true) { - Thread.Sleep(10000); + if (OnLineDic.Count == 0) continue; + Thread.Sleep(1000 * loopTime); } } - private static void init() + /// + /// 初始化服务器 + /// + public static void init() + { + //TCP + try + { + //调用socket(函数创建一个用于通信的套接字。 + serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + //给已经创建的套接宁绑定一个端口号 + IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("172.16.1.49"), 6800); + serverSocket.Bind(endPoint); + + //调用listen(函数使套接宁成为—个监听 + serverSocket.Listen(1000);//最大连接数 + + //开启监听任务 + Task.Run(new Action(() => + { + ListenConnection(); + })); + + } + catch (Exception e) + { + log.Info(string.Format("服务器初始化异常:{0}", e.Message)); + } + } + + /// + /// 监听任务 + /// + private static void ListenConnection() + { + while (true) + { + //调用accept() 函数来接受客户端的连接,这是就可以和客户端通信了。 + Socket clientSocket = serverSocket.Accept();//新用户连接后触发返回新的socket(阻塞) + string ipPort = clientSocket.RemoteEndPoint.ToString();//连接用户的IP及端口 + addOnLine(ipPort, clientSocket, true); + Console.WriteLine(clientSocket.RemoteEndPoint.ToString() + "上线了"); + Task.Run(() => ReceiveMsg(clientSocket));//针对单个客户端开启线程(接收) + + } + } + + /// + /// 接收消息--接收到进入触发 + /// + /// + private static void ReceiveMsg(Socket clientSocket) + { + while (true) + { + byte[] bytes = new byte[1024]; + int length = -1; + try + { + length = clientSocket.Receive(bytes);//返回字节数 + } + catch (Exception) + { + //用户下线--更新在线列表 + addOnLine(clientSocket.RemoteEndPoint.ToString(), clientSocket, false); + Console.WriteLine(clientSocket.RemoteEndPoint.ToString() + "下线了"); + break;//结束线程 + } + if (length == 0) + { + //用户下线--更新在线列表 + addOnLine(clientSocket.RemoteEndPoint.ToString(), clientSocket, false); + Console.WriteLine(clientSocket.RemoteEndPoint.ToString() + "下线了"); + break;//结束线程 + } + + if (length > 0) + { + //string message = Encoding.Default.GetString(bytes, 0, length); + string message = ToolKit.byteArrayToHexString(bytes, length); + log.Info(string.Format("接受报文:{0}", message)); + string mag = message.Replace(" ", ""); + byte[] by = ToolKit.hexStringToByteArray(mag); + //收到消息--mag + } + } + } + + /// + /// 更新在线用户列表 + /// + /// 用户IP端口号套接 + /// Socket + /// 用是否增加 + private static void addOnLine(string clientIp, Socket clientSocket, bool isAdd) { try { - //var IP = "111.229.30.246"; - - var IP = "172.17.0.9"; - //string IP = "172.16.1.49"; - int Port = 12302; - - //建立udp服务器,参数2:udp协议以数据报的方式传输,参数3:UDP协议 - udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - //为服务器绑定IP - IPAddress ip = IPAddress.Parse(IP); - EndPoint ep = new IPEndPoint(ip, Port); - udpServer.Bind(ep); - //接收数据 - EndPoint endP = new IPEndPoint(IPAddress.Any, 0); - - string message; - byte[] data = new byte[1024 * 1024 * 2]; - length = 0; - //把数据的来源放到第二个参数上 - while (true) + if (isAdd) { - length = udpServer.ReceiveFrom(data, ref endP); - //message = Encoding.Default.GetString(data, 0, length); - message = ToolKit.byteArrayToHexString(data, length); - log.Info("接收到 " + endP.ToString() + ": " + message); - message = message.Replace(" ", ""); - - - + idOnLine.Add(clientIp); + OnLineDic.Add(clientIp, clientSocket); + } + else + { + idOnLine.Remove(clientIp); + OnLineDic.Remove(clientIp); } } catch (Exception e) { - - Console.WriteLine("接收初始化:" + e.Message); + log.Info(string.Format("更新在线用户异常:{0}", e.Message)); } - + /*Invoke(new Action(() => + { + if (isAdd) { idOnLine.Add(clientIp); } + else { idOnLine.Remove(clientIp); } + }));*/ } - - - S } } diff --git a/DianBiao/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/DianBiao/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index d9679fc..6aae575 100644 Binary files a/DianBiao/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/DianBiao/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/DianBiao/obj/Debug/DianBiao.csproj.AssemblyReference.cache b/DianBiao/obj/Debug/DianBiao.csproj.AssemblyReference.cache index 7c3f806..04b8491 100644 Binary files a/DianBiao/obj/Debug/DianBiao.csproj.AssemblyReference.cache and b/DianBiao/obj/Debug/DianBiao.csproj.AssemblyReference.cache differ