123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Threading;
- using System.Net;
- using System.Configuration;
- using System.Data.SqlClient;
- using AGV_WPF_Global;
- using System.Windows.Media;
- namespace AGV_WPF.Services
- {
- public class Client
- {
- public Client(string ip,int port)
- {
- ServerIP = ip;
- ServerPort = port;
- ComClient = this;
- }
- private string serverIP;
- public static Client ComClient;
- public string ServerIP
- {
- get { return serverIP; }
- set
- {
- if(serverIP != value)
- serverIP = value;
- }
- }
- private int serverPort;
- public int ServerPort
- {
- get { return serverPort; }
- set
- {
- if (serverPort != value)
- {
- serverPort = value;
- }
-
- }
- }
-
- public TcpClient _client = null;
- public Thread receiveThread = null;
- public Thread sendThread = null;
- public static bool DisLinkFlag = false;//断开连接标志
- public static event EventHandler LinkStatusChanged;
- public static string databaseStr;
- public static bool SuccessFalg = false;
- public delegate void InvokeDelegate();
- public void Connect()
- {
- try
- {
- if (_client == null)
- _client = new TcpClient(ServerIP, ServerPort);
- receiveThread = new Thread(ReceiveData);
- receiveThread.IsBackground = true;
- receiveThread.Start(_client);
- /*sendThread = new Thread(SendData);
- sendThread.IsBackground = true;*/
- }
- catch (System.Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- public void ReceiveData(object o)
- {
- TcpClient client = o as TcpClient;
- if (client == null)
- return;
- int i = 0;
- byte[] buffer = new byte[1024];
- while (true)
- {
- if (client == null)
- break;
- if (client.Client.Poll(-1, SelectMode.SelectRead))
- {
- try
- {
- i = client.Client.Receive(buffer);
- DisLinkFlag = false;
- if (LinkStatusChanged != null)
- {
- LinkStatusChanged(this, null);
- }
- }
- catch (System.Net.Sockets.SocketException ex)
- {
- SuccessFalg = false;
- while (true)
- {
- try
- {
- if (MainWindow.mainWindow != null)
- {
- //MainWindow.mainWindow.UpdateLinkStatus("重连中...", Brushes.Red);
- }
- _client = new TcpClient(ServerIP, ServerPort);
- receiveThread = new Thread(ReceiveData);
- receiveThread.Start(_client);
-
- break;
- }
- catch (System.Exception ex2)
- {
- if (MainWindow.mainWindow != null)
- {
- //MainWindow.mainWindow.UpdateLinkStatus("重连失败", Brushes.Red);
- }
- Thread.Sleep(500);
- continue;
- }
- }
- Console.WriteLine("服务器关闭");
- DisLinkFlag = true;
- if (LinkStatusChanged != null)
- {
- LinkStatusChanged(this, null);
- }
- break;
- }
- if (i == 0)
- {
- //MessageBox.Show("服务器已关闭");
- Console.WriteLine("服务器关闭");
- DisLinkFlag = true;
- if (LinkStatusChanged != null)
- {
- LinkStatusChanged(this, null);
- }
- break;
- }
- else if (i > 0)
- {
- string str = System.Text.Encoding.Default.GetString(buffer, 0, i);
- if(str.StartsWith("#") && str.EndsWith("#"))
- {
- str = str.Replace("#","");
- Console.WriteLine(str);
- //string strCon = string.Format("Data Source={0};DataBase={1};user={2};pwd={3}", ServerIP, str, GlobalPara.DataBaseUser, GlobalPara.DataBasePwd);
- if (!SuccessFalg)
- {
- try
- {
- //using (SqlConnection con = new SqlConnection(strCon))
- {
- //con.Open();
- SuccessFalg = true;
- if (MainWindow.mainWindow != null)
- {
- //MainWindow.mainWindow.UpdateBackground();
- }
- Console.WriteLine("登录成功");
- //databaseStr = strCon;
- //con.Close();
- }
- }
- catch (System.Exception ex)
- {
- SuccessFalg = false;
- throw new Exception(ex.Message);
- }
- }
-
- }
-
- }
- }
- }
- Console.WriteLine("退出接收线程");
- }
- public void SendData(object o)
- {
- TcpClient client = o as TcpClient;
- while (true)
- {
- if (sendThread != null)
- {
- string str="";
- if (!string.IsNullOrEmpty(str))
- {
- byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
- try
- {
- client.Client.Send(buffer);
- }
- catch (System.Exception ex)
- {
- Console.WriteLine(ex.Message);
- break;
- }
- }
- Thread.Sleep(500);
- }
- }
- }
- public void Close()
- {
- if (receiveThread != null)
- {
- receiveThread.Abort();
- receiveThread = null;
- }
- if (sendThread != null)
- {
- sendThread.Abort();
- sendThread = null;
- }
- if (_client != null)
- {
- _client = null;
- }
- }
- }
- }
|