程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#網絡編程初探(2)

C#網絡編程初探(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 ( ) ;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved