Upload
oscar-cantu
View
26
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Chapter 06.
UDP Server/Client
IT COOKBOOKIT COOKBOOK
- 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
IT COOKBOOKIT COOKBOOK
- 3 -
TCP and UDP (1/3)
• Similarity of TCP and UDP– Addressing based on port number– Checking data error
IT COOKBOOKIT COOKBOOK
- 4 -
TCP and UDP (2/3)
• Differences between TCP and UDPitem
TCP UDP
①connection-oriented protocol- Data comm. after connection setup
Connectionless protocol- Data comm. without connection
② - byte-stream service - Datagram service
③Reliable data transfer- By data retransmission
Unreliable data transfer- No data retransmission
④ unicastUnicast,Broadcast,Multicast
IT COOKBOOKIT COOKBOOK
- 5 -
UDP server/client operation (1/2)
• UDP server/client operation
UDP server UDP server
UDP Client #1
IT COOKBOOKIT COOKBOOK
- 6 -
UDP server/client operation (2/2)
• UDP server/client operation (cont’d)
UDP server
UDP client #1
UDPclient #2
Comm. Comm.
UDP server
UDP client #1
UDP client #n
. . .
. . .
IT COOKBOOKIT COOKBOOK
- 7 -
UDP server/client example
• Code example
UDP client UDP server
fgets() sendto()
printf()
recvfrom() printf()
sendto()recvfrom()
IT COOKBOOKIT COOKBOOK
- 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
IT COOKBOOKIT COOKBOOK
- 9 -
UDP server/client analysis (2/4)
• Socket data structure
server
Local IP addrLocal port num
Remote IP addrRemote port num
client
Local IP addrLocal port num
Remote IP addrRemote port num
application
OS
network
• • • • • •
Recvfrom bufferSendto buffer
IT COOKBOOKIT COOKBOOK
- 10 -
UDP server/client analysis (3/4)
• UDP server/client structure ①
socket()
bind()
recvfrom()
sendto()
closesocket()
socket()
sendto()
recvfrom()
closesocket()
UDP server UDP client
network
IT COOKBOOKIT COOKBOOK
- 11 -
UDP server/client analysis (4/4)
• UDP server/client structure ② (Ignore!!)
socket()
bind()
recvfrom()
sendto()
closesocket()
socket()
send()
recv()
closesocket()
UDP server UDP client
network
connect()
IT COOKBOOKIT COOKBOOK
- 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
IT COOKBOOKIT COOKBOOK
- 13 -
data transfer system call (2/4)
• sendto() example
// initialize socket address structure to receiver addressSOCKADDR_IN serveraddr;...
// declare buffer for sending datachar 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);
IT COOKBOOKIT COOKBOOK
- 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
IT COOKBOOKIT COOKBOOK
- 15 -
data transfer system call (4/4)
• recvfrom() example
// initialize socket address structure to sender addressSOCKADDR_IN peeraddr;int addrlen;
// declare buffer for received datachar buf[BUFSIZE];
// data receivingaddrlen = sizeof(peeraddr);retval = recvfrom(sock, buf, BUFSIZE, 0, (SOCKADDR *)&peeraddr, &addrlen);if(retval == SOCKET_ERROR) error handling;printf("%d bytes received...\n", retval);
IT COOKBOOKIT COOKBOOK
- 16 -
broadcasting (1/6)
• Communication types
unicasting broadcasting multicasting
IT COOKBOOKIT COOKBOOK
- 17 -
broadcasting (2/6)
• Broadcasting principle
sender
IT COOKBOOKIT COOKBOOK
- 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()");
IT COOKBOOKIT COOKBOOK
- 19 -
broadcasting (4/6)
• Procedure to send broadcast data (cont’d)② Sending data to broadcast address// initialize socket address structureSOCKADDR_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 bufferchar 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);
IT COOKBOOKIT COOKBOOK
- 20 -
broadcasting (5/6)
• Broadcast address
host ID
11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
network ID 11 . . . . . . . . . . . . 1 Network-directed broadcast
local broadcast
network ID 11 . . . 1 subnet-directed broadcastsubnet ID
IT COOKBOOKIT COOKBOOK
- 21 -
broadcasting (6/6)
• Broadcast address (cont’d)
• BroadcastSender.cpp, BroadcastReceiver.cpp
router
router
Network-directed broadcast
Local broadcast