In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to carry out C# network programming server-side program source code analysis, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
C # network programming server-side program to achieve the source code is what? Let's take a look at an important part of it:
Because the structure we use in this program is asynchronous blocking, in the actual program, in order not to affect the running speed of the server-side program, we have designed a thread in the program to listen for network requests, receive and send data are processed in the thread, please note this in the following code, the following is the complete code of the C# network programming server-side program:
/ / server.cs 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; / / C # Namespace used in server-side programs / / importers: Form {private ListBox ListBox1; private Button button2; private Label label1; private TextBox textBox1; private Button button1 Private Socket socketForClient; private NetworkStream networkStream; private TcpListener tcpListener; private StreamWriter streamWriter; private StreamReader streamReader; private Thread _ thread1; private System.ComponentModel.Container components = null; public Form1 () {InitializeComponent ();} / / C # Network programming various resources used in server-side programs / / clear programs 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:"; / / C# network programming server-side programs / / other controls are set in the same way, omitting 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 = "network programming server of C#!" ; this.Closed + = new System.EventHandler (this.Form1_Closed); this.ResumeLayout (false);} private void Listen () {/ / C # network programming server-side program / / create a tcpListener object, which is mainly to listen on a given port tcpListener = new TcpListener (1234); / / start listening on tcpListener.Start () / / returns the Socket instance socketForClient = tcpListener.AcceptSocket () that can be used to process the connection; try {/ / if the return value is "true", the resulting socket has accepted the connection request if (socketForClient.Connected) {ListBox1.Items.Add ("successfully connected with the client!" ); while (true) {/ / create networkStream objects to accept and send data networkStream = new NetworkStream (socketForClient) through network sockets; / / read a line of characters from the current data stream and return the string streamReader = new StreamReader (networkStream); string msg = streamReader.ReadLine (); ListBox1.Items.Add ("receive client message:" + msg); streamWriter = new StreamWriter (networkStream) If (textBox1.Text! = ") {ListBox1.Items.Add (" feedback to the client: "+ textBox1.Text); / / write a string streamWriter.WriteLine (textBox1.Text) to the current data stream; / / refresh the data in the current data stream / / C# network programming server-side program streamWriter.Flush () } catch (Exception ey) {MessageBox.Show (ey.ToString ());}} static void Main () {Application.Run (new Form1 ());} private void button1_Click (object sender, System.EventArgs e) {ListBox1.Items .Add ("Service started!" ); _ thread1 = new Thread (new ThreadStart (Listen)); _ thread1.Start ();} private void button2_Click (object sender, System.EventArgs e) {/ / C # network programming server-side programs / / close threads and streams networkStream.Close (); streamReader.Close (); streamWriter.Close (); _ thread1.Abort (); tcpListener.Stop (); socketForClient.Shutdown (SocketShutdown.Both) SocketForClient.Close ();} private void Form1_Closed (object sender, System.EventArgs e) {/ / C # network programming server-side programs / / close threads and streams networkStream.Close (); streamReader.Close (); streamWriter.Close (); _ thread1.Abort (); tcpListener.Stop (); socketForClient.Shutdown (SocketShutdown.Both); socketForClient.Close ();}}
C# network programming server-side procedures to achieve the source code is introduced to you here, I hope to help you understand and learn C# network programming server-side procedures.
The above content is how to carry out the source code analysis of C# network programming server-side programs. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to 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.