構(gòu)造.NET環(huán)境下的頁(yè)面下載器 (2)
發(fā)表時(shí)間:2024-06-15 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]三.網(wǎng)頁(yè)下載器實(shí)例介紹: 最后,我就綜合以上.NET網(wǎng)絡(luò)編程的一些知識(shí),向大家展示一個(gè)很好的實(shí)例。該實(shí)例是一個(gè)運(yùn)用Socket的基于同步模式的客戶端應(yīng)用程序,它首先通過(guò)解析服務(wù)器的IP地址建立一個(gè)終結(jié)點(diǎn),同時(shí)創(chuàng)建一個(gè)基于流套接字的Socket連接,其運(yùn)用的協(xié)議是TCP協(xié)議。通過(guò)該...
三.網(wǎng)頁(yè)下載器實(shí)例介紹:
最后,我就綜合以上.NET網(wǎng)絡(luò)編程的一些知識(shí),向大家展示一個(gè)很好的實(shí)例。該實(shí)例是一個(gè)運(yùn)用Socket的基于同步模式的客戶端應(yīng)用程序,它首先通過(guò)解析服務(wù)器的IP地址建立一個(gè)終結(jié)點(diǎn),同時(shí)創(chuàng)建一個(gè)基于流套接字的Socket連接,其運(yùn)用的協(xié)議是TCP協(xié)議。通過(guò)該Socket就可以發(fā)送獲取網(wǎng)頁(yè)的命令,再通過(guò)該Socket獲得服務(wù)器上默認(rèn)的網(wǎng)頁(yè),最后通過(guò)文件流將獲得的數(shù)據(jù)寫(xiě)入本機(jī)文件。這樣就完成了網(wǎng)頁(yè)的下載工作了,程序運(yùn)行的效果如下所示:
程序的代碼如下:
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
{
///
/// Form1 的摘要說(shuō)明。
/// 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;
///
/// 必需的設(shè)計(jì)器變量。
/// private System.ComponentModel.Container
components = null;
public Form1()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent
// 調(diào)用后添加任何構(gòu)造函數(shù)代碼
}
///
/// 清理所有正在使用的資源。
/// protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// 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 = "服務(wù)器地址:";
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 = "開(kāi)始下載";
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
[1] [2] 下一頁(yè)