Chuyên đề lập trình mạng và internet

122 38 0
Chuyên đề lập trình mạng và internet

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chuyên đề Lập trình mạng internet 30 tiết lý thuyết maianhthuvn@gmail.com Học lập trình mạng để làm gì? • Có hiểu biết để quản trị dịch vụ có sẳn mạng tốt • Có kiến thức để lập tiện ích khai thác tài nguyên mạng internet • Bước đầu lập ứng dụng server client phục vụ cho mục đích riêng Nội dung • • • • • • Chương 1: Nhắc lại số khái niệm Chương 2: Liên lạc liệu NetBIOS Chương 3: Liên lạc liệu Pipe Chương 4: Liên lạc liệu Socket Chương 5: Lập trình socket với NET Chương 6: Một số tập ứng dụng Lưu ý học Ở phần NET, ví dụ viết C# VB.NET Bạn thích ngơn ngữ convert sang ngơn ngữ Chẳng hạn trang http://www.developerfusion.com/tools/convert/csharp-to-vb/ convert tốt: Chương Nhắc lại số khái niệm Mơ hình lớp OSI (Open Systems Interconnection) Các giao thức mạng Các giao thức (protocols) định hình gói chuẩn hố (standardized packets) liệu mà chúng lập cho việc chia sẻ thơng tin mạng • Transmission Control Protocol/Internet Protocol (TCP/IP) • Asynchronous Transfer Mode (ATM) • NetWare Internetwork Packet Exchange/Sequenced Packet Exchange (IPX/SPX) • NetBIOS Enhanced User Interface (NetBEUI) • AppleTalk • Data Link Control (DLC) • Infrared Data Association (IrDA) Kiến trúc lớp TCP/IP Giao thức TCP/IP ánh xạ thành mơ hình lớp kiểu mơ hình DARPA Bốn lớp mơ hình DARPA là: Application, Transport, Internet, Network Interface Mỗi lớp mơ hình DARPA tương ứng với một vài lớp mơ hình lớp OSI Các lớp kiến trúc TCP/IP Lớp giao tiếp mạng (Network Interface Layer) • Lớp Network Interface (cịn gọi lớp Network Access) chịu trách nhiệm đặt gói TCP/IP phần trung gian mạng nhận gói TCP/IP cuối phần trung gian mạng • TCP/IP thiết kế độc lập với phương pháp truy xuất, dạng khung (frame format) phần trung gian Theo cách này, TCP/IP sủ dụng để kết nối với kiểu mạng khác Chúng bao gồm công nhệ LAN Ethernet, Token Ring công nghệ WAN X.25, Frame Relay • Sự độc lập với cơng nghệ mạng làm cho TCP/IP thích nghi với công nghệ ATM (Asynchronous Transfer Mode ) • Lớp Network Interface đối sánh với lớp Data Link lớp Physical mơ hình OSI Các lớp kiến trúc TCP/IP (tiếp) Lớp internet (Internet Layer) • Internet Protocol (IP) giao thức có nhiệm vụ địa hố IP, chọn đường, phân mảnh tụ hội lại gói • Address Resolution Protocol (ARP) có nhiệm vụ phân giải địa Internet đến lớp Network Interface địa phần cứng • Internet Control Message Protocol (ICMP) có nhiệm vụ cung cấp chức dị tìm lỗi lập báo cáo lỗi cho thao tác nhận gói IP khơng thành cơng • Internet Group Management Protocol (IGMP) có nhiệm vụ quản lý nhóm IP multicast • Lớp Internet tương tự lớp Network mơ hình OSI Tìm hiểu lớp thuộc System.Net System.Net.Sockets Lớp IPAddress: IPAddress sử dụng để trình bày địa IP Ví dụ: using System; using System.Net; class AddressSample { public static void Main () { IPAddress a1 = IPAddress.Parse("192.168.1.1"); IPHostEntry h =Dns.GetHostByName(Dns.GetHostName()); IPAddress a2 = h.AddressList[0]; Console.WriteLine("The test address is: {0}", a1.ToString()); Console.WriteLine("The Local IP address is: {0}\n",a2.ToString()); } } Kết quả: The Local IP address is: 192.168.1.6 The test address is: 192.168.1.1 Lớp IPEndPoint: IPEndPoint tương tự cấu trúc địa socket (sockaddr_in) Ví dụ: using System; using System.Net; class IPEndPointSample { public static void Main () { IPAddress a = IPAddress.Parse("192.168.1.1"); IPEndPoint e = new IPEndPoint(a, 8000); Console.WriteLine("The IPEndPoint is: {0}", e.ToString()); Console.WriteLine("The AddressFamily is: {0}", e.AddressFamily); Console.WriteLine("The address is: {0}, and the port is: {1}\n", e.Address, e.Port); SocketAddress sa = e.Serialize(); Console.WriteLine("The SocketAddress is: {0}",sa.ToString()); } } Kết quả: The IPEndPoint is: 192.168.1.1:8000 The AddressFamily is: InterNetwork The address is: 192.168.1.1, and the port is: 8000 The SocketAddress is: InterNetwork:16:{0,80,192,168,1,1,0,0,0,0,0,0,0,0} Lớp Socket: Đối tượng Socket sử dụng tương tự sử dụng socket C, C++ (Chương 4) Ví dụ: Đoạn code Server sử dụng đối tượng Socket: IPHostEntry local = Dns.GetHostByName(Dns.GetHostName()); IPEndPoint iep = new IPEndPoint(local.AddressList[0], 8000); Socket sserver = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sserver.Bind(iep); sserver.Listen(5); Socket snew = sserver.Accept(); Và đoạn code sau Client: IPAddress host = IPAddress.Parse("192.168.1.1"); IPEndPoint hostep = new IPEndPoint(host, 8000); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.Connect(hostep); Lớp Socket (tiếp): Việc đẩy nhận sử dụng method Send(), Receive() Socket Ví dụ: using System; using System.Net; using System.Net.Sockets; using System.Text; class Client { public static void Main () { IPAddress host = IPAddress.Parse("192.168.1.1"); IPEndPoint hostep = new IPEndPoint(host, 8000); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { sock.Connect(hostep); } catch (SocketException e) { Console.WriteLine("Problem connecting to host"); sock.Close(); return; } try { sock.Send(Encoding.ASCII.GetBytes("testing")); } catch (SocketException e) { Console.WriteLine("Problem sending data"); sock.Close(); return; } sock.Close(); }} Lớp NetworkStream: • Lớp NetworkStream cung cấp method có việc đẩy nhận liệu kiểu Stream sockets theo mode block • Có thể sử dụng NetworkStream cho cách truyền đồng (synchronous) dị (asynchronous) • NetworkStream thường sử dụng với TcpClient, TcpListener, UdpClient Ví dụ: TcpClient sockclient = new TcpClient(“192.168.1.1", 8000); NetworkStream ns = sockclient.GetStream(); byte[] outbytes = Encoding.ASCII.GetBytes("Testing"); ns.Write(outbytes, 0, outbytes.Length); byte[] inbytes = new byte[1024]; ns.Read(inbytes, 0, inbytes.Length); string instring = Encoding.ASCII.GetString(inbytes); Console.WriteLine(instring); ns.Close(); Lớp TcpClient Là Helper class sử dụng cho kết nối TCP phía client Có cách sử dụng để kết nối với máy remote (server): 1) Cấu tử mặc định: TcpClient sockclient = new TcpClient(); sockclient.Connect(“192.168.1.1", 8000); 2) Chỉ đối tượng IPEndPoint IPAddress ia = Dns.GetHostByName(Dns.GetHostName()).AddressList[0]; IPEndPoint iep = new IPEndPoint(ia, 10232); TcpClient sockclient = new TcpClient(iep); sockclient.Connect(“192.168.1.1", 8000); 3) Chỉ remote host Cách hay sử dụng Nó khơng cần phải dùng method Connect() TcpClient sockclient = new TcpClient(“www.myweb.com", 8000); Ví dụ: TcpClient sockclient = new TcpClient(“192.168.1.1", 8000); NetworkStream ns = sockclient.GetStream(); byte[] outbytes = Encoding.ASCII.GetBytes("Testing"); ns.Write(outbytes, 0, outbytes.Length); byte[] inbytes = new byte[1024]; ns.Read(inbytes, 0, inbytes.Length); string instring = Encoding.ASCII.GetString(inbytes); Console.WriteLine(instring); ns.Close(); sockclient.Close(); Lớp TcpListener Là Helper class sử dụng server socket Có dạng tham số: •TcpListener(int port) •TcpListener(IPEndPoint ie) •TcpListener(IPAddress addr, int port) TcpListener.Start TcpListener.Stop bắt đầu dừng Listen Ví dụ: TcpListener sock1 = new TcpListener(9050); sock1.Start(); TcpClient sock2 = sock1.AcceptTcpClient(); NetworkStream ns = sock2.GetStream(); byte[] outbytes = Encoding.ASCII.GetBytes("Testing"); ns.Write(outbytes, 0, outbytes.Length); byte[] inbytes = new byte[1024]; ns.Read(inbytes, 0, inbytes.Length); string instring = Encoding.ASCII.GetString(inbytes); Console.WriteLine(instring); ns.Close(); sock2.Close(); sock1.Stop(); Một số ví dụ cho phần 5.2 Ví dụ 1: • Sử dụng lớp Socket • Hoạt động đơn giản sau: 1) Server listen 2) Client Connect với server chuyển message S1 đến server 3) Server đọc hiển thị message S1 chuyển cho client message S2 4) Client đọc message S2 hiển thị disconnect 5) Server lặp lại bước Server: Imports System Imports System.Net Imports System.Net.Sockets Imports System.Text Imports Microsoft.VisualBasic Public Class Server Public Shared data As String = Nothing Public Shared Sub Main() Dim bytes() As Byte = New [Byte](1024) {} Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName()) Dim ipAddress As IPAddress = ipHostInfo.AddressList(0) Dim localEndPoint As New IPEndPoint(ipAddress, 11000) ' Create a TCP/IP socket Dim listener As New Socket(AddressFamily.InterNetwork, _ SocketType.Stream, ProtocolType.Tcp) listener.Bind(localEndPoint) listener.Listen(10) While True Console.WriteLine("Waiting for a connection ") Dim handler As Socket = listener.Accept() data = Nothing While True bytes = New Byte(1024) {} Dim bytesRec As Integer = handler.Receive(bytes) data += Encoding.ASCII.GetString(bytes, 0, bytesRec) If data.IndexOf("") > -1 Then Exit While End If End While Console.WriteLine("Text received : {0}", data) Dim msg As Byte() = Encoding.ASCII.GetBytes(data) handler.Send(msg) handler.Shutdown(SocketShutdown.Both) handler.Close() End While End Sub End Class Client: Imports System Imports System.Net Imports System.Net.Sockets Imports System.Text Public Class Client Public Shared Sub Main() Dim bytes(1024) As Byte Dim host As IPAddress = IPAddress.Parse("192.168.1.1"); Dim remoteEP As New IPEndPoint(host, 11000) Dim sender As New Socket(AddressFamily.InterNetwork, _ SocketType.Stream, ProtocolType.Tcp) sender.Connect(remoteEP) Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()) Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test") Dim bytesSent As Integer = sender.Send(msg) Dim bytesRec As Integer = sender.Receive(bytes) Console.WriteLine("Echoed test = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec)) sender.Shutdown(SocketShutdown.Both) sender.Close() End Sub End Class Chương Một số tập ứng dụng • npbc# ...Học lập trình mạng để làm gì? • Có hiểu biết để quản trị dịch vụ có sẳn mạng tốt • Có kiến thức để lập tiện ích khai thác tài nguyên mạng internet • Bước đầu lập ứng dụng server... thơng tin mạng internet IP • Simple Network Management Protocol (SNMP) sử dụng hình quản trị mạng thiết bị mạng (routers, bridges, intelligent hubs) để tập hợp chuyển đổi thơng tin quản trị mạng Các... Sử dụng Thread lập trình pipe Hiểu Thread: Có thể cho tiến trình thực thread khác Cách thức đơn giản: Sử dụng hàm CreateThread để tạo thêm thread mới, tiến trình thực Khi tiến trình hồn tất,

Ngày đăng: 22/04/2021, 12:34

Mục lục

    Chuyên đề Lập trình mạng và internet

    Học lập trình mạng để làm gì?

    Lưu ý khi học

    Chương 1 Nhắc lại một số khái niệm

    Mô hình 7 lớp OSI (Open Systems Interconnection)

    Các giao thức mạng

    Kiến trúc lớp trong TCP/IP

    Liên lạc dữ liệu giữa các máy tính trên mạng

    Chương 2 Liên lạc dữ liệu bằng NetBIOS

    Tổ chức thực hiện lệnh của NetBIOS

Tài liệu cùng người dùng

Tài liệu liên quan