五.客户端的部分代码:
由于在客户端不需要侦听网络,所以在调用上面没有程序阻塞情况,所以在下面的代码中,我们没有使用到线程,这是和服务器端程序的一个区别的地方。总结上面的这些关键步骤,可以得到一个用C#网络编程 完整的客户端程序(client.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 ;
//导入程序中使用到的名字空间
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 ( ) ;
}
//清除程序中使用的各种资源
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 = "信息:" ;
//同样方法设置其他控件
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 ;
}
//创建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 ) ;
}
catch ( Exception ee )
{
MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" + ee.ToString ( ) ) ;
}
}
private void Form1_Closed ( object sender , System.EventArgs e )
{
streamReader.Close ( ) ;
streamWriter.Close ( ) ;
networkStream.Close ( ) ;
}
}
下图是编译上面二个程序后运行的界面:
图01:C#编写网络程序运行界面
七.总结:
虽然在.Net FrameWrok SDK 中只为网络编程提供了二个命名空间,但这二个命名空间中的内容却是十分丰富的,C#利用这二个命名空间既可以实现同步和异步,也可以实现阻塞和非阻塞。本文通过用C#编写一个网络上信息传输的程序,展现了其丰富的内容,由于篇幅所限,更深,更强大的功能还需要读者去实践、探索。
更多内容请看网络管理实用手册、网络故障手册、网络组网专题专题,或进入讨论组讨论。
由于在客户端不需要侦听网络,所以在调用上面没有程序阻塞情况,所以在下面的代码中,我们没有使用到线程,这是和服务器端程序的一个区别的地方。总结上面的这些关键步骤,可以得到一个用C#网络编程 完整的客户端程序(client.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 ;
//导入程序中使用到的名字空间
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 ( ) ;
}
//清除程序中使用的各种资源
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 = "信息:" ;
//同样方法设置其他控件
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 ;
}
//创建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 ) ;
}
catch ( Exception ee )
{
MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" + ee.ToString ( ) ) ;
}
}
private void Form1_Closed ( object sender , System.EventArgs e )
{
streamReader.Close ( ) ;
streamWriter.Close ( ) ;
networkStream.Close ( ) ;
}
}
下图是编译上面二个程序后运行的界面:
图01:C#编写网络程序运行界面
七.总结:
虽然在.Net FrameWrok SDK 中只为网络编程提供了二个命名空间,但这二个命名空间中的内容却是十分丰富的,C#利用这二个命名空间既可以实现同步和异步,也可以实现阻塞和非阻塞。本文通过用C#编写一个网络上信息传输的程序,展现了其丰富的内容,由于篇幅所限,更深,更强大的功能还需要读者去实践、探索。
- 用Photoshop给漂亮的烫发MM抠图
- Photoshop透明婚纱抠图大法
- Photoshop:让MM做个“变色龙”
- 用Photoshop来制作一款精美的宝宝照片墙
- Photoshop绝色美女通道抠图法
- 用Photoshop教你打造绚丽光芒效果
巧巧读书:http://www.qqread.com/csharp/p303745.html
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- 网络管理实用手册 (22499篇文章)
- 网络故障手册 (14532篇文章)
- 网络组网专题 (12555篇文章)
- 网络建设 (14095篇文章)
- Java编程开发手册 (8309篇文章)
- C# 3.0新特性之扩展方法 (1次浏览)
- Windows Shell 开发:从“桌面”开始展开 (0次浏览)
- 实验分析C#中三种计时器使用异同点 (0次浏览)
- C#调用QTP自动化对象模型的实例 (0次浏览)
- 利用 C# 实现任务栏通知窗口 (0次浏览)
- 深入C#学习系列之不可小瞧的using关键字 (0次浏览)
- 如何用.NET技术在线生成网站LOGO (0次浏览)
- C#实现遗传算法 模拟花朵的进化 (0次浏览)
- c#里的运算符重载 (0次浏览)
- QTP调用自己开发的.net类库 (0次浏览)



