修改工具脚本
This commit is contained in:
parent
7d47c289ca
commit
d4e80b2fcc
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -165,14 +165,25 @@ namespace ToolKitlib
|
|||
/// byte数组转16进制字符串
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="isSpace">是否添加空格</param>
|
||||
/// <returns></returns>
|
||||
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
|
|||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -377,6 +388,20 @@ namespace ToolKitlib
|
|||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 16进制 转小端 byte[]
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
/// <summary>
|
||||
/// 在线用户列表
|
||||
/// </summary>
|
||||
public static List<string> idOnLine = new List<string>();
|
||||
/// <summary>
|
||||
/// 存储IP及Socket--便于服务器与指定客户端通信--Socket.Send ()
|
||||
/// </summary>
|
||||
public static Dictionary<string, Socket> OnLineDic = new Dictionary<string, Socket>();
|
||||
|
||||
public static EndPoint serverEnd; //服务端
|
||||
public static IPEndPoint ipEnd; //发送消息服务端
|
||||
public static Socket socket; //目标socket
|
||||
public static Thread connectThread; //连接线程
|
||||
public static byte[] sendData;//发送的数据
|
||||
/// <summary>
|
||||
/// 循环获取数据时间
|
||||
/// </summary>
|
||||
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()
|
||||
/// <summary>
|
||||
/// 初始化服务器
|
||||
/// </summary>
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 监听任务
|
||||
/// </summary>
|
||||
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));//针对单个客户端开启线程(接收)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收消息--接收到进入触发
|
||||
/// </summary>
|
||||
/// <param name="clientSocket"></param>
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新在线用户列表
|
||||
/// </summary>
|
||||
/// <param name="clientIp">用户IP端口号套接</param>
|
||||
/// <param name="clientSocket">Socket</param>
|
||||
/// <param name="isAdd">用是否增加</param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue