User Datagram Protocol (UDP) in Unix and Network Programming

 
CSCI 330
UNIX and Network Programming
 
 
Unit XIV: User Datagram Protocol
 
Unit Overview
 
Transport layer
User datagram protocol
UDP programming
Example UDP client/server programs
 
2
 
CSCI 330 - UNIX and Newtork Programming
 
Review: Network Layer
 
also called: Internet Protocol (IP) Layer
provides host to host transmission service,
   where hosts are not necessarily adjacent
IP layer provides services:
addressing
hosts have global addresses: IPv4, IPv6
routing and forwarding
find path from host to host
 
3
 
CSCI 330 - UNIX and Newtork Programming
 
Transport Layer
 
provides end-to-end communication services for applications
provides multiple endpoints on a single node: 
port
 
TCP: transmission control protocol
connection oriented, guaranteed delivery
stream oriented:
  
basis for:
 http, ftp, smtp, ssh
UDP: user datagram protocol
best effort
datagram oriented:
  
basis for:
 dns, rtp
 
4
 
CSCI 330 - UNIX and Newtork Programming
 
UDP
 
simple message-based connection-less protocol
transmits information in one direction from source to destination
without verifying the readiness or state of the receiver
 
uses  
datagram
  as message
 
stateless and fast
 
5
 
CSCI 330 - UNIX and Newtork Programming
 
UPD packet format
 
6
 
CSCI 330 - UNIX and Newtork Programming
 
UDP programming
 
common abstraction:  
socket
first introduced in BSD Unix in 1981
 
socket is end-point of communication link
identified as IP address + port number
 
can receive data
can send data
 
7
 
CSCI 330 - UNIX and Newtork Programming
Socket system calls
8
 
server
 
client
 
optional
CSCI 330 - UNIX and Newtork Programming
 
UDP communications pattern
 
9
optional
 
CSCI 330 - UNIX and Newtork Programming
 
System call: socket
 
10
10
 
CSCI 330 - UNIX and Newtork Programming
 
System call: socket
 
int socket(int domain, int type, int protocol)
 
creates a new socket, as end point to a communications link
domain
  is set to  
AF_INET
type
  is set to  
SOCK_DGRAM
  for datagrams
protocol
  is set to 0, i.e. default UDP
returns socket descriptor:
used in bind, sendto, recvfrom, close
 
11
11
 
CSCI 330 - UNIX and Newtork Programming
 
System call: bind
 
12
12
 
CSCI 330 - UNIX and Newtork Programming
 
System call: bind
 
int bind(int sockfd,
             const struct sockaddr *addr,
             socklen_t addrlen)
 
assigns address to socket: IP number and port
struct sockaddr
 
holds address information
will accept  
struct sockaddr_in
 
pointer
addrlen
 specifies length of 
addr
 structure
returns 0 on success, -1 otherwise
 
13
13
 
CSCI 330 - UNIX and Newtork Programming
 
structure sockaddr: 16bytes
 
struct sockaddr {
   short 
 
sa_family;
 
/* address family 
 
*/
   char 
 
sa_data[14];
 
/* address data   
 
*/
};
 
struct sockaddr_in {
  short           sin_family; /* address family
 
 */
  unsigned short  sin_port;   /* port number:2 bytes 
 
 */
  struct in_addr  sin_addr;   /* IP address: 4 bytes 
 
 */
                    /* pad to size of struct sockaddr */
  char            sin_zero[8];
};
 
CSCI 330 - UNIX and Newtork Programming
 
14
14
 
structure sockaddr_in: members
 
sin_family 
 
   /* address family       */
 
always: AF_INET
sin_port 
 
        /* port number: 2 bytes */
 
htons(4444)  /* ensure network order */
sin_addr 
 
  /* Internet address: 4 bytes  */
   
 
s_addr = INADDR_ANY
 
s_addr = inet_addr("127.0.0.1")
 
CSCI 330 - UNIX and Newtork Programming
 
15
15
 
Inet Address Manipulation
 
CSCI 330 - UNIX and Newtork Programming
 
16
16
 
System call: recvfrom
 
17
17
 
CSCI 330 - UNIX and Newtork Programming
 
System call: recvfrom
 
ssize_t recvfrom(int sockfd, void *buf, size_t len,
 
 int flags, struct sockaddr *src_addr,
   
      socklen_t *addrlen
)
 
receives a datagram  
buf
  of size  
len
  from socket  
sockfd
will wait until a datagram is available
flags
  specifies wait behavior, e.g.: 0 for default
src_addr
  will hold address information of sender
struct sockaddr
 
defines address structure
addrlen
  specifies length of  
src_addr
  structure
returns the number of bytes received, i.e. size of datagram
 
18
18
 
CSCI 330 - UNIX and Newtork Programming
 
System call: sendto
 
19
19
 
CSCI 330 - UNIX and Newtork Programming
 
System call: sendto
 
ssize_t sendto(int sockfd,
  const void *buf, size_t len, int flags,
  const struct sockaddr *dest_addr, socklen_t addrlen)
 
sends datagram 
buf
 of size 
len
 to socket  
sockfd
will wait if there is no ready receiver
flags specifies wait behavior, e.g.: 0 for default
dest_addr
  holds address information of receiver
struct sockaddr
 
defines address structure
addrlen
  specifies length of  
dest_addr
  structure
returns the number of bytes sent, i.e. size of datagram
 
20
20
 
CSCI 330 - UNIX and Newtork Programming
 
Example: UDP Programming
 
simple server: echo
sends all received datagrams back to sender
 
simple client
send datagram to server
 
21
21
 
CSCI 330 - UNIX and Newtork Programming
 
Illustration: echoServer.cxx
 
22
22
 
CSCI 330 - UNIX and Newtork Programming
 
Illustration: echoClient.cxx
 
23
23
 
CSCI 330 - UNIX and Newtork Programming
 
Detail: create UDP socket
 
int sock;
// Create the UDP socket
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
 
perror("Failed to create socket");
 
exit(EXIT_FAILURE);
}
 
24
24
 
CSCI 330 - UNIX and Newtork Programming
 
Detail: bind the socket
 
struct sockaddr_in echoserver;  // structure for address of server
// Construct the server sockaddr_in structure
memset(&echoserver, 0, sizeof(echoserver));       /* Clear struct */
echoserver.sin_family = AF_INET;                  /* Internet/IP */
echoserver.sin_addr.s_addr = INADDR_ANY;          /* Any IP address */
echoserver.sin_port = htons(atoi(argv[1]));       /* server port */
 
// Bind the socket
serverlen = sizeof(echoserver);
if (bind(sock, (struct sockaddr *) &echoserver, serverlen) < 0) {
 
perror("Failed to bind server socket");
 
exit(EXIT_FAILURE);
}
 
25
25
 
CSCI 330 - UNIX and Newtork Programming
 
Detail: receive from socket
 
addrlen = sizeof(echoserver);
received = recvfrom(sock, buffer, 256, 0,
              (struct sockaddr *) &echoserver, &addrlen);
 
cout << "Received: << received bytes\n";
buffer[received] = '\0';   /* Assure null-terminated string */
 
cout << "Server ("
     << inet_ntoa(echoserver.sin_addr)
     << ") echoed: " << buffer << endl;
 
26
26
 
CSCI 330 - UNIX and Newtork Programming
 
Detail: send to socket
 
// Construct the server sockaddr_in structure
memset(&echoserver, 0, sizeof(echoserver));       /* Clear struct */
echoserver.sin_family = AF_INET;                   /* Internet/IP */
echoserver.sin_addr.s_addr = inet_addr(argv[1]);   /* IP address */
echoserver.sin_port = htons(atoi(argv[2]));        /* server port */
// Send the message to the server
echolen = strlen(argv[3]);
if (sendto(sock, argv[3], strlen(argv[3]), 0,
 
(struct sockaddr *) &echoserver, sizeof(echoserver))
       
!= echolen) {
 
perror("Mismatch in number of sent bytes");
 
exit(EXIT_FAILURE);
}
 
27
27
 
CSCI 330 - UNIX and Newtork Programming
 
Summary
 
Transport layer
User datagram protocol
UDP programming
Example UDP client/server programs
 
28
28
 
CSCI 330 - UNIX and Newtork Programming
Slide Note

The Bash Shell

Copyright Department of Computer Science, Northern Illinois University, 2005

09-

Embed
Share

Explore the fundamentals of User Datagram Protocol (UDP) in Unix and Network Programming, covering topics such as UDP client/server programs, network layers, transport layer services, UDP packet format, and socket programming. Learn about the characteristics and usage of UDP, including its simple message-based, connection-less nature and its role as a fast, stateless protocol for transmitting data between hosts.

  • UDP
  • Unix
  • Network Programming
  • Transport Layer
  • Socket Programming

Uploaded on Sep 07, 2024 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol

  2. CSCI 330 - UNIX and Newtork Programming 2 Unit Overview Transport layer User datagram protocol UDP programming Example UDP client/server programs

  3. CSCI 330 - UNIX and Newtork Programming 3 Review: Network Layer also called: Internet Protocol (IP) Layer provides host to host transmission service, where hosts are not necessarily adjacent IP layer provides services: addressing hosts have global addresses: IPv4, IPv6 routing and forwarding find path from host to host

  4. CSCI 330 - UNIX and Newtork Programming 4 Transport Layer provides end-to-end communication services for applications provides multiple endpoints on a single node: port TCP: transmission control protocol connection oriented, guaranteed delivery stream oriented: UDP: user datagram protocol best effort datagram oriented: basis for: http, ftp, smtp, ssh basis for: dns, rtp

  5. CSCI 330 - UNIX and Newtork Programming 5 UDP simple message-based connection-less protocol transmits information in one direction from source to destination without verifying the readiness or state of the receiver uses datagram as message stateless and fast

  6. CSCI 330 - UNIX and Newtork Programming 6 UPD packet format bits 0 32 64 96 128 0 7 8 15 Source IP address Destination IP address Protocol Source Port Length 16 23 24 31 Zeros UDP length Destination Port Checksum 160+ Data

  7. CSCI 330 - UNIX and Newtork Programming 7 UDP programming common abstraction: socket first introduced in BSD Unix in 1981 socket is end-point of communication link identified as IP address + port number can receive data can send data

  8. CSCI 330 - UNIX and Newtork Programming 8 Socket system calls client server System call Meaning socket Create a new communication endpoint bind Attach a local address to a socket optional sendto Send(write) some data over the connection recvfrom Receive(read) some data over the connection close Release the connection

  9. CSCI 330 - UNIX and Newtork Programming 9 UDP communications pattern optional

  10. CSCI 330 - UNIX and Newtork Programming 10 System call: socket

  11. CSCI 330 - UNIX and Newtork Programming 11 System call: socket int socket(int domain, int type, int protocol) creates a new socket, as end point to a communications link domain is set to AF_INET type is set to SOCK_DGRAM for datagrams protocol is set to 0, i.e. default UDP returns socket descriptor: used in bind, sendto, recvfrom, close

  12. CSCI 330 - UNIX and Newtork Programming 12 System call: bind

  13. CSCI 330 - UNIX and Newtork Programming 13 System call: bind int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) assigns address to socket: IP number and port struct sockaddrholds address information will accept struct sockaddr_inpointer addrlen specifies length of addr structure returns 0 on success, -1 otherwise

  14. CSCI 330 - UNIX and Newtork Programming 14 structure sockaddr: 16bytes struct sockaddr { short char }; sa_family; sa_data[14]; /* address data /* address family */ */ struct sockaddr_in { short sin_family; /* address family unsigned short sin_port; /* port number:2 bytes */ struct in_addr sin_addr; /* IP address: 4 bytes */ /* pad to size of struct sockaddr */ char sin_zero[8]; }; */

  15. CSCI 330 - UNIX and Newtork Programming 15 structure sockaddr_in: members sin_family always: AF_INET sin_port /* port number: 2 bytes */ htons(4444) /* ensure network order */ sin_addr /* Internet address: 4 bytes */ s_addr = INADDR_ANY s_addr = inet_addr("127.0.0.1") /* address family */

  16. CSCI 330 - UNIX and Newtork Programming 16 Inet Address Manipulation

  17. CSCI 330 - UNIX and Newtork Programming 17 System call: recvfrom

  18. CSCI 330 - UNIX and Newtork Programming 18 System call: recvfrom ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) receives a datagram buf of size len from socket sockfd will wait until a datagram is available flags specifies wait behavior, e.g.: 0 for default src_addr will hold address information of sender struct sockaddrdefines address structure addrlen specifies length of src_addr structure returns the number of bytes received, i.e. size of datagram

  19. CSCI 330 - UNIX and Newtork Programming 19 System call: sendto

  20. CSCI 330 - UNIX and Newtork Programming 20 System call: sendto ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) sends datagram buf of size len to socket sockfd will wait if there is no ready receiver flags specifies wait behavior, e.g.: 0 for default dest_addr holds address information of receiver struct sockaddrdefines address structure addrlen specifies length of dest_addr structure returns the number of bytes sent, i.e. size of datagram

  21. CSCI 330 - UNIX and Newtork Programming 21 Example: UDP Programming simple server: echo sends all received datagrams back to sender simple client send datagram to server

  22. CSCI 330 - UNIX and Newtork Programming 22 Illustration: echoServer.cxx

  23. CSCI 330 - UNIX and Newtork Programming 23 Illustration: echoClient.cxx

  24. CSCI 330 - UNIX and Newtork Programming 24 Detail: create UDP socket int sock; // Create the UDP socket if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Failed to create socket"); exit(EXIT_FAILURE); }

  25. CSCI 330 - UNIX and Newtork Programming 25 Detail: bind the socket struct sockaddr_in echoserver; // structure for address of server // Construct the server sockaddr_in structure memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = INADDR_ANY; /* Any IP address */ echoserver.sin_port = htons(atoi(argv[1])); /* server port */ // Bind the socket serverlen = sizeof(echoserver); if (bind(sock, (struct sockaddr *) &echoserver, serverlen) < 0) { perror("Failed to bind server socket"); exit(EXIT_FAILURE); }

  26. CSCI 330 - UNIX and Newtork Programming 26 Detail: receive from socket addrlen = sizeof(echoserver); received = recvfrom(sock, buffer, 256, 0, (struct sockaddr *) &echoserver, &addrlen); cout << "Received: << received bytes\n"; buffer[received] = '\0'; /* Assure null-terminated string */ cout << "Server (" << inet_ntoa(echoserver.sin_addr) << ") echoed: " << buffer << endl;

  27. CSCI 330 - UNIX and Newtork Programming 27 Detail: send to socket // Construct the server sockaddr_in structure memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */ echoserver.sin_port = htons(atoi(argv[2])); /* server port */ // Send the message to the server echolen = strlen(argv[3]); if (sendto(sock, argv[3], strlen(argv[3]), 0, (struct sockaddr *) &echoserver, sizeof(echoserver)) perror("Mismatch in number of sent bytes"); exit(EXIT_FAILURE); } != echolen) {

  28. CSCI 330 - UNIX and Newtork Programming 28 Summary Transport layer User datagram protocol UDP programming Example UDP client/server programs

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#