.NET网络编程学习(三)
在上一節中,我們通過一個簡單的Http服務器程序學習了Socket服務器端的編程.這一節將通過一個簡單的網絡蜘蛛程序(Spider)來學習Socket客戶端的程序設計.
Spider是搜索引擎重要的組成部分,其基本的原理也比較簡單,但要真正寫一個能夠用于搜索引擎的Spider絕非一件易事。
從本質來說,Spider就是一個網頁下載程序,然后再對下載的網頁進行分析,提取,整理,然后交給索引程序處理生成索引。
?
而對于Socket客戶端來說,分以下幾步來實現連接服務器端:
(1)創建IPEndPoint實例和套接字
?IPAddress hostIp = Dns.GetHostEntry("http://www.hust.edu.cn").addresslist[0];
?IPEndPoint ep = new IPEndPoint(hostIp, 80);
?Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
(2)連接服務器端
?????????? try
??????????? {
??????????????? client.Connect(ep);
??????????? }
??????????? catch (SocketException e)
??????????? {
??????????????? Console.WriteLine(e.Message);
??????????? }
(3)發送請求
??????????? client.Send(Encoding.GetEncoding("us-ascii").GetBytes("GET /index.html HTTP/1.1\r\n"));
??????????? client.Send(Encoding.GetEncoding("us-ascii").GetBytes("Host:hust.edu.cn\r\n"));
??????????? client.Send(Encoding.GetEncoding("us-ascii").GetBytes("Connection:Close\r\n"));
??????????? client.Send(Encoding.GetEncoding("us-ascii").GetBytes("\r\n"));
(4)接收數據
??????????? StringBuilder recstr = new StringBuilder();
??????????? byte[] buff = new byte[1024];
??????????? int rCount=0;
??????????? while(true)
??????????? {
??????????????? rCount = client.Receive(buff, buff.Length, SocketFlags.None); //讀取數據
??????????????? if (rCount > 0)
??????????????? {
??????????????????? recstr.Append(ASCIIEncoding.ASCII.GetString(buff, 0, rCount));
??????????????? }
??????????????? else
??????????????????? break;
??????????? }
完整代碼如下:
?
Codeusing?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Net;
using?System.Net.Sockets;
using?System.IO;
namespace?MyHttpClient
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????IPAddress?hostIp?=?Dns.GetHostEntry("www.hust.edu.cn").AddressList[0];
????????????IPEndPoint?ep?=?new?IPEndPoint(hostIp,?80);
????????????Socket?client?=?new?Socket(AddressFamily.InterNetwork,?SocketType.Stream,?ProtocolType.Tcp);
????????????try
????????????{
????????????????client.Connect(ep);
????????????}
????????????catch?(SocketException?e)
????????????{
????????????????Console.WriteLine(e.Message);
????????????}
????????????//發送請求
????????????client.Send(Encoding.GetEncoding("us-ascii").GetBytes("GET?/index.html?HTTP/1.1\r\n"));
????????????client.Send(Encoding.GetEncoding("us-ascii").GetBytes("Host:hust.edu.cn\r\n"));
????????????client.Send(Encoding.GetEncoding("us-ascii").GetBytes("Connection:Close\r\n"));
????????????client.Send(Encoding.GetEncoding("us-ascii").GetBytes("\r\n"));
????????????StringBuilder?recstr?=?new?StringBuilder();
????????????byte[]?buff?=?new?byte[1024];
????????????int?rCount=0;
????????????while(true)
????????????{
????????????????rCount?=?client.Receive(buff,?buff.Length,?SocketFlags.None);?//讀取數據
????????????????if?(rCount?>?0)
????????????????{
????????????????????recstr.Append(ASCIIEncoding.ASCII.GetString(buff,?0,?rCount));
????????????????}
????????????????else
????????????????????break;
????????????}
????????????using?(StreamWriter?sw?=?new?StreamWriter(File.Create(@"c:\test.html")))
????????????{
????????????????sw.Write(recstr.ToString());?//寫入文件
????????????}
????????????Console.WriteLine("下載成功!");
????????????Console.Read();
????????}
????}
}
?
運行以上程序,將會把華工的主頁下載到你的c盤根目錄下.這恐怕是一個最簡單的Spider程序了.
轉載于:https://www.cnblogs.com/hustcat/archive/2008/08/12/1265961.html
總結
以上是生活随笔為你收集整理的.NET网络编程学习(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL LIKE 通配符随笔
- 下一篇: .NET MasterPage技术