| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 824 人关注过本帖, 1 人收藏
标题:C#网络编程服务器端程序的部分源代码
只看楼主 加入收藏
reandy
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-15
收藏(1)
 问题点数:0 回复次数:2 
C#网络编程服务器端程序的部分源代码
C#网络编程服务器端程序的部分源代码(server.cs):

  由于在此次程序中我们采用的结构是异步阻塞方式,所以在实际的程序中,为了不影响服务器端程序的运行速度,我们在程序中设计了一个线程,使得对网络请求侦听,接受和发送数据都在线程中处理,请在下面的代码中注意这一点,下面是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 ;
//导入程序中使用到的名字空间
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 ( ) ;
}
//清除程序中使用的各种资源
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 = "往客户端反馈信息:" ;
//同样的方式设置其他控件,这里略去

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 = "C#的网络编程服务器端!" ;
this.Closed += new System.EventHandler ( this.Form1_Closed ) ;
this.ResumeLayout ( false ) ;

}
private void Listen ( )
{
//创建一个tcpListener对象,此对象主要是对给定端口进行侦听
tcpListener = new TcpListener ( 1234 ) ;
//开始侦听
tcpListener.Start ( ) ;
//返回可以用以处理连接的Socket实例
socketForClient = tcpListener.AcceptSocket ( ) ;
try
{
//如果返回值是"true",则产生的套节字已经接受来自远方的连接请求
if ( socketForClient.Connected )
{
ListBox1.Items.Add ( "已经和客户端成功连接!" ) ;
while ( true )
{
//创建networkStream对象通过网络套节字来接受和发送数据
networkStream = new NetworkStream ( socketForClient ) ;
//从当前数据流中读取一行字符,返回值是字符串
streamReader = new StreamReader ( networkStream ) ;
string msg = streamReader.ReadLine ( ) ;
ListBox1.Items.Add ( "收到客户端信息:" + msg ) ;
streamWriter = new StreamWriter ( networkStream ) ;
if ( textBox1.Text != "" )
{
ListBox1.Items.Add ( "往客户端反馈信息:" + textBox1.Text ) ;
//往当前的数据流中写入一行字符串
streamWriter.WriteLine ( textBox1.Text ) ;
//刷新当前数据流中的数据
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 ( "服务已经启动!" ) ;
_thread1 = new Thread ( new ThreadStart ( Listen ) ) ;
_thread1.Start ( ) ;

}

private void button2_Click ( object sender , System.EventArgs e )
{
//关闭线程和流
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 )
{
//关闭线程和流
networkStream.Close ( ) ;
streamReader.Close ( ) ;
streamWriter.Close ( ) ;
_thread1.Abort ( ) ;
tcpListener.Stop ( ) ;
socketForClient.Shutdown ( SocketShutdown.Both ) ;
socketForClient.Close ( ) ;
}
搜索更多相关主题的帖子: 源代码 网络 using 服务器 
2007-11-06 14:04
like5635
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2007-11-29
收藏
得分:0 
好东西啊。
2007-11-29 20:33
FenteLi
Rank: 1
来 自:上海
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-11-24
收藏
得分:0 
学习,。。。。。路过。。。。。。
2007-11-30 13:10
快速回复:C#网络编程服务器端程序的部分源代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012084 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved