Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki CS 547/490 Network.

Similar presentations


Presentation on theme: "Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki CS 547/490 Network."— Presentation transcript:

1 Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network Programming IP Multicast/000 IP Multicast - Program Implementation

2 CS 547 Advanced Network Programming Unicast Server and Clients IP Multicast 2/002 Client Time Server A client initiates a session to get a reply from a server “Pull” Model Message Request

3 CS 547 Advanced Network Programming Multicast Server and Clients IP Multicast 2/003 Client 1 Client 2 Client 3 Time Server Multicast sender “sends” message to clients “Push” Model Message

4 CS 547 Advanced Network Programming Procedures in Client and Server Process IP Multicast/004 Initialize socket Socket() Set Local Address/Port Bind() Join a multicast group Set TTL value Disable Loop-back Set Multicast Address sendto() closesocket() SERVER CLIENT Initialize socket Socket() Reuse Socket Bind() Join a multicast group recvfrom() closesocket() UDP Connection-less transmission

5 IP Multicast 2/004 CS 547 Advanced Network Programming Initialize socket WSADATA stWSAData; // status = WSAStartup(0X0202, &stWSAData); WinSock Version Number (Ver. 2.2) WinSock data strcuture Execution status Make sure Winsock 2.X

6 Socket_ID = socket(AF_INET, SOCK_DGRAM, 0); AF_INET for IP SOCK_DGRAAM for UDP session Socket ID to identify this socket session IP Multicast 2/005 CS 547 Advanced Network Programming Get a datagram socket AF_INET = IP GRAM = UDP

7 IP Multicast 2/006 CS 547 Advanced Network Programming Set the local IP address and port number stLclAddr.sin_family= AF_INET; stLclAddr.sin_addr.s_addr= htonl (INADDR_ANY); stLclAddr.sin_port= 0; SOCKADDR_IN stLclAddr;

8 IP Multicast 2/007 CS 547 Advanced Network Programming Bind the socket status = bind (Socket_ID, (struct sockaddr *) &stLclAddr, sizeof (stLclAddr); Pointer to stLclAddr structure Byte size of the stLclAddr structure Socket ID to identify this socket session

9 IP Multicast 2/008 CS 547 Advanced Network Programming Join the multicast group stMreq.imr_multiaddr.s_addr = inet_addr (archMCAddr) stMreq.imr_interface.s_addr = INADDR_ANY; status = setsockopt (Socket_ID, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &stMreq, sizeof (stMreq)); Multicast Address (Class-D IP Address) Any NIC if multiple NICs exist Join a multicast group Multicast IP address (Class-D) is a multicast group ID

10 IP Multicast 2/009 CS 547 Advanced Network Programming Set TTL status = setsockopt (Socket_ID, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &lTTL, sizeof (lTTL)); strcpy (lTTL, “2”); u_long lTTL; // Declare variable TTL lTTL = (u_long)3; // Assign TTL value Specify the reach of the multicast messages

11 IP Multicast 2/010 CS 547 Advanced Network Programming Disable Loop-back status = setsockopt (Socket_ID, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &fFlag, sizeof (fFlag)); BOOL fFlag; // Declare variable loop-back Flag fFlag = FALSE; // Reset the loop-back flag Loop-back = FALSE -> The Multicast sender will not receiver its message

12 IP Multicast 2/011 CS 547 Advanced Network Programming Specify Multicast Address stDstAddr.sin_family = AF_INET; stDstAddr.sin_addr.s_addr = inet_addr (achMCAddr); stDstAddr.sin_port = htons (nPort); stDstAddr

13 IP Multicast 2/012 CS 547 Advanced Network Programming Transmit a multicast message status = sendto (Socket_ID, (char *) &stSysTime, sizeof (stSysTime), 0, (struct sockaddr *)&sdDstAddr, sizeof (stDstAddr)); Close socket connection status = closesocket (Socket_ID);

14 IP Multicast 2/013 CS 547 Advanced Network Programming Procedures in the client side

15 Initialize socket IP Multicast 2/014 Same as the server process Get a datagram socket Same as the server process CS 547 Advanced Network Programming

16 IP Multicast/015 Initialize socket Socket() Set Local Address/Port Bind() Join a multicast group Set TTL value Disable Loop-back Set Multicast Address sendto() closesocket() SERVER CLIENT Initialize socket Socket() Reuse Socket Bind() Join a multicast group recvfrom() closesocket() Leave a group Problem (Bind Problem) Possible Bind problem

17 Step 3: Reuse Socket IP Multicast 2/016 CS 547 Advanced Network Programming Network Application Transport Protocol (TCP/UDP) Network Protocol (IP)LLC/MAC Protocol Network NW Interface Operating System Device Driver closesocket () only delete this

18 IP Multicast 2/017 CS 547 Advanced Network Programming Three techniques to solve this problem (1) Hard disconnection – zero lingering (2) Use different port # for each bind (3) Socket reuse

19 IP Multicast 2/018 CS 547 Advanced Network Programming Hard Disconnection Network Application Transport Protocol (TCP/UDP) Network Protocol (IP)LLC/MAC Protocol Network closesocket () only delete this Always destroy the connection

20 IP Multicast 2/019 CS 547 Advanced Network Programming Hard Disconnection Disadvantage: Part of the last message could be lost

21 CS 547 Advanced Network Programming IP Multicast/020 Initialize socket Socket() Set Local Address/Port Bind() Join a multicast group Set TTL value Disable Loop-back Set Multicast Address sendto() closesocket() SERVER CLIENT Initialize socket Socket() Bind() Join a multicast group recvfrom() closesocket() Leave a group Use different # for each iteration Use different Port number

22 IP Multicast 2/021 CS 547 Advanced Network Programming Disadvantage: Use different # for each iteration One process (program) might consumes many port numbers

23 CS 547 Advanced Network Programming IP Multicast/022 Initialize socket Socket() Set Local Address/Port Bind() Join a multicast group Set TTL value Disable Loop-back Set Multicast Address sendto() closesocket() SERVER CLIENT Initialize socket Socket() Reuse Socket Bind() Join a multicast group recvfrom() closesocket() Leave a group Possible Bind problem Socket Reuse

24 Step 3: Socket Reuse IP Multicast 2/023 CS 547 Advanced Network Programming int status = setsockopt (socket_id, SOL_SOCKET, SO_REUSEADDR, (char *)&fFlag, sizeof (fFlag)); BOOL fFlag; fFlag =FALSE; Socket ID SOL_SOCKET constant To reuse a socket Pointer to the Boolean variable

25 IP Multicast 2/024 CS 547 Advanced Network Programming Set the local IP address and port number stLclAddr.sin_family= AF_INET; stLclAddr.sin_addr.s_addr= htonl (INADDR_ANY); stLclAddr.sin_port= 0; SOCKADDR_IN stLclAddr;

26 IP Multicast 2/025 CS 547 Advanced Network Programming Bind the socket status = bind (socket_ID, (struct sockaddr *) &stLclAddr, sizeof (stLclAddr); Pointer to stLclAddr structure Byte size of the stLclAddr structure Socket ID to identify this socket session

27 IP Multicast 2/026 CS 547 Advanced Network Programming Join the multicast group stMreq.imr_multiaddr.s_addr = (achMCAddr); stMreq.imr_interface.s_addr = INADDR_ANY; status = setsockopt (socket_id, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &stMreq, sizeof (stMreq));

28 IP Multicast 2/027 CS 547 Advanced Network Programming Join the multicast group struct ip_mreq stMreq; // Create Multicast interface Structure char achMCAddress[20]; // IP Multicast Address Holder strcpy (achMCAddress, “234.5.6.7”); Assign multicast address

29 IP Multicast 2/028 CS 547 Advanced Network Programming Receive a multicast message status = recvfrom (socket_ID, (char *) achInBuf, BUFF_SIZE, 0, (struct sockaddr *)&stSrcAddr, &addr_size); Close socket connection status = closesocket (socket_ID); int addr_size = sizeof (stSrcAddr);

30 IP Multicast 2/029 CS 547 Advanced Network Programming Leaving the multicast group int status = setsockopt (socket_id, IPPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)stMreq, sizeof (stMreq)); Socket ID IPPROTO_IP constant To drop a multicast group Multicast Interface Structure Size of the structure

31 IP Multicast 2/030 CS 547 Advanced Network Programming stMreq.imr_multiaddr.s_addr = (achMCAddr); stMreq.imr_interface.s_addr = INADDR_ANY; Leaving the multicast group Specify from which multicast group you are leaving Specify which interface should be removed from the group setsockopt(my_socket, IPPROTO, IP_DROP_MEMBERSHIP, (char *)stMreq, sizeof(stMreq));

32 IP Multicast 2/031 CS 547 Advanced Network Programming Clean up winsock object Close socket connection status = closesocket (socket_ID);WASCleanup();

33 IP Multicast 2/020 CS 547 Advanced Network Programming How to get the current system time (1) ? Server Side Declaration SYSTEMTIME stSystemTime; // Hold the current time System Call GetSystemTime (&stSystemTime); // Get the current system time On transmission, you should typecast it to char pointer: (char *) &stSystemTime

34 IP Multicast 2/021 CS 547 Advanced Network Programming How to get the current system time (2) ? Client Side (1) Declaration SYSTEMTIME stSystemTime; // Hold the current time SYSTEMTIME * lpstSysTime; // Pointer of type SYSTIME

35 IP Multicast 2/022 CS 547 Advanced Network Programming How to get the current system time (3) ? SYSTEMTIME stSystemTime; // Hold the current time SYSTEMTIME * lpstSysTime; // Pointer of type SYSTIME char achInBuff [BUF_SIZE] // Char string for receiving a message lpstSysTime = (SYSTEMTIME *)achInBuf; recvfrom (Socket_ID, achInBuf, BUF_SIZE, 0, (struct sockaddr *)&stSrcAddr, &addr_size); SetSystemTime (lpstSysTime); GetSystemTime (stSystemTime); [Display the updated current system time]

36 IP Multicast 2/023 CS 547 Advanced Network Programming How to get the current system time (4) ? printf (“The local time update to %02d-%02d-%02d at %02d-%02d-%02d\n”, stSysTime.wMonth, stSysTime.wDay, stSysTime.wYear, stsSyTime.wHour, stSysTime.wMinute, stSysTime.wSecond, stSysTime.wMillisecond); ! Blue Bold - Member variables in stSysTime structure.

37 CS 547 Advanced Network Programming Client and Server Program Structure IP Multicast/024 Initialize socket Socket() Set Local Address/Port Bind() Join a multicast group Set TTL value Disable Loop-back Set Multicast Address sendto() closesocket() SERVER CLIENT Initialize socket Socket() ??? Bind() Join a multicast group recvfrom() closesocket()


Download ppt "Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki CS 547/490 Network."

Similar presentations


Ads by Google