Using system; Using system. drawing; Using system. collections; Using system. componentmodel; Using system. Windows. forms; Using system. Data; Using system. net; Using system. net. Sockets; Using system. text; Using system. Threading; Namespace myqq { /// <Summary> /// Summary of form1. /// </Summary> Public class form1: system. Windows. Forms. Form { Public bool runing = false; // flag Public udpclient Listen = new udpclient (2525 ); Public ipendpoint end; Public IPaddress groupaddress = IPaddress. parse ("broadcast 255.255"); // broadcast address Public int groupport = 2525; // broadcast Port Public String machinename; Public String machineip; Public String romename; Public String romeip; Public String romecon;   Private system. Windows. Forms. ListBox box; Private system. Windows. Forms. Label label1; Private system. Windows. Forms. Label label2; Private system. Windows. Forms. Button button1; Private system. Windows. Forms. Button button3; Private system. Windows. Forms. Label label3; Private system. Windows. Forms. textbox t_rec; Private system. Windows. Forms. textbox t_send; Private system. Windows. Forms. Button button2; Private system. Windows. Forms. Button newmsg; /// <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. Box = new system. Windows. Forms. ListBox (); This. t_rec = new system. Windows. Forms. Textbox (); This. label1 = new system. Windows. Forms. Label (); This. label2 = new system. Windows. Forms. Label (); This. t_send = new system. Windows. Forms. Textbox (); This. button1 = new system. Windows. Forms. Button (); This. button3 = new system. Windows. Forms. Button (); This. label3 = new system. Windows. Forms. Label (); This. button2 = new system. Windows. Forms. Button (); This. newmsg = new system. Windows. Forms. Button (); This. suspendlayout (); // // Box // This. Box. itemheight = 12; This. Box. Location = new system. Drawing. Point (0, 32 ); This. Box. Name = "box "; This. Box. size = new system. Drawing. Size (200,352 ); This. Box. tabindex = 0; // // T_rec // This. t_rec.backcolor = system. Drawing. systemcolors. inactiveborder; This. t_rec.forecolor = system. drawing. color. fromargb (system. byte) (192), (system. byte) (64), (system. byte) (0 ))); This. t_rec.location = new system. Drawing. Point (208, 56 ); This. t_rec.multiline = true; This. t_rec.name = "t_rec "; This. t_rec.scrollbars = system. Windows. Forms. scrollbars. vertical; This. t_rec.size = new system. Drawing. Size (328,136 ); This. t_rec.tabindex = 1; This. t_rec.text = ""; // // Label1 // This. label1.location = new system. Drawing. Point (208, 40 ); This. label1.name = "label1 "; This. label1.size = new system. Drawing. Size (120, 16 ); This. label1.tabindex = 2; This. label1.text = "receive :"; // // Label2 // This. label2.location = new system. Drawing. Point (208,200 ); This. label2.name = "label2 "; This. label2.size = new system. Drawing. Size (120, 16 ); This. label2.tabindex = 2; This. label2.text = "Send :"; // // T_send // This. t_send.location = new system. Drawing. Point (208,216 ); This. t_send.multiline = true; This. t_send.name = "t_send "; This. t_send.scrollbars = system. Windows. Forms. scrollbars. vertical; This. t_send.size = new system. Drawing. Size (328,112 ); This. t_send.tabindex = 1; This. t_send.text = ""; // // Button1 // This. button1.location = new system. Drawing. Point (440,344 ); This. button1.name = "button1 "; This. button1.size = new system. Drawing. Size (75, 32 ); This. button1.tabindex = 3; This. button1.text = "sending "; This. button1.click + = new system. eventhandler (this. button#click ); // // Button3 // This. button3.flatstyle = system. Windows. Forms. flatstyle. Popup; This. button3.location = new system. Drawing. Point (88, 3 ); This. button3.name = "button3 "; This. button3.size = new system. Drawing. Size (56, 23 ); This. button3.tabindex = 4; This. button3.text = "refresh "; This. button3.click + = new system. eventhandler (this. button3_click ); // // Label3 // This. label3.font = new system. drawing. font ("", 9f, system. drawing. fontstyle. regular, system. drawing. graphicsunit. point, (system. byte) (134 ))); This. label3.forecolor = system. Drawing. color. blue; This. label3.location = new system. Drawing. Point (8, 8 ); This. label3.name = "label3 "; This. label3.size = new system. Drawing. Size (80, 16 ); This. label3.tabindex = 5; This. label3.text = "online user :"; // // Button2 // This. button2.location = new system. Drawing. Point (360,344 ); This. button2.name = "button2 "; This. button2.size = new system. Drawing. Size (75, 32 ); This. button2.tabindex = 6; This. button2.text = "empty "; This. button2.click + = new system. eventhandler (this. button2_click ); // // Newmsg // This. newmsg. Location = new system. Drawing. Point (208, 56 ); This. newmsg. Name = "newmsg "; This. newmsg. size = new system. Drawing. Size (328,136 ); This. newmsg. tabindex = 7; This. newmsg. Text = "there is a new message! "; This. newmsg. Click + = new system. eventhandler (this. newmsg_click ); // // Form1 // This. autoscalebasesize = new system. Drawing. Size (6, 14 ); This. clientsize = new system. Drawing. Size (544,389 ); This. Controls. Add (this. button2 ); This. Controls. Add (this. label3 ); This. Controls. Add (this. button3 ); This. Controls. Add (this. button1 ); This. Controls. Add (this. label1 ); This. Controls. Add (this. Box ); This. Controls. Add (this. label2 ); This. Controls. Add (this. t_send ); This. Controls. Add (this. t_rec ); This. Controls. Add (this. newmsg ); This. Name = "form1 "; This. Text = "y "; This. Closing + = new system. componentmodel. canceleventhandler (this. form1_closing ); This. Load + = new system. eventhandler (this. form#load ); This. resumelayout (false ); } # Endregion /// <Summary> /// Main entry point of the application. /// </Summary> [Stathread] Static void main () { Application. Run (New form1 ()); } Private void form1_load (Object sender, system. eventargs E) {
 
  End = new ipendpoint (groupaddress, groupport ); // Initialize the computer name and port Iphostentry myentry = DNS. gethostbyname (DNS. gethostname ()); IPaddress myaddress = new IPaddress (myentry. Addresslist [0]. Address ); Machinename = DNS. gethostname (); Machineip = myaddress. tostring (); Box. Items. Add ("IP host name "); // Enable the listening thread Runing = true; Thread mythread = new thread (New threadstart (this. listenport )); Mythread. Start (); // Send online information String sends = "0 &" + machineip + "&" + machinename + "&" + machineip; Sendpack (sends ); } // Listen to the UDP packet of the broadcast address on the specified port Public void listenport () { Ipendpoint tempend = new ipendpoint (ipaddresses. Any, 2525 ); While (runing) { Application. doevents (); Try { Byte [] recb = listen. Receive (ref tempend ); // Check the received information Checkmessage (recb ); } Catch (exception E) { MessageBox. Show (E. Message. tostring ()); Break; }
  } Listen. Close (); Box. Items. Add ("the thread has exited! "); Runing = false; } // Receive packets cyclically Public void checkmessage (byte [] recbb) { String recstr = encoding. Default. getstring (recbb ); String [] rec = recstr. Split ('&'); Switch (REC [0]) { Case "0": // refresh If (REC [3] = machineip) { If (box. findstring (REC [1] + "" + rec [2]) <= 0) Box. Items. Add (REC [1] + "" + rec [2]); } Else if (REC [1] = rec [3]) { // Refresh request sent from another machine // Return your own information String sendstr = "0 &" + machineip + "&" + machinename + "&" + rec [3]; This. sendpack (sendstr ); // Add the user if the user does not exist If (box. findstring (REC [1] + "" + rec [2]) <= 0) Box. Items. Add (REC [1] + "" + rec [2]); } Break; Case "1": // speak If (REC [3] = machineip) { Romename = rec [2]; Romeip = rec [1]; Romecon = rec [4]; Showne (); } Break; Case "2": // upload a file Break; Case "9": // confirm the package If (REC [3] = machineip) MessageBox. Show ("information from: REC [2] (" + rec [1] + ") \ r \ n" + rec [4], "message "); Break; }
  }
  // Send data packets to the broadcast address Public void sendpack (string sendstr) {
  Byte [] sendb = encoding. Default. getbytes (sendstr ); Try { Listen. Send (sendb, sendb. length, end ); } Catch (exception E) { MessageBox. Show (E. Message. tostring ()); } } // Refresh Private void button3_click (Object sender, system. eventargs E) { Box. Items. Clear (); Box. Items. Add ("IP host name "); String temp2 = "0 &" + machineip + "&" + machinename + "&" + machineip; This. sendpack (temp2 ); } // Close the loop Private void form1_closing (Object sender, system. componentmodel. canceleventargs E) { Runing = false; Udpclient Mm = new udpclient (); Ipendpoint tempipend = new ipendpoint (IPaddress. parse ("127.0.0.1"), 2525 ); String temps = "0 & 0 & 0 & 0 & 0 "; Byte [] sendb = encoding. Default. getbytes (temps ); Mm. Send (sendb, sendb. length, tempipend ); } // Send Private void button#click (Object sender, system. eventargs E) { If (t_send.text = "") { MessageBox. Show ("Empty message cannot be sent! "); Return; } If (box. selecteditem = NULL | box. selectedindex = 0) { MessageBox. Show ("select a user first! "); Return; } String send = "1 &" + machineip + "&" + machinename + "&" + box. selecteditem. tostring (). split ('') [0] +" & "+ t_send.text; This. sendpack (send ); } // Clear Private void button2_click (Object sender, system. eventargs E) { T_send.text = string. empty; T_rec.text = string. empty; } // Click Show received information and hide itself Private void newmsg_click (Object sender, system. eventargs E) { T_rec.text = "Message from" + romename + "(" + romeip + ")" + datetime. Now. to1_datestring (); T_rec.text + = "\ r \ n>" + romecon; String back = "9 &" + machineip + "&" + machinename + "&" + romeip + "& information enabled! "; This. sendpack (back ); Newmsg. sendtoback (); } // Display the prompt button when there is information Public void showne () { // Form display If (this. windowstate! = Formwindowstate. Normal) { This. Visible = true; This. windowstate = formwindowstate. normal; } Newmsg. bringtofront (); } } }  |