Socket編程:
Microsoft.Net Framework爲應用程序訪問Internet提供了分層的、可擴展的以及受管轄的網絡服務,其名字空間System.Net和System.Net.Sockets包含豐富的類可以開發多種網絡應用程序。.Net類採用的分層結構允許應用程序在不同的控制級別上訪問網絡,開發人員可以根據需要選擇針對不同的級別編制程序,這些級別幾乎囊括了Internet的所有需要--從socket套接字到普通的請求/響應,更重要的是,這種分層是可以擴展的,能夠適應Internet不斷擴展的需要。
拋開ISO/OSI模型的7層構架,單從TCP/IP模型上的邏輯層面上看,.Net類可以視爲包含3個層次:請求/響應層、應用協議層、傳輸層。WebReqeust和WebResponse 代表了請求/響應層,支持Http、Tcp和Udp的類組成了應用協議層,而Socket類處於傳輸層。可以如下示意:
可見,傳輸層位於這個結構的最底層,當其上面的應用協議層和請求/響應層不能滿足應用程序的特殊需要時,就需要使用這一層進行Socket套接字編程。
而在.Net中,System.Net.Sockets 命名空間爲需要嚴密控制網絡訪問的開發人員提供了 Windows Sockets (Winsock) 接口的託管實現。System.Net 命名空間中的所有其他網絡訪問類都建立在該套接字Socket實現之上,如TCPClient、TCPListener 和 UDPClient 類封裝有關創建到 Internet 的 TCP 和 UDP 連接的詳細信息;NetworkStream類則提供用於網絡訪問的基礎數據流等,常見的許多Internet服務都可以見到Socket的蹤影,如Telnet、Http、Email、Echo等,這些服務儘管通訊協議Protocol的定義不同,但是其基礎的傳輸都是採用的Socket。
其實,Socket可以象流Stream一樣被視爲一個數據通道,這個通道架設在應用程序端(客戶端)和遠程服務器端之間,而後,數據的讀取(接收)和寫入(發送)均針對這個通道來進行。
可見,在應用程序端或者服務器端創建了Socket對象之後,就可以使用Send/SentTo方法將數據發送到連接的Socket,或者使用Receive/ReceiveFrom方法接收來自連接Socket的數據;
針對Socket編程,.NET 框架的 Socket 類是 Winsock32 API 提供的套接字服務的託管代碼版本。其中爲實現網絡編程提供了大量的方法,大多數情況下,Socket 類方法只是將數據封送到它們的本機 Win32 副本中並處理任何必要的安全檢查。如果你熟悉Winsock API函數,那麼用Socket類編寫網絡程序會非常容易,當然,如果你不曾接觸過,也不會太困難,跟隨下面的解說,你會發覺使用Socket類開發windows 網絡應用程序原來有規可尋,它們在大多數情況下遵循大致相同的步驟。
在使用之前,你需要首先創建Socket對象的實例,這可以通過Socket類的構造方法來實現:
public Socket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType);
其中,addressFamily 參數指定 Socket 使用的尋址方案,socketType 參數指定 Socket 的類型,protocolType 參數指定 Socket 使用的協議。
下面的示例語句創建一個 Socket,它可用於在基於 TCP/IP 的網絡(如 Internet)上通訊。
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
若要使用 UDP 而不是 TCP,需要更改協議類型,如下面的示例所示:
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
一旦創建 Socket,在客戶端,你將可以通過Connect方法連接到指定的服務器,並通過Send/SendTo方法向遠程服務器發送數據,而後可以通過Receive/ReceiveFrom從服務端接收數據;而在服務器端,你需要使用Bind方法綁定所指定的接口使Socket與一個本地終結點相聯,並通過Listen方法偵聽該接口上的請求,當偵聽到用戶端的連接時,調用Accept完成連接的操作,創建新的Socket以處理傳入的連接請求。使用完 Socket 後,記住使用 Shutdown 方法禁用 Socket,並使用 Close 方法關閉 Socket。其間用到的方法/函數有:
Socket.Connect方法:建立到遠程設備的連接
public void Connect(EndPoint remoteEP)(有重載方法)
Socket.Send 方法:從數據中的指示位置開始將數據發送到連接的 Socket。
public int Send(byte[], int, SocketFlags);(有重載方法)
Socket.SendTo 方法 將數據發送到特定終結點。
public int SendTo(byte[], EndPoint);(有重載方法)
Socket.Receive方法:將數據從連接的 Socket 接收到接收緩衝區的特定位置。
public int Receive(byte[],int,SocketFlags);
Socket.ReceiveFrom方法:接收數據緩衝區中特定位置的數據並存儲終結點。
public int ReceiveFrom(byte[], int, SocketFlags, ref EndPoint);
Socket.Bind 方法:使 Socket 與一個本地終結點相關聯:
public void Bind( EndPoint localEP );
Socket.Listen方法:將 Socket 置於偵聽狀態。
public void Listen( int backlog );
Socket.Accept方法:創建新的 Socket 以處理傳入的連接請求。
public Socket Accept();
Socket.Shutdown方法:禁用某 Socket 上的發送和接收
public void Shutdown( SocketShutdown how );
Socket.Close方法:強制 Socket 連接關閉
public void Close();
可以看出,以上許多方法包含EndPoint類型的參數,在Internet中,TCP/IP 使用一個網絡地址和一個服務端口號來唯一標識設備。網絡地址標識網絡上的特定設備;端口號標識要連接到的該設備上的特定服務。網絡地址和服務端口的組合稱爲終結點,在 .NET 框架中正是由 EndPoint 類表示這個終結點,它提供表示網絡資源或服務的抽象,用以標誌網絡地址等信息。.Net同時也爲每個受支持的地址族定義了 EndPoint 的子代;對於 IP 地址族,該類爲 IPEndPoint。IPEndPoint 類包含應用程序連接到主機上的服務所需的主機和端口信息,通過組合服務的主機IP地址和端口號,IPEndPoint 類形成到服務的連接點。
用到IPEndPoint類的時候就不可避免地涉及到計算機IP地址,.Net中有兩種類可以得到IP地址實例:
IPAddress類:IPAddress 類包含計算機在 IP 網絡上的地址。其Parse方法可將 IP 地址字符串轉換爲 IPAddress 實例。下面的語句創建一個 IPAddress 實例:
IPAddress myIP = IPAddress.Parse("192.168.1.2");
Dns 類:向使用 TCP/IP Internet 服務的應用程序提供域名服務。其Resolve 方法查詢 DNS 服務器以將用戶友好的域名(如"host.contoso.com")映射到數字形式的 Internet 地址(如 192.168.1.1)。Resolve方法 返回一個 IPHostEnty 實例,該實例包含所請求名稱的地址和別名的列表。大多數情況下,可以使用 AddressList 數組中返回的第一個地址。下面的代碼獲取一個 IPAddress 實例,該實例包含服務器 host.contoso.com 的 IP 地址。
IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
IPAddress ipAddress = ipHostInfo.AddressList[0];
你也可以使用GetHostName方法得到IPHostEntry實例:
IPHosntEntry hostInfo=Dns.GetHostByName("host.contoso.com")
在使用以上方法時,你將可能需要處理以下幾種異常:
SocketException異常:訪問Socket時操作系統發生錯誤引發
ArgumentNullException異常:參數爲空引用引發
ObjectDisposedException異常:Socket已經關閉引發
在掌握上面得知識後,下面的代碼將該服務器主機( host.contoso.com的 IP 地址與端口號組合,以便爲連接創建遠程終結點:
IPEndPoint ipe = new IPEndPoint(ipAddress,11000);
確定了遠程設備的地址並選擇了用於連接的端口後,應用程序可以嘗試建立與遠程設備的連接。下面的示例使用現有的 IPEndPoint 實例與遠程設備連接,並捕獲可能引發的異常:
try {
s.Connect(ipe);//嘗試連接
}
//處理參數爲空引用異常
catch(ArgumentNullException ae) {
Console.WriteLine("ArgumentNullException : {0}", ae.ToString());
}
//處理操作系統異常
catch(SocketException se) {
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch(Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
需要知道的是:Socket 類支持兩種基本模式:同步和異步。其區別在於:在同步模式中,對執行網絡操作的函數(如 Send 和 Receive)的調用一直等到操作完成後纔將控制返回給調用程序。在異步模式中,這些調用立即返回。
另外,很多時候,Socket編程視情況不同需要在客戶端和服務器端分別予以實現,在客戶端編制應用程序向服務端指定端口發送請求,同時編制服務端應用程序處理該請求,這個過程在上面的闡述中已經提及;當然,並非所有的Socket編程都需要你嚴格編寫這兩端程序;視應用情況不同,你可以在客戶端構造出請求字符串,服務器相應端口捕獲這個請求,交由其公用服務程序進行處理。以下事例語句中的字符串就向遠程主機提出頁面請求:
string Get = "GET / HTTP/1.1/r/nHost: " + server + "/r/nConnection: Close/r/n/r/n";
遠程主機指定端口接受到這一請求後,就可利用其公用服務程序進行處理而不需要另行編制服務器端應用程序。
綜合運用以上闡述的使用Visual C#進行Socket網絡程序開發的知識,下面的程序段完整地實現了Web頁面下載功能。用戶只需在窗體上輸入遠程主機名(Dns 主機名或以點分隔的四部分表示法格式的 IP 地址)和預保存的本地文件名,並利用專門提供Http服務的80端口,就可以獲取遠程主機頁面並保存在本地機指定文件中。如果保存格式是.htm格式,你就可以在Internet瀏覽器中打開該頁面。適當添加代碼,你甚至可以實現一個簡單的瀏覽器程序。
實現此功能的主要源代碼如下:
//"開始"按鈕事件
private void button1_Click(object sender, System.EventArgs e) {
//取得預保存的文件名
string fileName=textBox3.Text.Trim();
//遠程主機
string hostName=textBox1.Text.Trim();
//端口
int port=Int32.Parse(textBox2.Text.Trim());
//得到主機信息
IPHostEntry ipInfo=Dns.GetHostByName(hostName);
//取得IPAddress[]
IPAddress[] ipAddr=ipInfo.AddressList;
//得到ip
IPAddress ip=ipAddr[0];
//組合出遠程終結點
IPEndPoint hostEP=new IPEndPoint(ip,port);
//創建Socket 實例
Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
//嘗試連接
socket.Connect(hostEP);
}
catch(Exception se)
{
MessageBox.Show("連接錯誤"+se.Message,"提示信息
,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
//發送給遠程主機的請求內容串
string sendStr="GET / HTTP/1.1/r/nHost: " + hostName +
"/r/nConnection: Close/r/n/r/n";
//創建bytes字節數組以轉換髮送串
byte[] bytesSendStr=new byte[1024];
//將發送內容字符串轉換成字節byte數組
bytesSendStr=Encoding.ASCII.GetBytes(sendStr);
try
{
//向主機發送請求
socket.Send(bytesSendStr,bytesSendStr.Length,0);
}
catch(Exception ce)
{
MessageBox.Show("發送錯誤:"+ce.Message,"提示信息
,MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
//聲明接收返回內容的字符串
string recvStr="";
//聲明字節數組,一次接收數據的長度爲1024字節
byte[] recvBytes=new byte[1024];
//返回實際接收內容的字節數
int bytes=0;
//循環讀取,直到接收完所有數據
while(true)
{
bytes=socket.Receive(recvBytes,recvBytes.Length,0);
//讀取完成後退出循環
if(bytes<=0)
break;
//將讀取的字節數轉換爲字符串
recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes);
}
//將所讀取的字符串轉換爲字節數組
byte[] content=Encoding.ASCII.GetBytes(recvStr);
try
{
//創建文件流對象實例
FileStream fs=new FileStream(fileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);
//寫入文件
fs.Write(content,0,content.Length);
}
catch(Exception fe)
{
MessageBox.Show("文件創建/寫入錯誤:"+fe.Message,"提示信息",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
//禁用Socket
socket.Shutdown(SocketShutdown.Both);
//關閉Socket
socket.Close();
}
)
示例1:使用Socket
server端代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketServer
{
public partial class Server : Form
{
public Server()
{
InitializeComponent();
}
Thread mythread;
Socket socket;
delegate void SetTextCallback(string text);
// 清理所有正在使用的資源。
protected override void Dispose(bool disposing)
{
try
{
socket.Close();//釋放資源
mythread.Abort();//中止線程
}
catch { }
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
public static IPAddress GetServerIP()
{
IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName());
return ieh.AddressList[0];
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.label1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.label1.Text = text;
}
}
private void SetListText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.listBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetListText);
this.Invoke(d, new object[] { text });
}
else
{
this.listBox1.Items.Add(text);
}
}
private void BeginListen()
{
IPAddress ServerIp = GetServerIP();
IPEndPoint iep = new IPEndPoint(ServerIp, 8000);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] byteMessage = new byte[100];
//this.label1.Text = iep.ToString(); //在C#2。0中非安全
this.SetText(iep.ToString()); //安全線程
socket.Bind(iep);
// do
while (true)
{
try
{
socket.Listen(5);
Socket newSocket = socket.Accept();
newSocket.Receive(byteMessage);
string sTime = DateTime.Now.ToShortTimeString();
string msg = sTime + ":" + "Message from:";
msg += newSocket.RemoteEndPoint.ToString() + Encoding.Default.GetString(byteMessage);
//this.listBox1.Items.Add(msg);
this.SetListText(msg);
}
catch (SocketException ex)
{
//this.label1.Text += ex.ToString();
this.SetText(ex.ToString());
}
}
// while(byteMessage!=null);
}
//開始監聽
private void button1_Click_1(object sender, EventArgs e)
{
try
{
mythread = new Thread(new ThreadStart(BeginListen));
mythread.Start();
}
catch (System.Exception er)
{
MessageBox.Show(er.Message, "完成", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
}
client代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace SocketClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
BeginSend();
}
private void BeginSend()
{
string ip = this.txtip.Text;
string port = this.txtport.Text;
IPAddress serverIp = IPAddress.Parse(ip);
int serverPort = Convert.ToInt32(port);
IPEndPoint iep = new IPEndPoint(serverIp, serverPort);
byte[] byteMessage;
// do
// {
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(iep);
byteMessage = Encoding.ASCII.GetBytes(textBox1.Text);
socket.Send(byteMessage);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
// }
// while(byteMessage!=null);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
示例2:使用TcpListener
server端代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets; //使用到TcpListen類
using System.Threading; //使用到線程
using System.IO; //使用到StreamReader類
using System.Net;
namespace SocketTcpServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int port = 8000; //定義偵聽端口號
private Thread thThreadRead; //創建線程,用以偵聽端口號,接收信息
private TcpListener tlTcpListen; //偵聽端口號
private bool blistener = true; //設定標示位,判斷偵聽狀態
private NetworkStream nsStream; //創建接收的基本數據流
private StreamReader srRead;
//private System.Windows.Forms.StatusBar statusBar1;
//private System.Windows.Forms.Button button1;
//private System.Windows.Forms.ListBox listBox1; //從網絡基礎數據流中讀取數據
private TcpClient tcClient;
delegate void SetTextCallback(string text);
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.statusBar1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.leftlab.Text = text;
}
}
private void SetListText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.listBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetListText);
this.Invoke(d, new object[] { text });
}
else
{
this.listBox1.Items.Add(text);
}
}
private void Listen()
{
try
{
IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName());
tlTcpListen = new TcpListener(ieh.AddressList[0], port); //以端口號來初始化TcpListener實例
tlTcpListen.Start(); //開始監聽
//statusBar1.Text = "正在監聽...";
this.SetText(ieh.AddressList[0].ToString()+"正在監聽...");
tcClient = tlTcpListen.AcceptTcpClient(); //通過TCP連接請求
nsStream = tcClient.GetStream(); //獲取用以發送、接收數據的網絡基礎數據流
srRead = new StreamReader(nsStream);//以得到的網絡基礎數據流來初始化StreamReader實例
//statusBar1.Text = "已經連接!";
this.SetText("已經連接!");
while (blistener) //循環偵聽
{
string sMessage = srRead.ReadLine();//從網絡基礎數據流中讀取一行數據
if (sMessage == "STOP") //判斷是否爲斷開TCP連接控制碼
{
tlTcpListen.Stop(); //關閉偵聽
nsStream.Close(); //釋放資源
srRead.Close();
//statusBar1.Text = "連接已經關閉!";
this.SetText("連接已經關閉!");
thThreadRead.Abort(); //中止線程
return;
}
string sTime = DateTime.Now.ToShortTimeString(); //獲取接收數據時的時間
//listBox1.Items.Add(sTime + " " + sMessage);
this.SetListText(sTime + " " + sMessage);
}
}
catch (System.Security.SecurityException)
{
MessageBox.Show("偵聽失敗!", "錯誤");
}
}
// 清理所有正在使用的資源。
protected override void Dispose(bool disposing)
{
try
{
tlTcpListen.Stop(); //關閉偵聽
nsStream.Close();
srRead.Close();//釋放資源
thThreadRead.Abort();//中止線程
}
catch { }
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
//開始監聽
private void button1_Click_1(object sender, EventArgs e)
{
thThreadRead = new Thread(new ThreadStart(Listen));
thThreadRead.Start();//啓動線程
button1.Enabled = false;
}
}
}
client代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets; //使用到TcpListen類
using System.Threading; //使用到線程
using System.IO; //使用到StreamWriter類
using System.Net; //使用IPAddress類、IPHostEntry類等
namespace SocketTcpClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private StreamWriter swWriter; //用以向網絡基礎數據流傳送數據
private NetworkStream nsStream; //創建發送數據的網絡基礎數據流
private TcpClient tcpClient;
private bool tcpConnect = false; //定義標識符,用以表示TCP連接是否建立
//連接
private void button1_Click(object sender, System.EventArgs e)
{
IPAddress ipRemote ;
try
{
ipRemote = IPAddress.Parse ( textBox1.Text ) ;
}
catch //判斷給定的IP地址的合法性
{
MessageBox.Show ( "輸入的IP地址不合法!" , "錯誤提示!" ) ;
return ;
}
IPHostEntry ipHost ;
try
{
ipHost = Dns.GetHostEntry( textBox1.Text ) ;
}
catch //判斷IP地址對應主機是否在線
{
MessageBox.Show ("遠程主機不在線!" , "錯誤提示!" ) ;
return ;
}
string sHostName = ipHost.HostName ;
try
{
TcpClient tcpClient = new TcpClient(sHostName,8000);//對遠程主機的端口提出TCP連接申請
nsStream = tcpClient.GetStream();//通過申請,並獲取傳送數據的網絡基礎數據流
swWriter = new StreamWriter(nsStream);//使用獲取的網絡基礎數據流來初始化StreamWriter實例
button1.Enabled = false ;
button2.Enabled = true ;
tcpConnect = true ;
lblStatus.Text = "已經連接!" ;
}
catch
{
MessageBox.Show ( "無法和遠程主機端口建立連接!" , "錯誤提示!" ) ;
return ;
}
}
//發送
private void button2_Click(object sender, System.EventArgs e)
{
if (textBox2.Text !="")
{
swWriter.WriteLine(textBox2.Text);//刷新當前數據流中的數據
swWriter.Flush();
}
else
{
MessageBox.Show("發送信息不能爲空!","錯誤提示!");
}
}
// 清理所有正在使用的資源。
protected override void Dispose( bool disposing )
{
if ( tcpConnect )
{
swWriter.WriteLine ( "STOP" ) ; //發送控制碼
swWriter.Flush (); //刷新當前數據流中的數據
nsStream.Close (); //清除資源
swWriter.Close ();
}
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
}
}