lập trình mạng nguyễn cao đạt chương 3 lập trình multicasting sinhvienzone com

17 46 0
lập trình mạng nguyễn cao đạt chương 3 lập trình multicasting sinhvienzone com

Đ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

Si nh Vi en Zo ne C om Chương 3: Lập trình Multicasting SinhVienZone.com https://fb.com/sinhvienzonevn Chương Giới thiệu multicast Multicast : phát tán thông tin tới nhóm địa cách đồng thời om - C Root multicast non –root multicast Root multicast: thành viên đóng vai trò root phát tán thơng tin tới leaf, root truyền thông tin đồng thời tới lá, truyền thông tin root mà truyền thông tin sang khác o Non root multicast: tất thành viên (leaf) , truyền thông tin tới tất khác Si nh Vi en Zo ne o SinhVienZone.com https://fb.com/sinhvienzonevn Chương Giới thiệu multicast Unicast: giao tiếp có bên gửi bên nhận - Broadcast: Giao tiếp gửi từ điểm tới tất điểm khác, broadcast có điểm gửi Si nh Vi en Zo ne C om - SinhVienZone.com https://fb.com/sinhvienzonevn Chương Địa IP dùng cho Multicast nh Vi en Zo ne C om Sử dụng địa lớp D từ 224.0.0.0 tới 239.255.255.255 Một số địa IP đặc biệt dùng cho multicast Si - SinhVienZone.com https://fb.com/sinhvienzonevn Chương Địa IP dùng cho Multicast ne nh Vi en Zo Group Member C om Thành viên phải tham gia vào nhóm nhận dữ liệu multicast Chương trình gửi gửi cho nhóm thì thành viên nhóm nhận đc dữ liệu Receiver Receiver Si - Group Member Receiver Group Member Sender Group Member Sender & Receiver SinhVienZone.com https://fb.com/sinhvienzonevn Chương Địa IP dùng cho Multicast Si nh Vi en Zo ne C om Giao thức IGMP (Internet Group Management Protocol ) : Là giao thức mà máy nhận trao đổi thơng tin với bợ tìm đường multicast cục bợ (Local Multicast Router ) để trở thành thành viên nhóm multicast Cung cấp phương thức qua thành viên có thể tham gia rời bỏ nhóm multicast Để tham gia nhóm thành viên gửi thơng báo u cầu tham gia nhóm Các bợ tìm đường multicast gửi IGMP query tới tất thành viên để xem thành viên nhóm hay khơng SinhVienZone.com https://fb.com/sinhvienzonevn Chương Địa IP dùng cho Multicast om Giao thức IGMP (Internet Group Management Protocol ) : Zo ne C Joining a Group R2 R3 Report Si nh Vi en R1 224.1.2.3 SinhVienZone.com https://fb.com/sinhvienzonevn Chương Địa IP dùng cho Multicast om Giao thức IGMP (Internet Group Management Protocol ) : Zo X R1 224.1.2.3 nh Vi en 224.1.2.3 Report Si Suppressed ne C Maintaining a Group R2 224.1.2.3 X R3 Suppressed Query Bộ tìm đường định kỳ gửi query tới 224.0.0.1 (tất thành viên mạng này) Các thành viên có thể trả lời report khơng (còn tham gia không) SinhVienZone.com https://fb.com/sinhvienzonevn Chương 3 Truyền/ nhận multicast C om Gửi multicast Hiệu chỉnh lại địa hàm sendto cho phù hợp, địa nhóm cần multicast Zo ne Nhận multicast Trước multicast, cần tham gia vào nhóm multicast cách gọi hàm setsockopt nh Vi en struct ip_mreq mreq; setsockopt(sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)); Si ip_mreq có cấu trúc sau: struct ip_mreq { struct in_addr imr_multiaddr; /* multicast group to join */ struct in_addr imr_interface; /* interface to join on */ } SinhVienZone.com https://fb.com/sinhvienzonevn Chương 3 Truyền/ nhận multicast om Ví dụ tham gia vào nhóm multicast C SOCKET s; ne SOCKADDR_IN localif; struct ip_mreq mreq; localif.sin_family = AF_INET; nh Vi en localif.sin_port = htons(5150); Zo s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); localif.sin_addr.s_addr = htonl(INADDR_ANY); bind(s, (SOCKADDR *)&localif, sizeof(localif)); mreq.imr_interface.s_addr = inet_addr("157.124.22.104"); Si mreq.imr_multiaddr.s_addr = inet_addr("234.5.6.7"); setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); SinhVienZone.com https://fb.com/sinhvienzonevn Chương 3 Truyền/ nhận multicast om Nhận liệu Si nh Vi en Zo ne C nbytes=recvfrom(s,msgbuf,MSGBUFSIZE,0, (struct sockaddr *) &localif, sizeof(localif))) ; SinhVienZone.com https://fb.com/sinhvienzonevn Chương Thiết lập trình gửi tin cậy qua bước sau: om Gửi tin cậy Tạo socket multicast tin cậy Bind socket to INADDR_ANY Thiết lập địa giao tiếp RM_SET_SEND_IF Kết nối socket tới địa nhóm multicast Si nh Vi en Zo ne C SinhVienZone.com https://fb.com/sinhvienzonevn Chương Gửi tin cậy om Ví dụ q trình gửi tin cậy: C SOCKET s; ULONG sendif; ne SOCKADDR_IN localif, Zo multi; char buf[1024]; int buflen=1024; nh Vi en s = socket(AF_INET, SOCK_RDM, IPPROTO_RM); // SOCK_RDM giống SOCK_DGRAM // Bind to INADDR_ANY localif.sin_family = AF_INET; localif.sin_port = htons(0); Si localif.sin_addr.s_addr = htonl(INADDR_ANY); bind(s, (SOCKADDR *)&localif, sizeof(localif)); SinhVienZone.com https://fb.com/sinhvienzonevn Chương Gửi tin cậy om // Set the outgoing interface C sendif = inet_addr("157.124.22.104"); setsockopt(s, IPPROTO_RM, RM_SET_SEND_IF, (char *)&sendif, sizeof(sendif)); ne // Connect the socket to the multicast destination multi.sin_port = htons(5150); nh Vi en multi.sin_addr.s_addr = Zo multi.sin_family = AF_INET; inet_addr("234.5.6.7"); connect(s, (SOCKADDR *)&multi, sizeof(multi)); // Send the data closesocket(s); Si send(s, buf, buflen, 0); // Close up the session SinhVienZone.com https://fb.com/sinhvienzonevn Chương Nhận tin cậy om Thiết lập trình nhận tin cậy qua bước sau: Tạo socket multicast tin cậy Bind socket địa nhóm multicast Nếu chương trình nhận cần lắng nghe giao tiếp cụ thể gọi ne C Zo setsockopt RM_ADD_RECEIVE_IF cho interface Gọi listen Chờ đợi accept Si nh Vi en SinhVienZone.com https://fb.com/sinhvienzonevn Chương Nhận tin cậy om Ví dụ nhận tin cậy: C SOCKET s, ns; SOCKADDR_IN multi, safrom; char buf[1024]; int buflen=1024, fromlen, rc; Zo ne ULONG localif; nh Vi en s = socket(AF_INET, SOCK_RDM, IPPROTO_RM); multi.sin_family = AF_INET; multi.sin_port = htons(5150); multi.sin_addr.s_addr = inet_addr("234.5.6.7"); listen(s, 10); Si bind(s, (SOCKADDR *)&multi, sizeof(multi)); SinhVienZone.com https://fb.com/sinhvienzonevn Chương Nhận tin cậy om Ví dụ nhận tin cậy: localif = inet_addr("157.124.22.104"); C setsockopt(s, IPPROTO_RM, RM_ADD_RECEIVE_IF, (char *)&localif, sizeof(localif)); fromlen = sizeof(safrom); closesocket(s); // Don't need to listen anymore nh Vi en // start receiving data Zo ne ns = accept(s, (SOCKADDR *)&safrom, &fromlen); while (1) { rc = recv(ns, buf, buflen, 0); if (rc == SOCKET_ERROR) Si { if (WSAGetLastError() == WSAEDISCON) break; else { // An unexpected error } } } closesocket(ns); SinhVienZone.com https://fb.com/sinhvienzonevn ... 224.1.2 .3 SinhVienZone. com https://fb .com/ sinhvienzonevn Chương Địa IP dùng cho Multicast om Giao thức IGMP (Internet Group Management Protocol ) : Zo X R1 224.1.2 .3 nh Vi en 224.1.2 .3 Report... sizeof(localif))) ; SinhVienZone. com https://fb .com/ sinhvienzonevn Chương Thiết lập trình gửi tin cậy qua bước sau: om Gửi tin cậy Tạo socket multicast tin cậy Bind socket to INADDR_ANY Thiết lập địa giao... mreq.imr_multiaddr.s_addr = inet_addr(" 234 .5.6.7"); setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); SinhVienZone. com https://fb .com/ sinhvienzonevn Chương 3 Truyền/ nhận multicast

Ngày đăng: 30/01/2020, 22:43

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

  • Đang cập nhật ...

Tài liệu liên quan