240 likes | 452 Views
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
E N D
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 • Addressing based on port number • Checking data error
TCP and UDP (2/3) • Differences between TCP and UDP
UDP server UDP server UDP Client #1 UDP server/client operation (1/2) • UDP server/client operation
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
fgets() sendto() recvfrom() printf() UDP client UDP server printf() recvfrom() sendto() UDP server/client example • Code example
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
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
UDP server UDP client socket() socket() bind() network recvfrom() sendto() sendto() recvfrom() closesocket() closesocket() UDP server/client analysis (3/4) • UDP server/client structure ①
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!!)
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
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);
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
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);
unicasting broadcasting multicasting broadcasting (1/6) • Communication types
sender broadcasting (2/6) • Broadcasting principle
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()");
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);
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
router router broadcasting (6/6) • Broadcast address (cont’d) • BroadcastSender.cpp, BroadcastReceiver.cpp Network-directed broadcast Local broadcast