,歡迎訪問網頁設計愛好者web開發。三.網頁下載器實例介紹: 
  最后,我就綜合以上.net網絡編程的一些知識,向大家展示一個很好的實例。該實例是一個運用socket的基于同步模式的客戶端應用程序,它首先通過解析服務器的ip地址建立一個終結點,同時創建一個基于流套接字的socket連接,其運用的協議是tcp協議。通過該socket就可以發送獲取網頁的命令,再通過該socket獲得服務器上默認的網頁,最后通過文件流將獲得的數據寫入本機文件。這樣就完成了網頁的下載工作了
程序的代碼如下: 
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.net;
using system.net.sockets;
using system.text;
using system.io;
namespace socketsample
{
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.button download;
private system.windows.forms.textbox serveraddress;
private system.windows.forms.textbox filename;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private system.componentmodel.container 
 components = null;
public form1()
{
//
// windows 窗體設計器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 
// 調用后添加任何構造函數代碼
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
this.label1 = new system.windows.
 forms.label();
this.label2 = new system.windows.
 forms.label();
this.download = new system.windows.
 forms.button();
this.serveraddress = new system.windows.
 forms.textbox();
this.filename = new system.windows.
 forms.textbox();
this.suspendlayout();
// 
// label1
// 
this.label1.location = new system.drawing.
 point(16, 24);
this.label1.name = "label1";
this.label1.size = new system.drawing.
 size(80, 23);
this.label1.tabindex = 0;
this.label1.text = "服務器地址:";
this.label1.textalign = system.drawing.
 contentalignment.middleright;
// 
// label2
// 
this.label2.location = new system.drawing.
 point(16, 64);
this.label2.name = "label2";
this.label2.size = new system.drawing.
 size(80, 23);
this.label2.tabindex = 1;
this.label2.text = "本地文件名:";
this.label2.textalign = system.drawing.
 contentalignment.middleright;
// 
// download
// 
this.download.location = new system.
 drawing.point(288, 24);
this.download.name = "download";
this.download.tabindex = 2;
this.download.text = "開始下載";
this.download.click += new system.
 eventhandler(this.download_click);
// 
// serveraddress
// 
this.serveraddress.location = new system.
 drawing.point(96, 24);
this.serveraddress.name = "serveraddress";
this.serveraddress.size = new system.
 drawing.size(176, 21);
this.serveraddress.tabindex = 3;
this.serveraddress.text = "";
// 
// filename
// 
this.filename.location = new system.
 drawing.point(96, 64);
this.filename.name = "filename";
this.filename.size = new system.
 drawing.size(176, 21);
this.filename.tabindex = 4;
this.filename.text = "";
// 
// form1
// 
this.autoscalebasesize = new system.
 drawing.size(6, 14);
this.clientsize = new system.drawing.
 size(376, 117);
this.controls.addrange(new system.windows.
 forms.control[] {
 this.filename,
 this.serveraddress,
 this.download,
 this.label2,
 this.label1});
 this.name = "form1";
 this.text = "網頁下載器";
 this.resumelayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main() 
{
application.run(new form1());
}
private string dosocketget(string server) 
{
//定義一些必要的變量以及一條要發送到服務器的字符串
encoding ascii = encoding.ascii;
string get = "get / http/1.1/r/nhost: " 
 +server+"/r/nconnection: close/r/n/r/n";
byte[] byteget = ascii.getbytes(get);
byte[] recvbytes = new byte[256];
string strretpage = null;
//獲取服務器相關的ip地址列表,其中第一項即為我們所需的
ipaddress hostadd = dns.resolve(server).
 addresslist[0];
//根據獲得的服務器的ip地址創建一個終結點,端口為默認的80
ipendpoint ephost = new ipendpoint
 (hostadd, 80);
 
//創建一個socket實例
socket s = new socket(addressfamily.
 internetwork, sockettype.stream,
protocoltype.tcp );
try
{
//用上面所取得的終結點連接到服務器
s.connect(ephost);
}
catch(exception se)
{
 messagebox.show("連接錯誤:"+se.
 message,"提示信息",messageboxbuttons.
 retrycancel,messageboxicon.information); 
}
if (!s.connected)
{
strretpage = "不能連接到服務器!";
return strretpage;
}
try
{
//向服務器發送get命令
s.send(byteget, byteget.length, 
socketflags.none);
}
catch(exception ce)
{
 messagebox.show("發送錯誤:"+ce.
 message,"提示信息",messageboxbuttons.
 retrycancel,messageboxicon.information);
}
//接收頁面數據,直到所有字節接收完畢
int32 bytes = s.receive(recvbytes, 
 recvbytes.length, 0);
strretpage = "以下是在服務器" + server +
 "上的默認網頁:/r/n";
strretpage = strretpage + ascii.getstring
(recvbytes, 0, bytes);
 
while (bytes > 0)
{
 bytes = s.receive(recvbytes, 
 recvbytes.length, socketflags.none);
 strretpage = strretpage + ascii.
 getstring(recvbytes, 0, bytes);
}
//禁用并關閉socket實例
s.shutdown(socketshutdown.both); 
s.close();
return strretpage;
}
private void download_click(object sender, system.
 eventargs e)
{
//將所讀取的字符串轉換為字節數組
byte[] content=encoding.ascii.getbytes
 (dosocketget(serveraddress.text));
try
{
//創建文件流對象實例
filestream fs=new filestream
 (filename.text,filemode.openorcreate,fileaccess.
 readwrite); 
//寫入文件
fs.write(content,0,content.length);
}
catch(exception fe)
{
messagebox.show("文件創建/寫入錯誤:
"+fe.message,"提示信息",messageboxbuttons.
retrycancel,messageboxicon.information);
}
}
}
}
 
其中主要的函數為dosocketget(),首先程序在響應"開始下載"的事件處理函數download_click(),調用dosocketget()函數,該函數完成了套接字的創建、連接、與主機的通訊-即獲得主機上的網頁、禁用、關閉等功能。在調用完dosocketget()函數后,download_click()函數創建一個filestream對象,并試圖將dosocketget()函數返回的網頁文件以字節數組的形式寫到本機文件中,最終形成在本機上的一個html文件,這樣就完成了一個網頁文件的下載工作了。 
  不過這個程序的功能比較簡單,不能作為真正的網頁瀏覽器用,網頁文件下載后還是要用ie等瀏覽器才能打開。然而作為一個解釋.net底層網絡編程的實例絕對是一個好例子,所以希望讀者能好好研究,同時讀者還可以添加文件下載進度條等以完善本程序。 
  注:以上程序在windows 2000服務器版、visual studio.net中文正式版下調試通過。