In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how to analyze the source code of the client program of C# network programming. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it.
How does the client implementation of C# network programming do? Since there is no need to listen on the network on the client side, there is no program blocking on the call, so we do not use threads in the following code, which is a difference from the server-side program. Summing up the key steps above, you can get a client program programmed with C# network, as follows:
/ / C # network programming client program 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; / / C # network programming client program / / namespace used in the import program 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 ();} / / C# network programming client program / / 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 (); 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:" / / C # network programming client program / / set other controls 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" in the same way This.Text = "C # network programming client!" ; this.Closed + = new System.EventHandler (this.Form1_Closed); this.ResumeLayout (false); / / Connect to the server port, where the local machine is selected as the server, / / you can change the server try {myclient = new TcpClient ("localhost", 1234) by changing the IP address;} catch {MessageBox.Show ("not connected to the server!" ); return;} / C # network programming client program / / create networkStream objects to receive and send data through network sockets 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;} try {string s; / / write a string streamWriter.WriteLine (textBox1.Text) to the current data stream; / / refresh the data streamWriter.Flush () in the current data stream; / / read a line of characters from the current data stream, and the return value is the string s = streamReader.ReadLine () ListBox1.Items.Add ("read from the server:" + s);} / / C# network programming client program catch (Exception ee) {MessageBox.Show ("error reading data from the server, type:" + ee.ToString ());}} private void Form1_Closed (object sender, System.EventArgs e) {streamReader.Close (); streamWriter.Close () NetworkStream.Close ();}} above is how to analyze the source code of the client program of C# network programming. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.