程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#Socket(一)

C#Socket(一)

編輯:C#入門知識

C#Socket基礎,歡迎來訪!

測試一:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace Socket_Learn
{
    /// 
    ///  @author ZJC
    ///  這章主要學習基本的socket一些類的用法,DNS類可以獲得主機的名稱和ip地址列表
    ///  報文=數據塊 。
    ///  報文(message)是網絡中交換與傳輸的數據單元,即站點一次性要發送的數據塊。
    ///  報文包含了將要發送的完整的數據信息,其長短很不一致,長度不限且可變。
    ///  報文也是網絡傳輸的單位,傳輸過程中會不斷的封裝成分組、包、幀來傳輸,
    ///  封裝的方式就是添加一些信息段,那些就是報文頭以一定格式組織起來的數據。
    ///  比如裡面有報文類型,報文版本,報文長度,報文實體等等信息。
    /// 
    class Program
    {
        static void Main(string[] args)
        {
            string HostName = Dns.GetHostName();//get this commputer's name
            Console.WriteLine("My computer's name = {0}",HostName);//XX的PC         
            IPHostEntry ipEntry = Dns.GetHostEntry(HostName);//get this conputer's IP address by it's name;
            IPAddress[] addr = ipEntry.AddressList;
            Console.WriteLine("I have get all the addresses in this computer,show it as follows:");
            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP address[{0}]:{1}",i,addr[i].ToString());
                Console.WriteLine("Address's sort:[{0}],{1}",i,addr[i].AddressFamily.ToString());
                //addressfamily:ipv4&ipv6&局域網
            }
                Console.ReadKey();
        }
    }
}

\

測試二:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SocketLearn2
{
    class Program
    {
        static void Main(string[] args)
        {

            IPAddress ipaddr = IPAddress.Parse("127.0.0.1");                        //IPAddress
            IPEndPoint ipep = new IPEndPoint(ipaddr,8080);                          //creat a IPEndPoint Instance
            Socket test_socket = new Socket(AddressFamily.InterNetwork, 
                                            SocketType.Stream, ProtocolType.Tcp);   //creat a instance of socket

            Console.WriteLine("AddressFamily:{0}",test_socket.AddressFamily);       //print our socket's information
            Console.WriteLine("SocketType:{0}",test_socket.SocketType);
            Console.WriteLine("ProtocolType:{0}",test_socket.ProtocolType);
            Console.WriteLine("Blocking:{0}",test_socket.Blocking);


            test_socket.Blocking = false;                               //change the attriubutes of Socket's instance
            Console.WriteLine("Connected:{0}",test_socket.Connected);

            test_socket.Bind(ipep);                                     //ues Bind() method to connect socket to LocalEndPoint
            IPEndPoint sock_ipe = (IPEndPoint)test_socket.LocalEndPoint;//if not Bind(),will throw a exception.because test_socket.LocalEndPoint = null!
            Console.WriteLine("LocalEndPoint:{0}",sock_ipe.ToString());

            test_socket.Close();                                        //close the socket
            Console.ReadKey();
        }
    }
}



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved