C# Socket編程

來源:互聯網
上載者:User

標籤:

using System;using System.IO;using System.Net;using System.Net.Sockets;using System.Text;namespace ConsoleApplication2{    public class Program    {        public static void Main(string[] args)        {            //取得預儲存的檔案名稱             string fileName = "index.html";            //遠程主機             string hostName = "www.baidu.com";            //連接埠             int port = 80;            //得到主機資訊             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)            {                Console.WriteLine(se.Message);            }            //發送給遠程主機的請求內容串             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)            {                Console.WriteLine(ce.Message);            }            //聲明接收返回內容的字串             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)            {                Console.WriteLine(fe.Message);            }            //禁用Socket             socket.Shutdown(SocketShutdown.Both);            //關閉Socket             socket.Close();        }    }}

 

C# Socket編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.