C#网络编程客户端程序实现是如何办到的呢?由于在客户端不需要侦听网络,所以在调用上面没有程序阻塞情况,所以在下面的代码中,我们没有使用到线程,这是和服务器端程序的一个区别的地方。总结上面的这些关键步骤,可以得到一个用C#网络编程客户端程序,具体如下:
复制
//C#网络编程客户端程序 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#网络编程客户端程序 //导入程序中使用到的名字空间 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#网络编程客户端程序 //清除程序中使用的各种资源 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 = "信息:" ; //C#网络编程客户端程序 //同样方法设置其他控件 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#的网络编程客户器端!" ; this.Closed += new System.EventHandler ( this.Form1_Closed ) ; this.ResumeLayout ( false ) ; //连接到服务器端口,在这里是选用本地机器作为服务器, //你可以通过修改IP地址来改变服务器 try { myclient = new TcpClient ( "localhost" , 1234 ) ; } catch { MessageBox.Show ( "没有连接到服务器!" ) ; return ; } //C#网络编程客户端程序 //创建networkStream对象通过网络套节字来接受和发送数据 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 ( "请确定文本框为非空!" ) ; textBox1.Focus ( ) ; return ; } try { string s ; //往当前的数据流中写入一行字符串 streamWriter.WriteLine ( textBox1.Text ) ; //刷新当前数据流中的数据 streamWriter.Flush ( ) ; //从当前数据流中读取一行字符,返回值是字符串 s = streamReader.ReadLine ( ) ; ListBox1.Items.Add ( "读取服务器端发送内容:" + s ) ; } //C#网络编程客户端程序 catch ( Exception ee ) { MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" + ee.ToString ( ) ) ; } } private void Form1_Closed ( object sender , System.EventArgs e ) { streamReader.Close ( ) ; streamWriter.Close ( ) ; networkStream.Close ( ) ; } }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
C#网络编程客户端程序的具体实现代码就向你介绍到这里,希望地你了解和开发C#网络编程客户端程序有所帮助。
【编辑推荐】