using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace IpServer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Socket socketSend;//Socket responsible for communication Socket socketListener;//Socket responsible for monitoring Dictionary dictionary = new Dictionary();//Store the connected Socket //Use threads to receive data private void Listen(object o) { Socket socket = o as Socket; while (true) { //The socket responsible for monitoring is used to receive client connections try { //Create a socket responsible for communication socketSend = socket.Accept(); dictionary.Add(socketSend.RemoteEndPoint.ToString(), socketSend); clientCombo.Items.Add(socketSend.RemoteEndPoint.ToString()); clientCombo.SelectedIndex = clientCombo.Items.IndexOf(socketSend.RemoteEndPoint.ToString()); Log("Connected " + socketSend.RemoteEndPoint.ToString()); //Start a new thread to receive information from the client Thread th = new Thread(receive); th.IsBackground = true; th.Start(socketSend); } catch { continue; } } } //The server receives the message from the client private void receive(object o) { Socket socketSend = o as Socket; string str = ""; while (true) { //Try to connect to the client try { //After the client connects successfully, the server receives the message from the client byte[] buffer = new byte[1024 * 1024 * 2];//2M大小 //Number of valid bytes received int length = socketSend.Receive(buffer); if (length == 0) { string tmpIp = socketSend.RemoteEndPoint.ToString(); Log(tmpIp + " Offline"); clientCombo.Items.Remove(tmpIp); //Try to delete the connection information try { clientCombo.SelectedIndex = 0; } catch { clientCombo.Text = null; } CloseSocket(dictionary[tmpIp]); dictionary.Remove(tmpIp); break; } str += Encoding.ASCII.GetString(buffer, 0, length); Log(socketSend.RemoteEndPoint.ToString() + "\n\t" + str); int lengthFirstTag = str.IndexOf(">"); if (lengthFirstTag <= 0) continue; string lastTag = "= 0) { str = ""; Log(socketSend.RemoteEndPoint.ToString() + "\n\t message ready, whole message received!"); string txt = @""; byte[] bufferResponse = new byte[1024 * 1024 * 2]; bufferResponse = Encoding.UTF8.GetBytes(txt); try { string ip = clientCombo.SelectedItem.ToString();//Get the selected ip address Socket socketsend = dictionary[ip]; socketsend.Send(bufferResponse); } catch { Log("Transmission failed response from request"); } } // // // // // // // // // // // // // } catch { break; } } } private void listenerB_Click(object sender, EventArgs e) { //Create a listening socket //SocketType.Stream streaming corresponds to the tcp protocol //Dgram, datagram corresponds to UDP protocol socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //Create IP address and port number; IPAddress ip = IPAddress.Parse(Ipbox.Text); int port = Convert.ToInt32(PortBox.Text); IPEndPoint iPEndPoint = new IPEndPoint(ip, port); //Let the listening socket bind the ip and port number socketListener.Bind(iPEndPoint); Log("Listen successfully" + ip + "\t" + port); //Set up the listening queue socketListener.Listen(10);//Maximum number of connections at a time Thread thread = new Thread(Listen); thread.IsBackground = true; thread.Start(socketListener); } catch { Log("Failed to listen"); } } private void close_Click(object sender, EventArgs e) { try { try { string tmpip = socketSend.RemoteEndPoint.ToString(); CloseSocket(dictionary[tmpip]); dictionary.Remove(tmpip); clientCombo.Items.Remove(tmpip); try { clientCombo.SelectedIndex = 0; } catch { clientCombo.Text = null; } socketSend.Close(); Log(socketSend.RemoteEndPoint.ToString() + "Offline"); socketListener.Close(); Log("Listener is closed"); } catch { socketListener.Close(); Log("Listener is closed"); } } catch { Log("close error"); } } private void SendB_Click(object sender, EventArgs e) { string txt = SendText.Text; byte[] buffer = Encoding.UTF8.GetBytes(txt); try { string ip = clientCombo.SelectedItem.ToString();//Get the selected ip address Socket socketsend = dictionary[ip]; socketsend.Send(buffer); } catch { Log("Transmission failed"); } } private void CloseSocket(Socket o) { try { o.Shutdown(SocketShutdown.Both); o.Disconnect(false); o.Close(); } catch { Log(o.ToString() + "error"); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { try { string tmpip = socketSend.RemoteEndPoint.ToString(); CloseSocket(dictionary[tmpip]); dictionary.Remove(tmpip); clientCombo.Items.Remove(tmpip); try { clientCombo.SelectedIndex = 0; } catch { clientCombo.Text = null; } socketSend.Close(); socketListener.Close(); } catch { socketListener.Close(); } } catch { } } private void Form1_Load(object sender, EventArgs e) { //Cancel errors caused by cross-thread calls Control.CheckForIllegalCrossThreadCalls = false; } private void Log(string str) { ServerLog.AppendText(str + "\r\n"); } } }