C # Network Programming Preliminary Exploration (2)

xiaoxiao2021-03-06  41

On the Network Programming C # (2) Author: Ma Jinhu www.ASPCool.com Time: 2002-6-25 18:00:29 Views: 7244

three. Part of the C # Network programming server-side program: Server.cs): Since the structure we use in this program is an asynchronous blocking method, in the actual program, in order not to affect the running speed of the server-side program, we A thread is designed in the program so that the network requests listening, accepting, and sending data are handled in the thread. Please pay attention to this in the code below. Here is the full code of Server.cs: use system; use 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; // importer used to namespace public class Form1: 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 ();} // clear the resources used in the program protected override void Dispose (bool disposi Ng) {if (disponents! = null) {components.dispose ();}} Base.dispose ();} 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 = "Go to Client Feedback Information:" // Set other controls here, this is slightly thus.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 program server side!"; This.closed = new system.eventhandler (this.form1_closed); this.ResumeLayout (false);} private void listen ()} / / Create a TCPListener object, this object is mainly listening to the giveholding port to listen to TCPListener = new TCPListener (1234); // Start listening tcplistener.Start (); // Return the socket instance SocketForClient = TCPListener.Acceptsocket (); try {// If the return value is "true", the generated set of jacket has accepted the connection request IF (socketForClient.connected) {listbox1.Items.Add ("has been successful with the client. Connection! "); While (TRUE) {// Creating a NetworkStream object Accepts and sends data networkStream = new networkStream (SocketforClient) from the network set, read the character string from the current data stream, the return value is a string StreamReader = new streamreader; string msg = streamreader.readline (); listbox1.items.add ("Received Client Information: MSG); streamwriter = New streamwriter; if (TextBox1.text! = ") {listbox1.items.add (" Go to Client Feedback: " TextBox1.text); // Write a string in the current data stream StreamWriter.writeline (TextBox1.text); // Refresh the data in the current data stream streamWriter.Flush ();}}}} catch (}}}} catch (}}}});}} static void main () {Application.run (New Form1 ());} private void button1_click (Object sender, system.eventargs e) {listbox1.items .add ("service has started! "); _Thread1 =

转载请注明原文地址:https://www.9cbs.com/read-72674.html

New Post(0)