III. C # part of the source code of the network programming server program (server. CS ):
Because the structure we adopt in this program is asynchronous blocking mode, in the actual program, in order not to affect the running speed of the server-side program, we designed a thread in the program, make the network request listening, receiving, and sending processes in the thread. Please note this in the following code. The following is the server. complete CS code:
Using system; Using system. drawing; Using system. collections; Using system. componentmodel; Using system. Windows. forms; Using system. Data; Using system. net. Sockets; Using system. IO; Using system. Threading; Using system. net; // The namespace used in the import program Public class form1: Form { Private ListBox listbox1; Private button button2; Private Label label1; Private textbox textbox1; Private button button1; Private socket socketforclient; Private networkstream; Private tcplistener; Private streamwriter; Private streamreader; Private thread _ thread1; Private system. componentmodel. Container components = NULL; Public form1 () { Initializecomponent (); } // Clear various resources used in the program Protected override void dispose (bool disposing) { If (disposing) { If (components! = NULL) { Components. Dispose (); } } Base. Dispose (disposing ); } Private void initializecomponent () { Label1 = new label (); Button2 = new button (); Button1 = new button (); Listbox1 = new ListBox (); Textbox1 = new Textbox (); Suspendlayout (); Label1.location = new point (8,168 ); Label1.name = "label1 "; Label1.size = new size (120, 23 ); Label1.tabindex = 3; Label1.text = "feedback to the client :"; // Set other controls in the same way, skipped here
This. Controls. Add (button1 ); This. Controls. Add (textbox1 ); This. Controls. Add (label1 ); This. Controls. Add (button2 ); This. Controls. Add (listbox1 ); This. maximizebox = false; This. minimizebox = false; This. Name = "form1 "; This. Text = "C # network programming server! "; This. Closed + = new system. eventhandler (this. form1_closed ); This. resumelayout (false );
} Private void listen () { // Create a tcplistener object, which listens on the specified port. Tcplistener = new tcplistener (1234 ); // Start listening Tcplistener. Start (); // Return the socket instance that can be used to process the connection Socketforclient = tcplistener. acceptsocket (); Try { // If the returned value is "true", the resulting character set has accepted connection requests from far away. If (socketforclient. Connected) { Listbox1.items. Add ("You have successfully connected to the client! "); While (true) { // Create a networkstream object to accept and send data through the network set of characters Networkstream = new networkstream (socketforclient ); // Read a line of characters from the current data stream. The returned value is a string. Streamreader = new streamreader (networkstream ); String MSG = streamreader. Readline (); Listbox1.items. Add ("received client information:" + MSG ); Streamwriter = new streamwriter (networkstream ); If (textbox1.text! = "") { Listbox1.items. Add ("feedback to the client:" + textbox1.text ); // Write a line of string to the current data stream Streamwriter. writeline (textbox1.text ); // Refresh the data in the current data stream Streamwriter. Flush (); } } } } Catch (exception ey) { MessageBox. Show (ey. tostring ()); } } Static void main () { Application. Run (New form1 ()); }
Private void button#click (Object sender, system. eventargs E) { Listbox1.items. Add ("the service has been started! "); _ Thread1 = new thread (New threadstart (Listen )); _ Thread1.start ();
}
Private void button2_click (Object sender, system. eventargs E) { // Close the thread and stream Networkstream. Close (); Streamreader. Close (); Streamwriter. Close (); _ Thread1.abort (); Tcplistener. Stop (); Socketforclient. Shutdown (socketshutdown. Both ); Socketforclient. Close (); } Private void formpolicclosed (Object sender, system. eventargs E) { // Close the thread and stream Networkstream. Close (); Streamreader. Close (); Streamwriter. Close (); _ Thread1.abort (); Tcplistener. Stop (); Socketforclient. Shutdown (socketshutdown. Both ); Socketforclient. Close (); } } |