1 / 21

Chapter 06. UDP Server/Client

Chapter 06. UDP Server/Client. Goal. Understanding the basic structure and principle of UDP server/client Learning socket system calls for UDP application Understanding broadcasting concept and UDP based implementation. TCP and UDP (1/3). Similarity of TCP and UDP

oscar-cantu
Download Presentation

Chapter 06. UDP Server/Client

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 06. UDP Server/Client

  2. Goal • Understanding the basic structure and principle of UDP server/client • Learning socket system calls for UDP application • Understanding broadcasting concept and UDP based implementation

  3. TCP and UDP (1/3) • Similarity of TCP and UDP • Addressing based on port number • Checking data error

  4. TCP and UDP (2/3) • Differences between TCP and UDP

  5. UDP server UDP server UDP Client #1 UDP server/client operation (1/2) • UDP server/client operation

  6. UDP server/client operation (2/2) • UDP server/client operation (cont’d) UDP server UDP server . . . Comm. Comm. UDP client #1 UDP client #2 . . . UDP client #n UDP client #1

  7. fgets() sendto() recvfrom() printf() UDP client UDP server printf() recvfrom() sendto() UDP server/client example • Code example

  8. UDP server/client analysis (1/4) • UDP/IP Socket requires three components ① protocol • Defined by Socket() system call ② local IP address and port number • Server or client side ③ remote IP address and port number

  9. network server client application Local IP addr Local IP addr Local port num Local port num Remote IP addr Remote IP addr OS Remote port num Remote port num Recvfrom buffer Sendto buffer ••• ••• UDP server/client analysis (2/4) • Socket data structure

  10. UDP server UDP client socket() socket() bind() network recvfrom() sendto() sendto() recvfrom() closesocket() closesocket() UDP server/client analysis (3/4) • UDP server/client structure ①

  11. UDP server UDP client socket() socket() bind() connect() network recvfrom() send() sendto() recv() closesocket() closesocket() UDP server/client analysis (4/4) • UDP server/client structure ② (Ignore!!)

  12. data transfer system call (1/4) • sendto() • send application data to peer side int sendto ( SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to, int tolen ) ; success: num. of bytes sent, fail: SOCKET_ERROR

  13. data transfer system call (2/4) • sendto() example // initialize socket address structure to receiver address SOCKADDR_IN serveraddr; ... // declare buffer for sending data char buf[BUFSIZE]; // store sending data to buffer ... // data sending. retval = sendto(sock, buf, strlen(buf), 0, (SOCKADDR *)&serveraddr, sizeof(serveraddr)); if(retval == SOCKET_ERROR) error handling; printf("%d bytes sent...\n", retval);

  14. data transfer system call (3/4) • recvfrom() • Data receving from peer side int recvfrom ( SOCKET s, char* buf, int len, int flags, struct sockaddr* from, int* fromlen ) ; success: num. of bytes sent, fail: SOCKET_ERROR

  15. data transfer system call (4/4) • recvfrom() example // initialize socket address structure to sender address SOCKADDR_IN peeraddr; int addrlen; // declare buffer for received data char buf[BUFSIZE]; // data receiving addrlen = sizeof(peeraddr); retval = recvfrom(sock, buf, BUFSIZE, 0, (SOCKADDR *)&peeraddr, &addrlen); if(retval == SOCKET_ERROR) error handling; printf("%d bytes received...\n", retval);

  16. unicasting broadcasting multicasting broadcasting (1/6) • Communication types

  17. sender broadcasting (2/6) • Broadcasting principle

  18. broadcasting (3/6) • Procedure to send broadcast data ① Enable broadcast option for a socket BOOL bEnable = TRUE; retval = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&bEnable, sizeof(bEnable)); if(retval == SOCKET_ERROR) err_quit("setsockopt()");

  19. broadcasting (4/6) • Procedure to send broadcast data (cont’d) ② Sending data to broadcast address // initialize socket address structure SOCKADDR_IN remoteaddr; ZeroMemory(&remoteaddr, sizeof(remoteaddr)); remoteaddr.sin_family = AF_INET; remoteaddr.sin_port = htons(9000); remoteaddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); // store data to buffer char buf[BUFSIZE]; ... // sending data. retval = sendto(sock, buf, strlen(buf), 0, (SOCKADDR *)&remoteaddr, sizeof(remoteaddr)); if(retval == SOCKET_ERROR) error handling; printf("%d bytes sent….\n", retval);

  20. host ID network ID 11 . . . . . . . . . . . . 1 Network-directed broadcast network ID subnet ID 11 . . . 1 subnet-directed broadcast 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 local broadcast broadcasting (5/6) • Broadcast address

  21. router router broadcasting (6/6) • Broadcast address (cont’d) • BroadcastSender.cpp, BroadcastReceiver.cpp Network-directed broadcast Local broadcast

More Related