380 likes | 481 Views
CS 547/490 Network Programming. IP Multicast - Program Implementation. Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu. IP Multicast/000. Unicast Server and Clients. A client initiates a session
E N D
CS 547/490 Network Programming IP Multicast - Program Implementation Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu IP Multicast/000
Unicast Server and Clients A client initiates a session to get a reply from a server Message Request Client Time Server “Pull” Model CS 547 Advanced Network Programming IP Multicast 2/002
Multicast Server and Clients Message Client 1 Client 2 Client 3 Message Message Time Server Multicast sender “sends” message to clients “Push” Model CS 547 Advanced Network Programming IP Multicast 2/003
Reuse Socket Initialize socket Initialize socket Socket() Socket() Set Local Address/Port Bind() Bind() SERVER UDP Connection-less transmission closesocket() closesocket() recvfrom() sendto() Join a multicast group Join a multicast group Disable Loop-back Set Multicast Address Set TTL value CS 547 Advanced Network Programming Procedures in Client and Server Process CLIENT IP Multicast/004
Make sure Winsock 2.X WinSock Version Number (Ver. 2.2) status = WSAStartup(0X0202, &stWSAData); Initialize socket Execution status WinSock data strcuture CS 547 Advanced Network Programming WSADATA stWSAData; // IP Multicast 2/004
AF_INET = IP AF_INET for IP GRAM = UDP Socket_ID = socket(AF_INET, SOCK_DGRAM, 0); Get a datagram socket SOCK_DGRAAM for UDP session Socket ID to identify this socket session CS 547 Advanced Network Programming IP Multicast 2/005
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; CS 547 Advanced Network Programming IP Multicast 2/006
Bind the socket Byte size of the stLclAddr structure Pointer to stLclAddr structure Socket ID to identify this socket session CS 547 Advanced Network Programming status = bind (Socket_ID, (struct sockaddr *) &stLclAddr, sizeof (stLclAddr); IP Multicast 2/007
Join the multicast group Multicast Address (Class-D IP Address) Join a multicast group Any NIC if multiple NICs exist CS 547 Advanced Network Programming 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 IP address (Class-D) is a multicast group ID IP Multicast 2/008
Set TTL Specify the reach of the multicast messages u_long lTTL; lTTL = (u_long)3; // Declare variable TTL // Assign TTL value status = setsockopt (Socket_ID, IPPROTO_IP, IP_MULTICAST_TTL, (char *) &lTTL, sizeof (lTTL)); strcpy (lTTL, “2”); CS 547 Advanced Network Programming IP Multicast 2/009
Disable Loop-back BOOL fFlag; fFlag = FALSE; // Declare variable loop-back Flag // Reset the loop-back flag status = setsockopt (Socket_ID, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &fFlag, sizeof (fFlag)); CS 547 Advanced Network Programming Loop-back = FALSE -> The Multicast sender will not receiver its message IP Multicast 2/010
Specify Multicast Address CS 547 Advanced Network Programming stDstAddr.sin_family = AF_INET; stDstAddr.sin_addr.s_addr = inet_addr (achMCAddr); stDstAddr.sin_port = htons (nPort); stDstAddr IP Multicast 2/011
Transmit a multicast message status = sendto (Socket_ID, (char *) &stSysTime, sizeof (stSysTime), 0, (struct sockaddr *)&sdDstAddr, sizeof (stDstAddr)); Close socket connection CS 547 Advanced Network Programming status = closesocket (Socket_ID); IP Multicast 2/012
CS 547 Advanced Network Programming Procedures in the client side IP Multicast 2/013
Initialize socket Get a datagram socket CS 547 Advanced Network Programming Same as the server process Same as the server process IP Multicast 2/014
Leave a group Reuse Socket Initialize socket Initialize socket Possible Bind problem Socket() Socket() Set Local Address/Port Problem (Bind Problem) Bind() Bind() SERVER closesocket() recvfrom() sendto() Join a multicast group Join a multicast group Disable Loop-back Set Multicast Address closesocket() Set TTL value CS 547 Advanced Network Programming CLIENT IP Multicast/015
Step 3: Reuse Socket closesocket () only delete this Network Application Operating System Device Driver NW Interface Network Protocol (IP) LLC/MAC Protocol Transport Protocol (TCP/UDP) CS 547 Advanced Network Programming Network IP Multicast 2/016
Three techniques to solve this problem CS 547 Advanced Network Programming (1) Hard disconnection – zero lingering (2) Use different port # for each bind (3) Socket reuse IP Multicast 2/017
closesocket () only delete this Hard Disconnection Network Application Always destroy the connection Network Protocol (IP) LLC/MAC Protocol Transport Protocol (TCP/UDP) CS 547 Advanced Network Programming Network IP Multicast 2/018
Hard Disconnection CS 547 Advanced Network Programming Disadvantage: Part of the last message could be lost IP Multicast 2/019
Leave a group Use different # for each iteration Initialize socket Initialize socket Use different Port number Socket() Socket() Set Local Address/Port Bind() Bind() SERVER closesocket() recvfrom() sendto() Join a multicast group Join a multicast group Disable Loop-back Set Multicast Address closesocket() Set TTL value CS 547 Advanced Network Programming CLIENT IP Multicast/020
Use different # for each iteration CS 547 Advanced Network Programming Disadvantage: One process (program) might consumes many port numbers IP Multicast 2/021
Leave a group Socket Reuse Reuse Socket Initialize socket Initialize socket Possible Bind problem Socket() Socket() Set Local Address/Port Bind() Bind() SERVER closesocket() recvfrom() sendto() Join a multicast group Join a multicast group Disable Loop-back Set Multicast Address closesocket() Set TTL value CS 547 Advanced Network Programming CLIENT IP Multicast/022
Step 3: Socket Reuse Socket ID SOL_SOCKET constant To reuse a socket Pointer to the Boolean variable CS 547 Advanced Network Programming int status = setsockopt (socket_id, SOL_SOCKET, BOOL fFlag; fFlag =FALSE; SO_REUSEADDR, (char *)&fFlag, sizeof (fFlag)); IP Multicast 2/023
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; CS 547 Advanced Network Programming SOCKADDR_IN stLclAddr; IP Multicast 2/024
Bind the socket Byte size of the stLclAddr structure Pointer to stLclAddr structure Socket ID to identify this socket session CS 547 Advanced Network Programming status = bind (socket_ID, (struct sockaddr *) &stLclAddr, sizeof (stLclAddr); IP Multicast 2/025
Join the multicast group CS 547 Advanced Network Programming 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)); IP Multicast 2/026
Join the multicast group Assign multicast address strcpy (achMCAddress, “234.5.6.7”); CS 547 Advanced Network Programming structip_mreq stMreq; // Create Multicast interface Structure char achMCAddress[20]; // IP Multicast Address Holder IP Multicast 2/027
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); CS 547 Advanced Network Programming int addr_size = sizeof (stSrcAddr); IP Multicast 2/028
Leaving the multicast group Socket ID IPPROTO_IP constant To drop a multicast group Multicast Interface Structure Size of the structure CS 547 Advanced Network Programming int status = setsockopt (socket_id, IPPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)stMreq, sizeof (stMreq)); IP Multicast 2/029
Leaving the multicast group Specify from which multicast group you are leaving Specify which interface should be removed from the group CS 547 Advanced Network Programming stMreq.imr_multiaddr.s_addr = (achMCAddr); stMreq.imr_interface.s_addr = INADDR_ANY; setsockopt(my_socket, IPPROTO, IP_DROP_MEMBERSHIP, (char *)stMreq, sizeof(stMreq)); IP Multicast 2/030
Close socket connection Clean up winsock object WASCleanup(); status = closesocket (socket_ID); CS 547 Advanced Network Programming IP Multicast 2/031
How to get the current system time (1) ? System Call Declaration CS 547 Advanced Network Programming Server Side SYSTEMTIME stSystemTime; // Hold the current time GetSystemTime (&stSystemTime); // Get the current system time On transmission, you should typecast it to char pointer: (char *) &stSystemTime IP Multicast 2/020
How to get the current system time (2) ? Declaration CS 547 Advanced Network Programming Client Side (1) SYSTEMTIME stSystemTime; // Hold the current time SYSTEMTIME * lpstSysTime; // Pointer of type SYSTIME IP Multicast 2/021
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] CS 547 Advanced Network Programming IP Multicast 2/022
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); CS 547 Advanced Network Programming ! Blue Bold - Member variables in stSysTime structure. IP Multicast 2/023
Initialize socket Initialize socket Socket() Socket() ??? Set Local Address/Port Bind() Bind() SERVER closesocket() closesocket() recvfrom() sendto() Join a multicast group Join a multicast group Disable Loop-back Set Multicast Address Set TTL value CS 547 Advanced Network Programming Client and Server Program Structure CLIENT IP Multicast/024