Using System; Using System. Drawing; Using System. Collections; Using System. ComponentModel; Using System. Windows. Forms; Using System. Data; Using System. Net. Sockets; Using System. Net; Using System. IO; Using System. Text; Using System. Threading; Namespace WindowsApplication1 { /// <Summary> /// Summary of Form1. /// </Summary> Public class Form1: System. Windows. Forms. Form { Private System. Windows. Forms. Label label3; Private System. Windows. Forms. Label label4; Private System. Windows. Forms. Label label5; Private System. Windows. Forms. Label label6; Private System. Windows. Forms. Label label7; Private System. Windows. Forms. Label label8; Private System. Windows. Forms. TextBox textBox1; Private System. Windows. Forms. TextBox textBox2; Private System. Windows. Forms. TextBox textBox3; Private System. Windows. Forms. ListBox listBox1; Private System. Windows. Forms. ListBox listBox2; Private System. Windows. Forms. Button button1; Private System. Windows. Forms. Button button2; Private System. Windows. Forms. Button button3; Private System. Windows. Forms. Button button4; Private System. Windows. Forms. TextBox textBox4; Private System. Windows. Forms. Label label1; Private system. Windows. Forms. statusbar statusbar1; Private system. Windows. Forms. statusbarpanel statusbarpanel1; Private system. Windows. Forms. statusbarpanel statusbarpanel2; Private system. componentmodel. Container components = NULL; /// <Summary> /// Required designer variables. /// </Summary> // Create a thread to listen on the port number Private thread th; // Listener port number Private tcplistener tllisten1; // Set the flag to determine the listening status Private bool listenerrun = true; // Create a basic data stream instance for sending and receiving Private networkstream tcpstream; // Create an instance for transmitting information to a remote machine Private streamwriter reqstreamw; // Create a remote connection instance Private tcpclient TCPC; // Create an instance for receiving data from the remote host Private socket sksocket; Public form1 () { // // Required for Windows Form Designer support // Initializecomponent (); // // Todo: add Any constructor code after initializecomponent calls // } /// <Summary> /// Clear all resources in use. /// </Summary> Protected override void dispose (bool disposing) { Try { ListenerRun = false; Th. Abort (); Th = null; TlListen1.Stop (); SkSocket. Close (); } Catch (Exception) { } If (disposing) { If (components! = Null) { Components. Dispose (); } } Base. Dispose (disposing );
} # Region code generated by Windows Form Designer /// <Summary> /// The designer supports the required methods-do not use the code editor to modify /// Content of this method. /// </Summary> Private void InitializeComponent () { This. label3 = new System. Windows. Forms. Label (); This. label4 = new System. Windows. Forms. Label (); This. label5 = new System. Windows. Forms. Label (); This. label6 = new System. Windows. Forms. Label (); This. label7 = new System. Windows. Forms. Label (); This. label8 = new System. Windows. Forms. Label (); This. textBox1 = new System. Windows. Forms. TextBox (); This. textBox2 = new System. Windows. Forms. TextBox (); This. textBox3 = new System. Windows. Forms. TextBox (); This. listBox1 = new System. Windows. Forms. ListBox (); This. listBox2 = new System. Windows. Forms. ListBox (); This. button1 = new System. Windows. Forms. Button (); This. button2 = new System. Windows. Forms. Button (); This. button3 = new System. Windows. Forms. Button (); This. button4 = new System. Windows. Forms. Button (); This. textBox4 = new System. Windows. Forms. TextBox (); This. label1 = new System. Windows. Forms. Label (); This. statusBar1 = new System. Windows. Forms. StatusBar (); This. statusBarPanel1 = new System. Windows. Forms. StatusBarPanel (); This. statusBarPanel2 = new System. Windows. Forms. StatusBarPanel (); (System. ComponentModel. ISupportInitialize) (this. statusBarPanel1). BeginInit (); (System. ComponentModel. ISupportInitialize) (this. statusBarPanel2). BeginInit (); This. SuspendLayout (); // // Label3 // This. label3.BackColor = System. drawing. color. fromArgb (System. byte) (255), (System. byte) (192), (System. byte) (255 ))); This. label3.Location = new System. Drawing. Point (24,120 ); This. label3.Name = "label3 "; This. label3.size = new system. Drawing. Size (72, 23 ); This. label3.tabindex = 2; This. label3.text = "sending message :"; // // Label4 // This. label4.backcolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (192), (system. byte) (255 ))); This. label4.location = new system. Drawing. Point (24,312 ); This. label4.name = "label4 "; This. label4.size = new system. Drawing. Size (72, 23 ); This. label4.tabindex = 3; This. label4.text = "Receive information "; // // Label5 // This. label5.backcolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (192), (system. byte) (255 ))); This. label5.location = new system. Drawing. Point (24,184 ); This. label5.name = "label5 "; This. label5.size = new system. Drawing. Size (72, 23 ); This. label5.tabindex = 4; This. label5.text = "sent message "; // // Label6 // This. label6.backcolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (192), (system. byte) (255 ))); This. label6.location = new system. Drawing. Point (248, 88 ); This. label6.name = "label6 "; This. label6.Size = new System. Drawing. Size (56, 23 ); This. label6.TabIndex = 5; This. label6.Text = "local port "; // // Label7 // This. label7.BackColor = System. drawing. color. fromArgb (System. byte) (255), (System. byte) (192), (System. byte) (255 ))); This. label7.ForeColor = System. Drawing. Color. Black; This. label7.Location = new System. Drawing. Point (24, 32 ); This. label7.Name = "label7 "; This. label7.Size = new System. Drawing. Size (72, 23 ); This. label7.TabIndex = 0; This. label7.Text = "remote IP Address "; // // Label8 // This. label8.BackColor = System. drawing. color. fromArgb (System. byte) (255), (System. byte) (192), (System. byte) (255 ))); This. label8.Location = new System. Drawing. Point (24, 88 ); This. label8.Name = "label8 "; This. label8.Size = new System. Drawing. Size (72, 23 ); This. label8.TabIndex = 1; This. label8.Text = "remote port number "; // // TextBox1 // This. textBox1.Location = new System. Drawing. Point (128, 32 ); This. textBox1.Name = "textBox1 "; This. textBox1.Size = new System. Drawing. Size (264, 21 ); This. textBox1.TabIndex = 6; This. textBox1.Text = "textBox1 "; // // TextBox2 // This. textBox2.Location = new System. Drawing. Point (128, 88 ); This. textBox2.Name = "textBox2 "; This. textBox2.TabIndex = 7; This. textBox2.Text = "textBox2 "; // // TextBox3 // This. textBox3.Location = new System. Drawing. Point (312, 88 ); This. textBox3.Name = "textBox3 "; This. textBox3.Size = new System. Drawing. Size (80, 21 ); This. textbox3.tabindex = 8; This. textbox3.text = "textbox3 "; // // Listbox1 // This. listbox1.itemheight = 12; This. listbox1.location = new system. Drawing. Point (128,160 ); This. listbox1.name = "listbox1 "; This. listbox1.size = new system. Drawing. Size (264,112 ); This. listbox1.tabindex = 9; // // Listbox2 // This. listbox2.itemheight = 12; This. listbox2.location = new system. Drawing. Point (128,296 ); This. listbox2.name = "listbox2 "; This. listbox2.size = new system. Drawing. Size (264,112 ); This. listbox2.tabindex = 10; // // Button1 // This. button1.backcolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (255), (system. byte) (128 ))); This. button1.forecolor = system. Drawing. color. Red; This. button1.location = new system. Drawing. Point (408, 40 ); This. button1.name = "button1 "; This. button1.size = new system. Drawing. Size (120, 40 ); This. button1.tabindex = 11; This. button1.text = "connecting to a remote host "; This. button1.Click + = new System. EventHandler (this. button#click ); // // Button2 // This. button2.BackColor = System. drawing. color. fromArgb (System. byte) (255), (System. byte) (255), (System. byte) (128 ))); This. button2.ForeColor = System. Drawing. Color. Red; This. button2.Location = new System. Drawing. Point (408,144 ); This. button2.Name = "button2 "; This. button2.Size = new System. Drawing. Size (120, 40 ); This. button2.TabIndex = 12; This. button2.Text = "listening port "; This. button2.Click + = new System. EventHandler (this. button2_Click ); // // Button3 // This. button3.backcolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (255), (system. byte) (128 ))); This. button3.forecolor = system. Drawing. color. Red; This. button3.location = new system. Drawing. Point (408,240 ); This. button3.name = "button3 "; This. button3.size = new system. Drawing. Size (120, 40 ); This. button3.tabindex = 13; This. button3.text = "Send message "; This. button3.click + = new system. eventhandler (this. button3_click ); // // Button4 // This. button4.BackColor = System. drawing. color. fromArgb (System. byte) (255), (System. byte) (255), (System. byte) (128 ))); This. button4.ForeColor = System. Drawing. Color. Red; This. button4.Location = new System. Drawing. Point (408,352 ); This. button4.Name = "button4 "; This. button4.Size = new System. Drawing. Size (120, 40 ); This. button4.TabIndex = 14; This. button4.Text = "Disconnect "; This. button4.Click + = new System. EventHandler (this. button4_Click ); // // TextBox4 // This. textBox4.Location = new System. Drawing. Point (128,120 ); This. textBox4.Name = "textBox4 "; This. textBox4.Size = new System. Drawing. Size (264, 21 ); This. textBox4.TabIndex = 15; This. textBox4.Text = "textBox4 "; // // Label1 // This. label1.BackColor = System. drawing. color. fromArgb (System. byte) (192), (System. byte) (192), (System. byte) (255 ))); This. label1.Location = new System. Drawing. Point (0, 0 ); This. label1.Name = "label1 "; This. label1.Size = new System. Drawing. Size (544, 23 ); This. label1.TabIndex = 16; This. label1.Text = "Copyright All Rights Reserved network 05 xuanhun production "; // // StatusBar1 // This. statusBar1.Location = new System. Drawing. Point (0,429 ); This. statusBar1.Name = "statusBar1 "; This. statusBar1.Panels. AddRange (new System. Windows. Forms. StatusBarPanel [] { This. statusBarPanel1, This. statusBarPanel2 }); This. statusBar1.ShowPanels = true; This. statusBar1.Size = new System. Drawing. Size (544, 96 ); This. statusBar1.TabIndex = 17; This. statusBar1.Text = "statusBar1 "; // // StatusBarPanel1 // This. statusBarPanel1.Text = "statusBarPanel1 "; This. statusBarPanel1.Width = 260; // // StatusBarPanel2 // This. statusBarPanel2.Text = "statusBarPanel2 "; This. statusBarPanel2.Width = 260; // // Form1 // This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 ); This. BackColor = System. Drawing. Color. Black; This. ClientSize = new System. Drawing. Size (544,525 ); This. Controls. Add (this. statusBar1 ); This. Controls. Add (this. label1 ); This. Controls. Add (this. textBox4 ); This. Controls. Add (this. button4 ); This. Controls. Add (this. button3 ); This. Controls. Add (this. button2 ); This. Controls. Add (this. button1 ); This. Controls. Add (this. listBox2 ); This. Controls. Add (this. listBox1 ); This. Controls. Add (this. textBox3 ); This. Controls. Add (this. textBox2 ); This. Controls. Add (this. textBox1 ); This. Controls. Add (this. label6 ); This. Controls. Add (this. label5 ); This. Controls. Add (this. label4 ); This. Controls. Add (this. label3 ); This. Controls. Add (this. label7 ); This. Controls. Add (this. label8 ); This. Name = "Form1 "; This. Text = "point-to-point communication program "; This. Load + = new System. EventHandler (this. form#load ); (System. ComponentModel. ISupportInitialize) (this. statusBarPanel1). EndInit (); (System. ComponentModel. ISupportInitialize) (this. statusBarPanel2). EndInit (); This. ResumeLayout (false ); } # Endregion /// <Summary> /// Main entry point of the application. /// </Summary> [STAThread] Static void Main () { Application. Run (new Form1 ()); } Private void Listen () { String time = DateTime. Now. ToString (); Try { TlListen1 = new TcpListener (Int32.Parse (textBox3.Text )); // Listen on the remote port TlListen1.Start (); StatusBar1.Panels [1]. Text = "listening ......";
SkSocket = tlListen1.AcceptSocket ();
// Obtain the network remote endpoint corresponding to the remote computer EndPoint tempRemoteEP = skSocket. RemoteEndPoint; // Obtain the remote computer name based on the remote network endpoint IPEndPoint tempRemoteIP = (IPEndPoint) tempRemoteEP; IPHostEntry host = Dns. GetHostByAddress (tempRemoteIP. Address ); String HostName = host. HostName; Statusbar1.panels [1]. Text = "'" + hostname + "'" + "remote computer connection is correct! "; // Loop listening While (listenerrun) { // Define the data storage buffer received from a remote computer Byte [] stream = new byte [80]; // Obtain the current time
// The received data is stored in the defined buffer and parsed from the buffer with the specified encoding. Int I = sksocket. receivefrom (stream, ref tempremoteep ); String smessage = system. Text. encoding. utf8.getstring (Stream );
// Display received data Listbox2.items. Add (Time + "" + hostname + ":"); ListBox2.Items. Add (sMessage ); } } Catch (System. Security. SecurityException) { MessageBox. Show ("firewall security error! "," Error ", MessageBoxButtons. OK, MessageBoxIcon. Exclamation ); } } Private void button#click (object sender, System. EventArgs e) { // Submit an application to the remote host. If a connection is established, the data stream used for data transmission is obtained. Try { Tcpc = new TcpClient (textBox1.Text, Int32.Parse (textBox2.Text ));
TcpStream = tcpc. GetStream (); StatusBar1.Panels [0]. Text = "successfully connected to a remote computer! "; Button2.Enabled = true; Button1.Enabled = false; Button4.Enabled = true;
} Catch (Exception) { StatusBar1.Panels [0]. Text = "the target computer rejects the connection request! "; } } Private void button2_Click (object sender, System. EventArgs e) { Th = new Thread (new ThreadStart (Listen )); Th. Start (); } Private void button3_Click (object sender, System. EventArgs e) { Try { String sMsg = textBox4.Text; String MyName = Dns. GetHostName (); ReqStreamW = new StreamWriter (tcpStream ); ReqStreamW. Write (sMsg ); ReqStreamW. Flush (); String time = DateTime. Now. ToString (); ListBox1.Items. Add (time + "" + MyName + ":"); ListBox1.Items. Add (sMsg ); TextBox4.Clear (); } Catch (Exception) { StatusBar1.Panels [0]. Text = "cannot be sent to the target computer! "; } } Private void button4_Click (object sender, System. EventArgs e) { ListenerRun = false; Tcpc. Close (); StatusBar1.Panels [0]. Text = "disconnected! "; Button1.Enabled = true; Button3.Enabled = false; Button2.Enabled = true; } Private void Form1_Load (object sender, System. EventArgs e) {
} } } |