123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Collections;
- using System.Threading;
- using System.Net;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.Windows.Forms;
- namespace AGV_WPF.Services
- {
- public class Server
- {
- public Server()
- {
- //clientTable = new Hashtable();
- try
- {
- using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]))
- {
- con.Open();
- databaseStr = con.Database;
- con.Close();
- }
- }
- catch (System.Exception ex)
- {
- databaseStr = "Error";
- }
- }
- string databaseStr;
- private string localIP;
- public string LocalIP
- {
- get { return localIP; }
- set
- {
- if(localIP != value)
- localIP = value;
- }
- }
- private int port;
- public int Port
- {
- get { return port; }
- set
- {
- if(port != value)
- port = value;
- }
- }
-
- public TcpListener server = null;
- public Thread thread = null;
- public Thread receiveThread = null;
- public Thread sendThread = null;
- public static Hashtable clientTable = new Hashtable();
- public bool IsThreadActive = false;//监听线程是否活动状态
- public delegate void InvokeDelegate();
- public static event EventHandler ClientChanged;
- void ConnectHandler()
- {
- try
- {
- IPAddress[] address = Dns.GetHostAddresses(Dns.GetHostName());
- for (int i = 0; i < address.Count();i++ )
- {
-
- if (address[i].AddressFamily.ToString().ToLower()=="internetwork" && address[i].ToString() == LocalIP)
- {
- server = new TcpListener(IPAddress.Parse(LocalIP), Port);
- server.Start();
- break;
- }
- else if (i == address.Count() - 1)
- {
- MessageBox.Show("IP地址不存在");
- //throw new Exception("IP地址不存在");
- return;
- }
- }
-
- }
- catch (System.Exception ex)
- {
- //MessageBox.Show(ex.Message);
-
- throw new Exception(ex.Message);
- }
- while (IsThreadActive)
- {
- if (server.Pending())
- {
- TcpClient client = server.AcceptTcpClient();
- receiveThread = new Thread(ReceiveHandler);
- receiveThread.IsBackground = true;
- clientTable.Add(client, receiveThread);
- if (ClientChanged != null)
- {
- ClientChanged(clientTable, null);
- }
- receiveThread.Start(client);
- }
- else
- {
- Thread.Sleep(3000);//3秒扫描一次
- }
- }
- }
- void ReceiveHandler(object o)
- {
- TcpClient client = o as TcpClient;
- int i = 0;
- byte[] buffer = new byte[1024];
- while (true)
- {
- if (client.Client.Poll(-1, SelectMode.SelectRead))
- {
- try
- {
- i = client.Client.Receive(buffer);
- }
- catch (System.Net.Sockets.SocketException ex)
- {
- if (clientTable.ContainsKey(client))
- {
- client.Close();
- clientTable.Remove(client);
- if (ClientChanged != null)
- {
- ClientChanged(clientTable, null);
- }
- //MessageBox.Show("客户端强制下线");
- }
- break;
- }
- if (i == 0)
- {
- if (clientTable.ContainsKey(client))
- {
- client.Close();
- clientTable.Remove(client);
- if (ClientChanged != null)
- {
- ClientChanged(clientTable, null);
- }
-
- }
- break;
- }
- /*
- else if (i > 0)
- {
- string str = System.Text.Encoding.Default.GetString(buffer, 0, i);
- client.Client.Send(new byte[1]);
- }*/
- }
- }
- }
- void SendHandler()
- {
- byte[] buffer = System.Text.Encoding.Default.GetBytes(string.Format("#{0}#", databaseStr));
- while (true)
- {
- if (IsThreadActive)
- {
- foreach (DictionaryEntry dic in clientTable)
- {
- if (dic.Key != null)
- {
- TcpClient client = dic.Key as TcpClient;
- if (client != null)
- {
- try
- {
- client.Client.Send(buffer);
- }
- catch (System.Exception ex)
- {
- continue;
- }
- }
- }
- }
- }
- Thread.Sleep(3000);
- }
- }
- public void Open(string ip,int port)
- {
- if (thread != null && thread.ThreadState == ThreadState.Running)
- {
- //MessageBox.Show("正在监听中......");
- return;
- }
- try
- {
- this.LocalIP = ip;
- this.Port = port;
- thread = new Thread(new ThreadStart(ConnectHandler));
- thread.IsBackground = true;
- sendThread = new Thread(SendHandler);
- sendThread.IsBackground = true;
- IsThreadActive = true;
- thread.Start();
- sendThread.Start();
- }
- catch (System.Exception ex)
- {
- //MessageBox.Show(ex.Message);
- throw new Exception(ex.Message);
- //return;
- }
- }
- public void Close()
- {
- if (thread != null)
- {
- IsThreadActive = false;
- thread.Abort();
- }
- if (sendThread != null)
- {
- sendThread.Abort();
- }
- foreach (DictionaryEntry dic in clientTable)
- {
- if (dic.Value != null)
- {
- Thread t = dic.Value as Thread;
- if (t != null)
- {
- t.Abort();
- }
- }
-
- }
- if (server != null)
- server.Stop();
- clientTable.Clear();//移除所有客户信息
- }
- public static byte[] HexToByte(string msg)
- {
- msg = msg.Replace(" ", "");//移除空格
- if (msg.Length % 2 != 0)
- msg = msg.Substring(0, msg.Length - 1);
- byte[] comBuffer = new byte[msg.Length / 2];
- for (int i = 0; i < msg.Length; i += 2)
- {
- comBuffer[i / 2] = (byte)Convert.ToByte(msg.Substring(i, 2), 16);
- }
- return comBuffer;
- }
- }
- }
|