网络协议体系(二)

   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
Host: " + server +
                "
Connection: Close
Accept-Language:zh-cn
Accept-Charset:utf-8
Accept-Charset:iso-8859-1,gb2312

";
            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 + ":
";

            // 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.物理层

                

                  

      备注:本文的图片全部来自与谢希仁的<<网络原理>>,在此感谢,另只是学习交流用。

原文地址:https://www.cnblogs.com/liangjie/p/3140552.html