You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Cigarette/WindowsFormsApp2/Client.cs

368 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Linq;
namespace WindowsFormsApp2
{
public delegate void callback(string msg);
public class Client
{
//显示端
//显示端
public static byte[] clientIP = new byte[] { 192, 168, 31, 243 };
public static byte[] localIP = new byte[] { 192, 168, 31, 226 };
public static int dataPort = 2999;
public static int imagePort = 3999;
//图像大小
//int w = 80, h = 60;
int w = 160, h = 120;
static Client()
{
for (int i = 1; i < 16; i++)
{
//license(dataPort + i);
//license(imagePort + i);
}
}
public Client()
{
}
public void DisConnection()
{
m_Done = true;
try
{
if (u != null)
{
//if (recvThread != null)
//{
// this.recvThread.Abort();
//}
//if (recvThreadImage != null)
//{
// this.recvThreadImage.Abort();
//}
u.Close();
u = null;
}
if (uimage != null)
uimage.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private static void license(int port)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
List<int> list_pid = GetPidByPort(p, port);
if (list_pid.Count == 0)
{
return;
}
List<string> list_process = GetProcessNameByPid(p, list_pid);
StringBuilder sb = new StringBuilder();
sb.AppendLine("占用" + port + "端口的进程有:");
foreach (var item in list_process)
{
sb.Append(item + "\r\n");
}
sb.AppendLine("是否要结束这些进程?");
PidKill(p, list_pid);
}
private static void PidKill(Process p, List<int> list_pid)
{
p.Start();
foreach (var item in list_pid)
{
p.StandardInput.WriteLine("taskkill /pid " + item + " /f");
p.StandardInput.WriteLine("exit");
}
p.Close();
}
private static List<int> GetPidByPort(Process p, int port)
{
int result;
bool b = true;
p.Start();
p.StandardInput.WriteLine(string.Format("netstat -ano|find \"{0}\"", port));
p.StandardInput.WriteLine("exit");
StreamReader reader = p.StandardOutput;
string strLine = reader.ReadLine();
List<int> list_pid = new List<int>();
while (!reader.EndOfStream)
{
strLine = strLine.Trim();
if (strLine.Length > 0 && ((strLine.Contains("TCP") || strLine.Contains("UDP"))))
{
Regex r = new Regex(@"\s+");
string[] strArr = r.Split(strLine);
if (strArr.Length >= 4)
{
b = int.TryParse(strArr[3], out result);
if (b && !list_pid.Contains(result))
list_pid.Add(result);
}
}
strLine = reader.ReadLine();
}
p.WaitForExit();
reader.Close();
p.Close();
return list_pid;
}
private static List<string> GetProcessNameByPid(Process p, List<int> list_pid)
{
p.Start();
List<string> list_process = new List<string>();
foreach (var pid in list_pid)
{
p.StandardInput.WriteLine(string.Format("tasklist |find \"{0}\"", pid));
p.StandardInput.WriteLine("exit");
StreamReader reader = p.StandardOutput;//截取输出流
string strLine = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
strLine = strLine.Trim();
if (strLine.Length > 0 && ((strLine.Contains(".exe"))))
{
Regex r = new Regex(@"\s+");
string[] strArr = r.Split(strLine);
if (strArr.Length > 0)
{
list_process.Add(strArr[0]);
}
}
strLine = reader.ReadLine();
}
p.WaitForExit();
reader.Close();
}
p.Close();
return list_process;
}
private int _bindCamId;
UdpClient u=null;
IPEndPoint e;
UdpClient uimage = null;
IPEndPoint eimage;
public void bindCamId(int id)
{
_bindCamId = id;
m_Done = false;
try
{
e = new IPEndPoint(new IPAddress(localIP), dataPort + _bindCamId);
u = new UdpClient(e);
recvThread = new Thread(new ThreadStart(Received));
recvThread.Priority = ThreadPriority.Normal;
recvThread.Start();
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
try
{
eimage = new IPEndPoint(new IPAddress(localIP), imagePort + _bindCamId);
uimage = new UdpClient(eimage);
recvThreadImage = new Thread(new ThreadStart(ReceivedImage));
recvThreadImage.Priority = ThreadPriority.Normal;
recvThreadImage.Start();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
private Action<string> t;
private Action<int,Bitmap> imageCall;
private Action<string> j;
private Action<string> o;
private Action<string> n;
private Action<string> speed;
private Action<string> kick;
private Action<string> total;
public void InitLicenseData(Action<string> t, Action<string> j, Action<string> o, Action<string> n, Action<string> speed
, Action<string> kick, Action<string> total)
{
try
{
this.t = t;
this.j = j;
this.o = o;
this.n = n;
this.speed = speed;
this.kick = kick;
this.total = total;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write("端口号占用" + ex.ToString());
return;
}
}
public void InitLicenseData(Action<string> t)
{
try
{
this.t = t;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write("端口号占用" + ex.ToString());
return;
}
}
public void InitLicenseImage(Action<int,Bitmap> total)
{
try
{
this.imageCall = total;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write("端口号占用" + ex.ToString());
return;
}
}
bool m_Done = false;
public Thread recvThread = null;
public Thread recvThreadImage = null;
private void Received()
{
//ASCII 编码
Encoding ASCII = Encoding.ASCII;
while (!m_Done)
{
IPEndPoint endpoint = null;
if (u != null && null != recvThread && recvThread.IsAlive)
{
//接收数据
try
{
Byte[] data = u.Receive(ref endpoint);
//得到数据的ACSII的字符串形式
String receiveString = ASCII.GetString(data);
if (t!=null)
t(receiveString);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
//SOCKETEventArrive("receive:Nullerror");
}
}
Thread.Sleep(10); //防止系统资源耗尽
}
}
private void GenImage(byte[] data, out Bitmap bmp)
{
lock (this)
{
bmp = new Bitmap(w, h);
try
{
lock (this)
{
IntPtr pData = Marshal.AllocHGlobal(w * h * 3);
Marshal.Copy(data, 0, pData, w * h * 3);
bmp = new Bitmap(w, h, w * 3, PixelFormat.Format24bppRgb, pData);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
private void ReceivedImage()
{
//Thread.Sleep(2000);
//ASCII 编码
Encoding ASCII = Encoding.ASCII;
//Thread.Sleep(30); //防止系统资源耗尽
while (!m_Done)
{
IPEndPoint endpoint = null;
if (u != null && null != recvThread && recvThread.IsAlive)
{
try
{
Byte[] receiveBytes = uimage.Receive(ref endpoint);
{
//Bitmap bm;
//GenImage(receiveBytes.Skip(1).Take(receiveBytes.Length - 1).ToArray(), out bm);
//imageCall(receiveBytes[0],bm);
IntPtr pData = Marshal.AllocHGlobal(w * h * 3);
Marshal.Copy(receiveBytes.Skip(1).Take(receiveBytes.Length - 1).ToArray(), 0, pData, w * h * 3);
using (Bitmap bm = new Bitmap(w, h, w * 3, PixelFormat.Format24bppRgb, pData))
{
imageCall(receiveBytes[0], bm);
}
Marshal.FreeHGlobal(pData); //free tha memory
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
Thread.Sleep(10); //防止系统资源耗尽
}
}
}
}