1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài giảng Lập trình mạng: Chương 4 - ThS. Trần Đắc Tốt

30 6 0

Đ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

Bài giảng Lập trình mạng - Chương 4: Sử dụng các lớp trợ giúp cung cấp cho người học các kiến thức: Giới thiệu, lớp TcpClient, lớp TcpListener, lớp UdpClient. Đây là một tài liệu hữu ích dành cho các bạn sinh viên và những ai quan tâm dùng làm tài liệu học tập và nghiên cứu.

Chương Sử dụng lớp trợ giúp Mục lục chương Giới thiệu Lớp TcpClient Lớp TcpListener Lớp UdpClient Giới thiệu • Ở chương trước học lớp Socket mức thấp để tạo ứng dụng TCP UDP • Tuy nhiên người lập trình mạng thường cảm thấy lớp khó hiểu dễ nhầm lẫn • Chương cung cấp lớp trợ giúp nhằm đơn giản trình lập trình mạng Lớp TcpClient • Lớp TcpClient đặt System.Net.Sockets namespace • Được thiết kế để trợ giúp cho việc xây dựng ứng dụng TCP Client Hàm tạo • Cơ hàm tạo lớp TcpClient là: • TcpClient() • Hàm tạo tạo đối tượng TcpClient gắn với địa cục hệ thống đồng thời gắn với địa cổng ngẫu nhiên • Sau gọi hàm tạo ta phải kết nối đến máy tính xa sử dụng phương thức Connect() Hàm tạo • Ví dụ – TcpClient newcon = new TcpClient(); newcon.Connect("www.ispnet.net", 8000); Hàm tạo • Hàm tạo thứ là: • TcpClient(IPEndPoint localEP) • Hàm tạo thứ cho phép ta địa cục hệ thống với số hiệu cổng • Được sử dụng phổ biến máy tính có nhiều card mạng Hàm tạo • Ví dụ – IPEndPoint iep = new IPEndPoint(IPAddress,Parse("192.168.1.6"), 8000); TcpClient newcon = new TcpClient(iep); newcon.Connect("www.isp.net", 8000); Hàm tạo • • • • Hàm tạo thứ là: TcpClient(String host, int port) Hàm tạo sử dụng phổ biến Nó cho phép địa số hiệu cổng máy tính xa • Vì không cần gọi phương thức Connect() Hàm tạo • Ví dụ: – TcpClient newcon = new TcpClient("www.isp.net", 8000); 10 Hàm tạo • Khơng giống hàm tạo lớp TcpClient, hàm tạo lớp TcpListener cần phải số hiệu cổng cụ thể mà server lắng nghe • Nếu máy chủ có nhiều card mạng ta cần địa cụ thể mà server làm việc 16 Các phương thức lớp TcpListener Method Description AcceptSocket() Accepts an incoming connection on the port and assign it to a Socket object AcceptTcpClient() Accepts an incoming connection on the port and assigns it to a TcpClient object Equals() Determines if two TcpListener objects are equal GetHashCode() Gets a hash code suitable for use in hash functions GetType() Gets the type of the current instance Pending() Determines if there are pending connection requests Start() Starts listening for connection attempts Stop() Stops listening for connection attempts (closes the socket) ToString() Creates a string representation of the TcpListener object 17 Các phương thức lớp TcpListener • Để tạo một TcpListener lắng nghe kết nối đến từ Client ta làm sau: TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 9050); server.Start(); TcpClient newclient = server.AcceptTcpClient(); 18 Một ứng dụng Server đơn giản • Ví dụ TcpListenerSample.cs 19 Lớp UdpClient • Lớp UdpClient đặt System.Net.Sockets namespace • Lớp thiết kế nhằm trợ giúp cho lập trình viên việc xây dựng ứng UDP Client UDP Server 20 Hàm tạo • UdpClient có hàm tạo – UdpClient() This format creates a new UdpClient instance not bound to any specific address or port – UdpClient(int port) This constructor binds the new UdpClient object to a specific UDP port number 21 Hàm tạo • Hai hàm tạo lại – UdpClient(IPEndPoint iep) This constructor binds the new UdpClient object to a specific local IP address and port number – UdpClient(string host, int port) This format binds the new UdpClient object to any local IP address and port and associates it with a specific remote IP address and port 22 Các phương thức lớp UdpClient Method Description Close() Closes the underlying socket Connect() Allows you to specify a remote IP endpoint to send and receive data with DropMulticastGroup() Removes the socket from a UDP multicast group Equals() Determines if two UdpClient objects are equal GetHashCode() Gets a hash code for the UdpClient object GetType() Gets the Type of the current object JoinMulticastGroup() Adds the socket to a UDP multicast group Receive() Receives data from the socket Send() Sends data to a remote host from the socket ToString() Creates a string representation of the UdpClient object 23 Sử dụng lớp UdpClient chương trình • Có số khác biệt nhỏ hàm nhận gửi liệu lớp UdpClient so với hàm SendTo() ReceiveFrom() lớp Socket 24 Phương thức Receive() • Lớp UdpClient sử dụng phương thức Receive() để chấp nhận gói tin đến địa số hiệu cổng cụ thể • Nguyên mẫu phương thức sau: – byte[] Receive(ref IPEndPoint iep) 25 Phương thức Receive() • Ví dụ sử dụng phương thức Receive() – IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); – UdpClient newsock = new UdpClient(ipep); – byte[] data = new byte[1024]; – IPEndPoint ipep2 = new IPEndPoint(IPAddress.Any, 0); data = host.Receive(ref ipep2); – Console.WriteLine("The remote host is: {0}, port {1}", ipep2.Address, ipep2.Port); Console.WriteLine(Encoding.ASCII.GetString(data)); 26 Phương thức Send() • Có formats hàm Send() – Send(byte[] data, int sz) This format sends the byte array data of size sz to the default remote host To use this format, you must specify a default remote UDP host using either UdpClient constructor, or the Connect() method: – UdpClient host = new UdpClient("127.0.0.1", 9050); 27 Phương thức Send() • Hai formats khác là: – Send(byte[] data, int sz, IPEndPoint iep) This format sends the byte array data of size sz to the remote host specified by iep – Send(byte[] data, int sz, string host, int port) This format sends the byte array data of size sz to the host host at port port 28 • Ví dụ UdpClient Server 29 • Ví dụ UdpClient Client 30 ...Mục lục chương Giới thiệu Lớp TcpClient Lớp TcpListener Lớp UdpClient Giới thiệu • Ở chương trước học lớp Socket mức thấp để tạo ứng dụng TCP UDP • Tuy nhiên người lập trình mạng thường... nhiên người lập trình mạng thường cảm thấy lớp khó hiểu dễ nhầm lẫn • Chương cung cấp lớp trợ giúp nhằm đơn giản trình lập trình mạng Lớp TcpClient • Lớp TcpClient đặt System.Net.Sockets namespace... UdpClient object 23 Sử dụng lớp UdpClient chương trình • Có số khác biệt nhỏ hàm nhận gửi liệu lớp UdpClient so với hàm SendTo() ReceiveFrom() lớp Socket 24 Phương thức Receive() • Lớp UdpClient

Ngày đăng: 11/05/2021, 04:42

Xem thêm: