新聞中心
C#網(wǎng)絡(luò)編程服務(wù)器端程序?qū)崿F(xiàn)源碼是怎么樣的呢?讓我們來看看其中重要的一部分:

公司主營業(yè)務(wù):網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出獻(xiàn)縣免費(fèi)做網(wǎng)站回饋大家。
由于在此次程序中我們采用的結(jié)構(gòu)是異步阻塞方式,所以在實(shí)際的程序中,為了不影響服務(wù)器端程序的運(yùn)行速度,我們?cè)诔绦蛑性O(shè)計(jì)了一個(gè)線程,使得對(duì)網(wǎng)絡(luò)請(qǐng)求偵聽,接受和發(fā)送數(shù)據(jù)都在線程中處理,請(qǐng)?jiān)谙旅娴拇a中注意這一點(diǎn),下面是C#網(wǎng)絡(luò)編程服務(wù)器端程序的完整代碼:
- //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 ;
- //C#網(wǎng)絡(luò)編程服務(wù)器端程序
- //導(dǎo)入程序中使用到的名字空間
- 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 ( ) ;
- }
- //C#網(wǎng)絡(luò)編程服務(wù)器端程序
- //清除程序中使用的各種資源
- 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 = "往客戶端反饋信息:" ;
- //C#網(wǎng)絡(luò)編程服務(wù)器端程序
- //同樣的方式設(shè)置其他控件,這里略去
- 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#的網(wǎng)絡(luò)編程服務(wù)器端!" ;
- this.Closed += new System.EventHandler ( this.Form1_Closed ) ;
- this.ResumeLayout ( false ) ;
- }
- private void Listen ( )
- {
- //C#網(wǎng)絡(luò)編程服務(wù)器端程序
- //創(chuàng)建一個(gè)tcpListener對(duì)象,此對(duì)象主要是對(duì)給定端口進(jìn)行偵聽
- tcpListener = new TcpListener ( 1234 ) ;
- //開始偵聽
- tcpListener.Start ( ) ;
- //返回可以用以處理連接的Socket實(shí)例
- socketForClient = tcpListener.AcceptSocket ( ) ;
- try
- {
- //如果返回值是"true",則產(chǎn)生的套節(jié)字已經(jīng)接受來自遠(yuǎn)方的連接請(qǐng)求
- if ( socketForClient.Connected )
- {
- ListBox1.Items.Add ( "已經(jīng)和客戶端成功連接!" ) ;
- while ( true )
- {
- //創(chuàng)建networkStream對(duì)象通過網(wǎng)絡(luò)套節(jié)字來接受和發(fā)送數(shù)據(jù)
- networkStream = new NetworkStream ( socketForClient ) ;
- //從當(dāng)前數(shù)據(jù)流中讀取一行字符,返回值是字符串
- streamReader = new StreamReader ( networkStream ) ;
- string msg = streamReader.ReadLine ( ) ;
- ListBox1.Items.Add ( "收到客戶端信息:" + msg ) ;
- streamWriter = new StreamWriter ( networkStream ) ;
- if ( textBox1.Text != "" )
- {
- ListBox1.Items.Add ( "往客戶端反饋信息:" +
- textBox1.Text ) ;
- //往當(dāng)前的數(shù)據(jù)流中寫入一行字符串
- streamWriter.WriteLine ( textBox1.Text ) ;
- //刷新當(dāng)前數(shù)據(jù)流中的數(shù)據(jù)
- //C#網(wǎng)絡(luò)編程服務(wù)器端程序
- 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 ( "服務(wù)已經(jīng)啟動(dòng)!" ) ;
- _thread1 = new Thread ( new ThreadStart ( Listen ) ) ;
- _thread1.Start ( ) ;
- }
- private void button2_Click ( object sender ,
- System.EventArgs e )
- {
- //C#網(wǎng)絡(luò)編程服務(wù)器端程序
- //關(guān)閉線程和流
- 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 )
- {
- //C#網(wǎng)絡(luò)編程服務(wù)器端程序
- //關(guān)閉線程和流
- networkStream.Close ( ) ;
- streamReader.Close ( ) ;
- streamWriter.Close ( ) ;
- _thread1.Abort ( ) ;
- tcpListener.Stop ( ) ;
- socketForClient.Shutdown ( SocketShutdown.Both ) ;
- socketForClient.Close ( ) ;
- }
- }
C#網(wǎng)絡(luò)編程服務(wù)器端程序的實(shí)現(xiàn)源碼就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#網(wǎng)絡(luò)編程服務(wù)器端程序有所幫助。
【編輯推薦】
- C#客戶端程序?qū)崿F(xiàn)同步傳輸字符串淺析
- ASP.NET異步回調(diào)淺析
- ASP.NET異步回調(diào)開發(fā)實(shí)例淺析
- C#網(wǎng)絡(luò)編程入門基礎(chǔ)知識(shí)淺析
- C#網(wǎng)絡(luò)編程服務(wù)器端程序設(shè)計(jì)淺析
網(wǎng)站名稱:C#網(wǎng)絡(luò)編程服務(wù)器端程序?qū)崿F(xiàn)源碼淺析
網(wǎng)頁鏈接:http://m.fisionsoft.com.cn/article/djggeij.html


咨詢
建站咨詢
