123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace TestProject
- {
- class ModeBusTCPServer
- {
- private bool isOpen = false;
- Thread threadWatch = null;
- Socket socketWatch = null;
- public bool IsOpen
- {
- get
- {
- return isOpen;
- }
- set
- {
- isOpen = value;
- }
- }
- public string IP
- {
- get
- {
- return iP;
- }
- set
- {
- iP = value;
- }
- }
- public string Port
- {
- get
- {
- return port;
- }
- set
- {
- port = value;
- }
- }
- private string iP;
- private string port;
- RichTextAutoBottom richtext;
- ComboBox SelectIP;
- ComboBox Encodingbox;
- Button SEND;
- RichTextAutoBottom SENDMESSAGE;
- public List<Socket> list = new List<Socket>();
- public ModeBusTCPServer()
- {
- }
- public bool Open()
- {
- try
- {
- Encodingbox = Application.OpenForms["MainWindow"].Controls["Encoding"] as ComboBox;
- SelectIP = Application.OpenForms["MainWindow"].Controls["SelectIP"] as ComboBox;
- richtext = Application.OpenForms["MainWindow"].Controls["Result"] as RichTextAutoBottom;
- SEND = Application.OpenForms["MainWindow"].Controls["SEND"] as Button;
- SENDMESSAGE = Application.OpenForms["MainWindow"].Controls["SENDMESSAGE"] as RichTextAutoBottom;
- SEND.Click += SEND_Click;
-
- socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-
- IPAddress ipaddress = IPAddress.Parse(iP);
-
- IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(port));
-
- socketWatch.Bind(endpoint);
-
- socketWatch.Listen(20);
-
- threadWatch = new Thread(WatchConnecting);
-
- threadWatch.IsBackground = true;
-
- threadWatch.Start();
- isOpen = true;
- return true;
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- return false;
- }
- }
- private void SEND_Click(object sender, EventArgs e)
- {
- foreach (Socket item in list)
- {
- if (item != null)
- {
- if (item.RemoteEndPoint != null)
- {
- if (item.RemoteEndPoint.ToString() == SelectIP.Text)
- {
- switch (Encodingbox.Text)
- {
- case "UTF-8":
- item.Send(Encoding.UTF8.GetBytes(SENDMESSAGE.Text));
- break;
- case "ASCII":
- byte[] array = Encoding.ASCII.GetBytes(SENDMESSAGE.Text.Trim());
- string str = null;
- for (int i = 0; i < array.Length; i++)
- {
- int asciicode = (int)(array[i]);
- str += Convert.ToString(asciicode) + " ";
- }
- item.Send(Encoding.ASCII.GetBytes(str));
- break;
- case "Hexadecimal":
- char[] values = SENDMESSAGE.Text.ToCharArray();
- string strH = null;
- foreach (char letter in values)
- {
-
- int value = Convert.ToInt32(letter);
-
- string hexOutput = string.Format("{0:X}", value);
- strH += hexOutput + " ";
- }
- item.Send(Encoding.UTF8.GetBytes(strH));
- break;
- }
- }
- }
- else
- {
- int index = SelectIP.Items.IndexOf(item.RemoteEndPoint.ToString());
- SelectIP.Items.RemoveAt(index);
- if (SelectIP.Items.Count > 0)
- {
- if (SelectIP.Text == "")
- SelectIP.Text = SelectIP.Items[0].ToString();
- }
- else
- SelectIP.Text = "";
- richtext.AppendText("客户端" + item.RemoteEndPoint + "已经中断连接" + "\r\n");
- if (list.Contains(item))
- {
- list.Remove(item);
- }
-
- item.Close();
- }
- }
- }
- }
- private void WatchConnecting()
- {
- Socket connection = null;
- while (true)
- {
- try
- {
- connection = socketWatch.Accept();
- }
- catch (SocketException e)
- {
- break;
- }
-
- IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address;
- int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port;
-
- string sendmsg = "Connect Success!" + "LocalIP:" + clientIP + ",LocalPort" + clientPort.ToString();
- byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendmsg);
- connection.Send(arrSendMsg);
-
- string remoteEndPoint = connection.RemoteEndPoint.ToString();
-
- richtext.AppendText("成功与" + remoteEndPoint + "客户端建立连接!\t\n");
- SelectIP.Items.Add(clientIP + ":" + clientPort.ToString());
- SelectIP.SelectedItem = clientIP + ":" + clientPort.ToString();
-
- IPEndPoint netpoint = connection.RemoteEndPoint as IPEndPoint;
-
- ParameterizedThreadStart pts = new ParameterizedThreadStart(recv);
- Thread thread = new Thread(pts);
-
- thread.IsBackground = true;
-
- thread.Start(connection);
-
-
-
-
-
-
-
- list.Add(connection);
- }
- }
- void recv(object socketclientpara)
- {
- Socket socketServer = socketclientpara as Socket;
- while (true)
- {
-
- byte[] arrServerRecMsg = new byte[1024 * 1024];
-
- try
- {
- int length = socketServer.Receive(arrServerRecMsg);
- if (length == 0)
- {
- break;
- }
-
- string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
-
- richtext.AppendText("客户端:" + socketServer.RemoteEndPoint + ",\r\n" + strSRecMsg + "\r\n\n");
- }
- catch (Exception)
- {
- if (socketServer.RemoteEndPoint != null)
- {
-
- int index = SelectIP.Items.IndexOf(socketServer.RemoteEndPoint.ToString());
- SelectIP.Items.RemoveAt(index);
- if (SelectIP.Items.Count > 0)
- {
- if (SelectIP.Text == "")
- SelectIP.Text = SelectIP.Items[0].ToString();
- }
- else
- SelectIP.Text = "";
- richtext.AppendText("客户端" + socketServer.RemoteEndPoint + "已经中断连接" + "\r\n");
- }
- if (list.Contains(socketServer))
- {
- list.Remove(socketServer);
- }
-
- socketServer.Close();
- break;
- }
- }
- if (socketServer != null)
- {
- if (socketServer.RemoteEndPoint != null)
- {
- int index = SelectIP.Items.IndexOf(socketServer.RemoteEndPoint.ToString());
- SelectIP.Items.RemoveAt(index);
- if (SelectIP.Items.Count > 0)
- {
- if (SelectIP.Text == "")
- SelectIP.Text = SelectIP.Items[0].ToString();
- }
- else
- SelectIP.Text = "";
- richtext.AppendText("客户端" + socketServer.RemoteEndPoint + "已经中断连接" + "\r\n");
- }
- if (list.Contains(socketServer))
- {
- list.Remove(socketServer);
- }
- socketServer.Close();
- }
- }
- public void Close()
- {
- threadWatch.Abort();
- socketWatch.Close();
- isOpen = false;
- }
- public void SendOrder(string Code)
- {
- switch (Code)
- {
- default:
- break;
- }
- }
- }
- }
|