網路協議體系(二)

發表於2013-06-17

   1.體系結構簡介

     計算機之間通訊分為很多種,有檔案通訊,web通訊等,需要一套協議體系來規定通訊的各種各樣的規則,以保證各種各樣的通訊

有條不行的進行.

    OSI的七層協議概念清楚,但並不實用,而現實中TCP/IP協議採用四層,而我們學習的時候才用折中的的方法,採用五層的體系結

構 如下圖.

                       

 

     2.用wireshark擷取SNMP協議在各層的報文

         1.應用層

            

         2.運輸層

              

         3.網路層

              

         4.資料鏈路層

             

      3.五層協議介紹

            1.應用層

                

                    

                    例子1 應用層HTTP協議

                       

                       實驗1.用telnet模擬http請求

                            

                      實驗2.用socket模擬http請求

                           

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace SocketTest
{
    class Program
    {
        private static Socket ConnectSocket(string server, int port)
        {
            Socket s = null;
            IPHostEntry hostEntry = null;

            // Get host related information.
            hostEntry = Dns.GetHostEntry(server);

            // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
            // an exception that occurs when the host IP Address is not compatible with the address family
            // (typical in the IPv6 case).
            foreach (IPAddress address in hostEntry.AddressList)
            {
                IPEndPoint ipe = new IPEndPoint(address, port);
                Socket tempSocket =
                    new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                tempSocket.Connect(ipe);

                if (tempSocket.Connected)
                {
                    s = tempSocket;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return s;
        }

        // This method requests the home page content for the specified server.
        private static string SocketSendReceive(string server, int port)
        {
            string request = "GET /btim3.0sp1/login.aspx HTTP/1.1\r\nHost: " + server +
                "\r\nConnection: Close\r\nAccept-Language:zh-cn\r\nAccept-Charset:utf-8\r\nAccept-Charset:iso-8859-1,gb2312\r\n\r\n";
            Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
            Byte[] bytesReceived = new Byte[256];

            // Create a socket connection with the specified server and port.
            Socket s = ConnectSocket(server, port);

            if (s == null)
                return ("Connection failed");

            // Send request to the server.
            s.Send(bytesSent, bytesSent.Length, 0);

            // Receive the server home page content.
            int bytes = 0;
            string page = "Default HTML page on " + server + ":\r\n";

            // The following will block until te page is transmitted.
            do
            {
                bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
                page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
            }
            while (bytes > 0);

            return page;
        }

        public static void Main(string[] args)
        {
            string host;
            int port = 88;

            if (args.Length == 0)
                // If no server name is passed as argument to this program, 
                // use the current host name as the default.
                host = Dns.GetHostName();
            else
                host = args[0];

            string result = SocketSendReceive(host, port);
            Console.WriteLine(result);
            Console.Read();
        }

    }
}
View Code

 

            2.運輸層

               

                     

                          當運輸層從IP層收到UDP資料包時,就根據首部中的目的埠,把UDP資料包通過相應的埠,

                   上交最後的終點--應用程式

            3.IP層

                 

                    網路層體系結構如下:

                    

                     IP資料包格式:

                       

            4.資料鏈路層

              

                 資料鏈路層報文格式:

                  

                  

            5.物理層

                

                  

      備註:本文的圖片全部來自與謝希仁的<<網路原理>>,在此感謝,另只是學習交流用。

相關文章