On the Network Programming C # (4) Author: Ma Jinhu www.ASPCool.com Time: 2002-6-25 18:02:43 Views: 7457
Fives. Partial code of the client: Since there is no listening network on the client, there is no program blocking situation above, in the following code, we did not use the thread, which is a difference between the server-side program.
Summarize these key steps, you can get a client program (Client.cs) programming with C # network, which is as follows: use system; using system.drawing; use system.collections; using system.componentmodel; use system.Componentmodel; Windows.Forms; using System.Data; using System.Net.Sockets; using System.IO; using System.Threading; // used in the program to import namespace public class Form1: Form {private ListBox ListBox1; private Label label1; private TextBox textBox1; private Button button3; private NetworkStream networkStream; private StreamReader streamReader; private streamWriter streamWriter; TcpClient myclient; private Label label2; private System.ComponentModel.Container components = null; public Form1 () {InitializeComponent ();} // Clear Various resources used in the program protected {if (disponents! = Null) {components.dispose ();}}}} {Label1 = new label () Button3 = new button (); listbox1 = new listbox (); textbox1 = new textbox (); label2 = new label (); suspendlayout (); label1.location = new point (8, 168); label1.name = " Label1 "; label1.size = new size (56, 23); label1.tabindex = 3; label1.text = information:"; // The same method sets other control autoscalebasesize = new size (6, 14); clientsize = new SIZE (424, 205); this.controls.add (button3); this.controls.add (TextBox1); this.controls.add (label1); this.controls.add (label2); this.controls.add (Listbox1 This.maximizebox = false; this.minimizebox =
False; this.name = "form1"; this.text = "C # network program client!"; this.closed = new system.EventHandler (this.form1_closed); this.ResumeLayout (false); // Connect to the server port, here is the selection of local machines as the server, you can change the server TRY {MyClient = New TcpClient ("LocalHost", 1234) by modifying the IP address;} catch {messagebox.show ("is not connected to the server! "); return;} // Create NetworkStream object to receive and send data over the network socket word networkStream = myclient.GetStream (); streamReader = new StreamReader (networkStream); streamWriter = new streamWriter (networkStream);} static void Main ( ) {Application.Run (new form1 ());} private void button3_click (object sender, system.eventargs e) {if (textbox1.text == ") {messagebox.show (" Please make sure the text box is not empty! "); TextBox1.focus (); return;} trying s; // Write a line string streamwriter.writeline (TextBox1.text) in the current data stream; // Refresh the data streamwriter in the current data stream. Flush (); // Read a line from the current data stream, the return value is a string S = streamreader.readLine (); listbox1.items.add ("Read Server-side sending content: " s);} catch (Exception EE) {MessageBox.show (" from server-side read data error, type: EE.TOSTRING ());}} private void form1_closed (Object) Sender, System.Eventargs e) {streamreader.close (); streamwriter.close (); networkStream.close (); networkStream.close ();}} The picture below is the interface that runs after the two programs above: Figure 01: C # Writer the network program Interface seven. Summary: Although only two namespaces are provided for network programming in .NET Framewrok SDK, the contents of these two namespaces are very rich, and C # uses these two namespaces to be synchronized and asynchronous, Blocking and non-blocking can also be achieved.