首先我們解釋一下在網絡編程時候
所謂同步方式
其實在用C#進行網絡編程中
一.本文中介紹的程序設計及運行環境
(
(
二.服務器端程序設計的關鍵步驟以及解決辦法
在下面接受的程序中
(
//創建一個tcpListener對象
tcpListener = new TcpListener (
//開始偵聽
tcpListener
//返回可以用以處理連接的Socket實例
socketForClient = tcpListener
(
此時Socket實例已經產生
try
{
//如果返回值是
if ( socketForClient
{
ListBox
while ( true )
{
//創建networkStream對象通過網絡套節字來接受和發送數據
networkStream = new NetworkStream ( socketForClient ) ;
//從當前數據流中讀取一行字符
streamReader = new StreamReader ( networkStream ) ;
string msg = streamReader
ListBox
streamWriter = new StreamWriter ( networkStream ) ;
if ( textBox
{
ListBox
//往當前的數據流中寫入一行字符串
streamWriter
//刷新當前數據流中的數據
streamWriter
}
}
}
}
catch ( Exception ey )
{
MessageBox
}
(
//關閉線程和流
networkStream
streamReader
streamWriter
_thread
tcpListener
socketForClient
socketForClient
三.C#網絡編程服務器端程序的部分源代碼(server
由於在此次程序中我們采用的結構是異步阻塞方式
using System ;
using System
using System
using System
using System
using System
using System
using System
using System
using System
//導入程序中使用到的名字空間
public class Form
{
private ListBox ListBox
private Button button
private Label label
private TextBox textBox
private Button button
private Socket socketForClient ;
private NetworkStream networkStream ;
private TcpListener tcpListener ;
private StreamWriter streamWriter ;
private StreamReader streamReader ;
private Thread _thread
private System
public Form
{
InitializeComponent ( ) ;
}
//清除程序中使用的各種資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void InitializeComponent ( )
{
label
button
button
ListBox
textBox
SuspendLayout ( ) ;
label
label
label
label
label
//同樣的方式設置其他控件
this
this
this
this
this
this
this
this
this
this
this
}
private void Listen ( )
{
//創建一個tcpListener對象
tcpListener = new TcpListener (
//開始偵聽
tcpListener
//返回可以用以處理連接的Socket實例
socketForClient = tcpListener
try
{
//如果返回值是
if ( socketForClient
{
ListBox
while ( true )
{
//創建networkStream對象通過網絡套節字來接受和發送數據
networkStream = new NetworkStream ( socketForClient ) ;
//從當前數據流中讀取一行字符
streamReader = new StreamReader ( networkStream ) ;
string msg = streamReader
ListBox
streamWriter = new StreamWriter ( networkStream ) ;
if ( textBox
{
ListBox
//往當前的數據流中寫入一行字符串
streamWriter
//刷新當前數據流中的數據
streamWriter
}
}
}
}
catch ( Exception ey )
{
MessageBox
}
}
static void Main ( )
{
Application
}
private void button
{
ListBox
_thread
_thread
}
private void button
{
//關閉線程和流
networkStream
streamReader
streamWriter
_thread
tcpListener
socketForClient
socketForClient
}
private void Form
{
//關閉線程和流
networkStream
streamReader
streamWriter
_thread
tcpListener
socketForClient
socketForClient
}
}
四.客戶端程序設計的關鍵步驟以及解決辦法
(
我們采用的本地機既做服務器也做客戶機
//連接到服務器端口
try
{
myclient = new TcpClient (
}
catch
{
MessageBox
return ;
}
//創建networkStream對象通過網絡套節字來接受和發送數據
networkStream = myclient
streamReader = new StreamReader ( networkStream ) ;
streamWriter = new StreamWriter ( networkStream ) ;
(
在接受和發送數據上面
if ( textBox
{
MessageBox
textBox
return ;
}
try
{
string s ;
//往當前的數據流中寫入一行字符串
streamWriter
//刷新當前數據流中的數據
streamWriter
//從當前數據流中讀取一行字符
s = streamReader
ListBox
}
catch ( Exception ee )
{
MessageBox
}
(
streamReader
streamWriter
networkStream
五.客戶端的部分代碼
由於在客戶端不需要偵聽網絡
using System ;
using System
using System
using System
using System
using System
using System
using System
using System
//導入程序中使用到的名字空間
public class Form
{
private ListBox ListBox
private Label label
private TextBox textBox
private Button button
private NetworkStream networkStream ;
private StreamReader streamReader ;
private StreamWriter streamWriter ;
TcpClient myclient ;
private Label label
private System
public Form
{
InitializeComponent ( ) ;
}
//清除程序中使用的各種資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components
}
}
base
}
private void InitializeComponent ( )
{
label
button
ListBox
textBox
label
SuspendLayout ( ) ;
label
label
label
label
label
//同樣方法設置其他控件
AutoScaleBaseSize = new Size (
ClientSize = new Size (
this
this
this
this
this
this
this
this
this
this
this
//連接到服務器端口
try
{
myclient = new TcpClient (
}
catch
{
MessageBox
return ;
}
//創建networkStream對象通過網絡套節字來接受和發送數據
networkStream = myclient
streamReader = new StreamReader ( networkStream ) ;
streamWriter = new StreamWriter ( networkStream ) ;
}
static void Main ( )
{
Application
}
private void button
{
if ( textBox
{
MessageBox
textBox
return ;
}
try
{
string s ;
//往當前的數據流中寫入一行字符串
streamWriter
//刷新當前數據流中的數據
streamWriter
//從當前數據流中讀取一行字符
s = streamReader
ListBox
}
catch ( Exception ee )
{
MessageBox
}
}
private void Form
{
streamReader
streamWriter
networkStream
}
}
六.總結
雖然在
From:http://tw.wingwit.com/Article/program/net/201311/14603.html