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 IpClient { public partial class Form1 : Form { Socket socket;//The socket responsible for the connection protected AutoResetEvent EvTCP_NewMessageReceived; // StringBuilder ClientLog = new StringBuilder(); public Form1() { InitializeComponent(); EvTCP_NewMessageReceived = new AutoResetEvent(false); } private void Log(string str) { ClientLog.AppendText(str + "\r\n"); } private void receive() { while (true) { //Determine whether the data can be received try { byte[] buffer = new byte[1024 * 1024 * 2]; int length = socket.Receive(buffer); if (length == 0) { Log("Port is not open"); CloseSocket(socket); socket = null; break; } string txt = Encoding.UTF8.GetString(buffer, 0, length); Log(socket.RemoteEndPoint + ":\r\t" + txt); // Send an answer if a request message, for test without check anything. //string txtResponse = "Response \r\n" + txt; //byte[] bufferResponse = Encoding.ASCII.GetBytes(txtResponse);//ascii encoding //socket.Send(bufferResponse); EvTCP_NewMessageReceived.Set(); } catch { break; } } } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } private void ConncectB_Click(object sender, EventArgs e) { //Determine whether to request a connection repeatedly try { Log("Connected " + socket.LocalEndPoint.ToString() + "\nPlease do not request the connection repeatedly"); } catch { //Determine whether the input is wrong try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //socket.ReceiveTimeout = 3333; //socket.SendTimeout = 3333; //Create IP address and port number; System.Net.IPAddress ip = IPAddress.Parse(Ipbox.Text); int port = Convert.ToInt32(PortBox.Text); IPEndPoint iPEndPoint = new IPEndPoint(ip, port); //Determine whether you can connect to the server try { socket.Connect(iPEndPoint); //var result = socket.BeginConnect(iPEndPoint, null, null); //bool success = result.AsyncWaitHandle.WaitOne(15000, true); //if (success) //{ // socket.EndConnect(result); //} //else //{ // socket.Close(); // throw new SocketException(10060); // Connection timed out. //} Log("Connected " + socket.LocalEndPoint.ToString()); //Start receiving data thread Thread th = new Thread(receive); th.IsBackground = true; th.Start(); } catch { Log("Port is not open"); } } catch { socket = null; Log("Input error"); } } } private void CloseB_Click(object sender, EventArgs e) { if (socket != null) { CloseSocket(socket); socket = null; Log("Connection closed"); } else { Log("Not connected Close"); } } private void SendB_Click(object sender, EventArgs e) { try { EvTCP_NewMessageReceived.Reset(); string txt = SendText.Text; byte[] buffer = Encoding.ASCII.GetBytes(txt);//ascii encoding socket.Send(buffer); //buffer = Encoding.ASCII.GetBytes(">");//ascii encoding //socket.Send(buffer); if (!EvTCP_NewMessageReceived.WaitOne(15000)) { // throw new Exception($"Customer class - no answer from request!"); MessageBox.Show("Customer class - no answer from request!", "Info...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch { Log("Not connected SendB"); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (socket != null) { CloseSocket(socket); socket = null; } } //Disconnect socket private void CloseSocket(Socket o) { try { o.Shutdown(SocketShutdown.Both); o.Disconnect(false); o.Close(); } catch { Log("error"); } } } }