using DWMSFaceVerifyClient.UTIL;
using Newtonsoft.Json;
using PreviewDemo.Bean;
using PreviewDemo.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PreviewDemo
{
class DeviceHeart
{
private const string Finish_Flag = "[#over]";
private TcpListener tcpListener;
private Thread listenThread;
//private static TcpClient m_tcpClient;
public DeviceHeart()
{
this.tcpListener = new TcpListener(5000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
try
{
this.tcpListener.Start();
}
catch (Exception ex)
{
//MessageBox.Show("端口冲突,请重新启动软件!");
Console.WriteLine(ex.Message);
return;
}
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
Heart heart = new Heart(client);
Thread mythread = new Thread(new ThreadStart(heart.deal)); //将线程设为后台运行
mythread.Start();
Thread.Sleep(50);
}
}
class Heart
{
TcpClient client = null;
NetworkStream clientStream = null;
public Heart(TcpClient client)
{
this.client = client;
}
public void deal()
{
try
{
clientStream = client.GetStream();
byte[] message = new byte[512];
string recvText = "";
int bytesRead;
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 512);
}
catch (Exception ex)
{
//a socket error has occured
Console.WriteLine("关闭Socket异常:",ex);
if (client != null)
{
client.Close();
client = null;
}
}
if (bytesRead > 0)
{
recvText = recvText + Encoding.UTF8.GetString(message, 0, bytesRead);
//MessageBox.Show("接收到消息:" + recvText);
IDCardInfo cardinfo = JsonConvert.DeserializeObject<IDCardInfo>(recvText);
String xylx = cardinfo.Xylx;
String heart = cardinfo.Heart;
if (xylx != null && xylx.Equals("0") && heart != null && heart.Equals("2"))
{
dealDate(cardinfo);
}
//MessageBox.Show(cardinfo.Xylx);
/*
if (!Cache.serverIP.Equals(recvText))
{
Cache.serverIP = recvText;
Configuration cf = new Configuration();
cf.Load("config.ini", false, Encoding.GetEncoding("utf-8"));
cf.WriteProfileString("System", "serverIP", recvText);
cf.Save("config.ini", Encoding.GetEncoding("utf-8"));
}*/
}
else
{
recvText = "";
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine("关闭Socket:",ex);
if (client != null)
{
client.Close();
client = null;
}
}
finally
{
if (clientStream != null)
{
clientStream.Flush();
clientStream.Close();
}
if (client != null)
{
client.Close();
}
}
}
private void dealDate(IDCardInfo cardinfo)
{
List<Device> listdevice = Cache.listdevice;//设备list集合
String videoIp = cardinfo.VideoIP;//接收到的匹配设备
foreach (Device dev in listdevice)
{
if (ip!= null && !ip.Equals("") && ip.Equals(dev.Ip))
{
String value = "{\"xy\":\"1\",\"ip\":\"" + ip + "\",\"online\":\"" + dev.Online + "\"}";
TCPMessage tcpm = new TCPMessage(value, dev.ServIp);
tcpm.SendMessage();
}
}
}
}
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DWMSFaceVerifyClient.UTIL
{
class TCPMessage
{
private const string Finish_Flag = "[#over]";
Thread threadsend = null;
string message = "";
string serviceIp = null;
public TCPMessage(String value,String ip)
{
serviceIp = ip;
message = value + "#over";
}
public void SendMessage()
{
DateTime dt = DateTime.Now;
NetworkStream stream = null;
TcpClient m_tcpClient = null;
try
{
try
{
m_tcpClient = new TcpClient();
m_tcpClient.Connect(serviceIp, 6005);
}
catch (Exception ex)
{
Console.WriteLine("连接终端失败:" + ex.Message);
}
// 发送比对结果
byte[] msg = Encoding.UTF8.GetBytes(message);
stream = m_tcpClient.GetStream();
stream.Write(msg, 0, msg.Length);
DateTime dt1 = DateTime.Now;
TimeSpan ts = dt1 - dt;
Console.WriteLine("发送数据耗时:" + ts.Milliseconds);
Console.WriteLine("往终端发送消息成功");
}
catch (Exception ex)
{
Console.WriteLine("发送消息异常:" + ex.Message);
}finally
{
if (stream != null)
{
stream.Flush();
stream.Close();
}
if (m_tcpClient != null)
{
m_tcpClient.Close();
}
}
}
}
}