internal class Program
{
private static Logger LogHelper = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
//var list = File.ReadAllLines("d://2.txt");
// List<string> ips = new List<string>();
// foreach (var item in list)
// {
// if (string.IsNullOrWhiteSpace(item)) continue;
// ips.Add(string.Format("{0},{1}", item,"4545"));
// }
// var val = string.Join(";", ips);
//var xx = int.Parse("0003");
Console.WriteLine("请输入IP地址");
var cip = Console.ReadLine();
Console.WriteLine("输入的IP地址为" + cip);
Console.WriteLine("请输入端口");
var cport = Console.ReadLine();
Console.WriteLine("输入的端口为" + cport);
string ip = cip;
LogHelper.Info("IP地址" + ip);
int port = int.Parse(cport);
string communication = "00200001000000000000\0";
byte[] communicationbyte = Encoding.ASCII.GetBytes(communication);
string heart = "00209999000000000000\0";
byte[] heartbyte = Encoding.ASCII.GetBytes(heart);
string sub60 = "00200060000000000000\0";
byte[] sub60byte = Encoding.ASCII.GetBytes(sub60);
string sub61 = "00200062000000000000\0";
byte[] sub62byte = Encoding.ASCII.GetBytes(sub61);
string sub0900 = "00380008001 0900001390020010020\0"; ;
byte[] sub0900byte = Encoding.ASCII.GetBytes(sub0900);
var tcpClient = new TcpClient();
Task.Run(async delegate
{
try
{
IAsyncResult async2 = tcpClient.Client.BeginConnect(ip, port, null, null);
if (!async2.AsyncWaitHandle.WaitOne(1500))
{
tcpClient.Client.EndConnect(async2);
Console.WriteLine("连接失败");
}
else
{
tcpClient.Client.EndConnect(async2);
IAsyncResult sendsync3 = tcpClient.Client.BeginSend(communicationbyte, 0, communicationbyte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsync3);
await Task.Delay(1000);
IAsyncResult sendsyncsub = tcpClient.Client.BeginSend(sub60byte, 0, sub60byte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsyncsub);
await Task.Delay(1000);
IAsyncResult sendsy = tcpClient.Client.BeginSend(sub0900byte, 0, sub0900byte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsy);
}
}
catch (Exception ex)
{
LogHelper.Info("初始化连接异常" + ex.Message);
}
});
Task.Run(async delegate
{
while (true)
{
if (tcpClient != null && tcpClient.Connected)
try
{
//LogHelper.Info("当前拧紧连接状态"+ tcpClient.Connected.ToString());
byte[] result = new byte[1024];
IAsyncResult resultasync = tcpClient.Client.BeginReceive(result, 0, result.Length, SocketFlags.None, delegate { }, null);
int releg = tcpClient.Client.EndReceive(resultasync);
if (releg != 0)
{
var d = result.Take(releg).Select(x => x.ToString("X")).ToList();
LogHelper.Info($"接收到报文:{string.Join(" ", d)}");
string output = Encoding.Default.GetString(result, 0, releg);
LogHelper.Info("接收到报文:" + output);
// string sub60 = "00200060000000000000\0";
var mid = result.Skip(4).Take(4).ToArray();
string strmid = Encoding.Default.GetString(mid);
LogHelper.Info("接收到报文strmid:" + strmid);
if (strmid == "0061")
{
IAsyncResult sendsy = tcpClient.Client.BeginSend(sub62byte, 0, sub62byte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsy);
LogHelper.Info("回复0061");
}
}
}
catch (Exception ex)
{
LogHelper.Info("接收报文异常" + ex.Message);
}
else
await Task.Delay(3000);
}
});
Task.Run(async delegate
{
while (true)
{
try
{
await Task.Delay(5000);
IAsyncResult sendsync = tcpClient.Client.BeginSend(heartbyte, 0, heartbyte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsync);
}
catch (Exception)
{
if (tcpClient?.Connected ?? false)
tcpClient?.Client?.Shutdown(SocketShutdown.Both);
try
{
tcpClient = new TcpClient();
IAsyncResult async = tcpClient.Client.BeginConnect(ip, port, null, null);
if (!async.AsyncWaitHandle.WaitOne(1500))
{
tcpClient.Client.EndConnect(async);
Console.WriteLine("连接失败");
}
else
{
tcpClient.Client.EndConnect(async);
IAsyncResult sendsync2 = tcpClient.Client.BeginSend(communicationbyte, 0, communicationbyte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsync2);
Console.WriteLine("连接 communicationbyte");
await Task.Delay(1000);
IAsyncResult sendsyncsub60byte = tcpClient.Client.BeginSend(sub60byte, 0, sub60byte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsyncsub60byte);
Console.WriteLine("连接 sendsyncsub60byte");
IAsyncResult sendsy = tcpClient.Client.BeginSend(sub0900byte, 0, sub0900byte.Length, SocketFlags.None, null, null);
tcpClient.Client.EndSend(sendsy);
}
}
catch (Exception ex)
{
LogHelper.Info("重新连接异常" + ex.Message.ToString());
}
}
}
});
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}