// Broadcast data packets, Server 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; Namespace windowsapplication1 { /// <Summary> /// Summary of form1. /// </Summary> Public class form1: system. Windows. Forms. Form { Private system. Windows. Forms. Label label1; Private system. Windows. Forms. Button button1; Private system. Windows. Forms. textbox textbox1; Private system. Windows. Forms. Label label2; /// <Summary> /// Required designer variables. /// </Summary> Private system. componentmodel. Container components = NULL; 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) { 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. label1 = new system. Windows. Forms. Label (); This. button1 = new system. Windows. Forms. Button (); This. textbox1 = new system. Windows. Forms. Textbox (); This. label2 = new System. Windows. Forms. Label (); This. SuspendLayout (); // // Label1 // This. label1.Location = new System. Drawing. Point (40, 24 ); This. label1.Name = "label1 "; This. label1.Size = new System. Drawing. Size (152, 23 ); This. label1.TabIndex = 0; This. label1.Text = "input broadcast information "; // // Button1 // This. button1.Location = new System. Drawing. Point (96,200 ); This. button1.Name = "button1 "; This. button1.TabIndex = 2; This. button1.text = "send "; This. button1.click + = new system. eventhandler (this. button#click ); // // Textbox1 // This. textbox1.autosize = false; This. textbox1.location = new system. Drawing. Point (32, 56 ); This. textbox1.name = "textbox1 "; This. textbox1.size = new system. Drawing. Size (240,112 ); This. textbox1.tabindex = 3; This. textbox1.text = "textbox1 "; // // Label2 // This. label2.BackColor = System. drawing. color. fromArgb (System. byte) (128), (System. byte) (255), (System. byte) (255 ))); This. label2.Location = new System. Drawing. Point (24,232 ); This. label2.Name = "label2 "; This. label2.Size = new System. Drawing. Size (248, 40 ); This. label2.TabIndex = 4; This. label2.Text = "this program was created by Xuan soul of Liaoning University of Science and Technology, QQ717532978 "; // // Form1 // This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 ); This. ClientSize = new System. Drawing. Size (292,273 ); This. Controls. Add (this. label2 ); This. Controls. Add (this. textBox1 ); This. Controls. Add (this. button1 ); This. Controls. Add (this. label1 ); This. Name = "form1 "; This. Text = "form1 "; This. resumelayout (false ); } # Endregion /// <Summary> /// Main entry point of the application. /// </Summary> [Stathread] Static void main () { Application. Run (New form1 ()); } Private void button#click (Object sender, system. eventargs E) { // Broadcast can only be sent using UDP protocol, so protocoltype is set to UDP Socket socket = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp ); // Let it automatically provide IP addresses in the subnet IPEndPoint Internet Explorer = new IPEndPoint (IPAddress. Broadcast, 8899 ); // Set the value of broadcast to 1, allowing the socket to send broadcast information Socket. SetSocketOption (SocketOptionLevel. Socket, SocketOptionName. Broadcast, 1 ); // Convert the sent content to a byte array Byte [] bytes = System. Text. Encoding. Unicode. GetBytes (this. textBox1.Text ); // Send information to the subnet Socket. SendTo (bytes, iep ); Socket. Close (); } } } // Client Using System; Using System. Drawing; Using System. Collections; Using system. componentmodel; Using system. Windows. forms; Using system. Data; Using system. net; Using system. Threading; Using system. net. Sockets; Namespace windowsapplication1 { /// <Summary> /// Summary of form1. /// </Summary> Public class form1: system. Windows. Forms. Form { Private system. Windows. Forms. Label label1; Private system. Windows. Forms. Button button1; Private system. Windows. Forms. Button button2; Endpoint EP; String receivedata; /// <Summary> /// Required designer variables. /// </Summary> Private System. ComponentModel. Container components = null; Public Form1 () { // // Required for Windows Form Designer support // InitializeComponent (); // AcceptMessage (), which is blocked before receiving broadcast information. No form is generated. AcceptMessage (); // // TODO: add Any constructor code after InitializeComponent calls // } /// <Summary> /// Clear all resources in use. /// </Summary> Protected override void Dispose (bool disposing) { If (disposing) { If (components! = NULL) { Components. Dispose (); } } Base. Dispose (disposing ); } // Receive information Private void acceptmessage () { // D defines the socket object Socket socket = new socket (addressfamily. InterNetwork, sockettype. dgram, protocoltype. UDP ); Ipendpoint IEP = new ipendpoint (IPaddress. Any, 8899 ); Socket. BIND (IEP ); Ep = (endpoint) IEP; Byte [] bytes = new byte [1, 1024]; While (true) { Socket. ReceiveFrom (bytes, ref ep ); ReceiveData = System. Text. Encoding. Unicode. GetString (bytes ); ReceiveData = receiveData. TrimEnd ('\ u000000 '); Thread th = new Thread (new ThreadStart (Acc )); Th. Start (); // Th. Abort ();
} Socket. Close (); } Private void Acc () { String message = "message from" + ep. ToString () + "; Dialogresult result = MessageBox. Show (receivedata, message, messageboxbuttons. abortretryignore ); } # 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. label1 = new system. Windows. Forms. Label (); This. button1 = new system. Windows. Forms. Button (); This. button2 = new system. Windows. Forms. Button (); This. suspendlayout (); // // Label1 // This. label1.backcolor = system. drawing. color. fromargb (system. byte) (255), (system. byte) (255), (system. byte) (192 ))); This. label1.ForeColor = System. Drawing. Color. Black; This. label1.Location = new System. Drawing. Point (0, 8 ); This. label1.Name = "label1 "; This. label1.Size = new System. Drawing. Size (424, 23 ); This. label1.TabIndex = 0; This. label1.Text = "you have chosen not to receive broadcast information. Please confirm "; // // Button1 // This. button1.BackColor = System. Drawing. Color. Red; This. button1.Location = new System. Drawing. Point (80, 88 ); This. button1.Name = "button1 "; This. button1.TabIndex = 1; This. button1.Text = "continue receiving "; // This. button1.Click + = new System. EventHandler (this. button#click ); // // Button2 // This. button2.BackColor = System. Drawing. Color. Red; This. button2.Location = new System. Drawing. Point (240, 88 ); This. button2.Name = "button2 "; This. button2.TabIndex = 2; This. button2.Text = "exit system "; // This. button2.Click + = new System. EventHandler (this. button2_Click ); // // Form1 // This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 ); This. BackColor = System. Drawing. Color. Blue; This. ClientSize = new System. Drawing. Size (424,125 ); This. Controls. Add (this. button2 ); This. Controls. Add (this. button1 ); This. Controls. Add (this. label1 ); This. FormBorderStyle = System. Windows. Forms. FormBorderStyle. FixedDialog; This. MaximizeBox = false; This. MinimizeBox = false; This. Name = "Form1 "; This. Text = "prompt "; This. ResumeLayout (false ); } # Endregion /// <Summary> /// Main entry point of the application. /// </Summary> [STAThread] Static void Main () { Application. Run (new Form1 ()); } } }
|