Elementary UDP Sockets

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Programming with TCP – I
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Elementary TCP Sockets socket function connect function bind function listen function accept function fork and exec functions Concurrent servers close.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Computer Networks Sockets.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
Tutorial 8 Socket Programming
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
2: Application Layer 1 Chapter 2 Application Layer Computer Networking: A Top Down Approach, 5th edition. Jim Kurose, Keith Ross Addison-Wesley, April.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
Lecture 8 UDP Sockets & I/O Multiplexing
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
Elementary TCP Sockets
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
Sirak Kaewjamnong Computer Network Systems
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Chapter 2 Applications and Layered Architectures Sockets.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
Advanced Sockets API-II Vinayak Jagtap
Cli/Serv.: sockets 3/91 Client/Server Distributed Systems v Objectives –describe iterative clients and servers using the UDP protocol ,
Lecture 15 Overview.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction A Simple Daytime Client A Simple Daytime Server
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Today’s topic: UDP Reliable communication over UDP.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Review: –Concurrent server and multiplexed server How they work? Which one is better?
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
I/O Multiplexing.
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
CS 1652 Jack Lange University of Pittsburgh
Network Programming CSC- 341
Socket Programming in C
Review: TCP Client-Server Interaction
CHAPTER 8 ELEMENTARY UDP SOCKETS
Chapter 8 Elementary UDP Socket
UNIX Domain sockets The Linux Programming Interface (ch 57)
Network Programming CSC- 341
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
UDP Sockets Programming
Socket Programming in C
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Elementary UDP Sockets connectionless, unreliable, datagram
Internet Networking recitation #8
Socket Programming with UDP
Presentation transcript:

Elementary UDP Sockets

Outline Elementary UDP Sockets Information to write a complete UDP client and server

Typical Scenario between UDP client/server Typical UDP client Client does not establish a connection with the server Client sends a datagram to the server using sendto function Typical UDP server Does not accept a connection from a client Server calls recvfrom function which waits until data arrives from some client

Socket functions for UDP client/server UDP Server socket( ) bind( ) recvfrom( ) sendto( ) close( ) Process request block until datagram received from a client UDP Client data(request) data(reply)

recvfrom and sendto Function 1/2 #include<sys/socket.h> ssize_t recvfrom(int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen); ssize_t sendto(int sockfd, const void *buff, size_t nbytes, int flags, const struct sockaddr *to, socklen_t addrlen); //Both return: number of bytes read or written if OK,-1 on error Both return the amount of user data in the datagram received Writing a datagram of length 0 is acceptable (return value from recvfrom?) Closing a UDP connection does not make sense?

UDP Echo server: main //source code is udpcliserv/udpserv01.c #include “unp.h” int main(int argc, char **argv) { int sockfd; struck sockaddr_in servaddr,cliaddr; sockfd=Socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_fammily=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(SERV_PORT); bind(sockfd, (SA *) &servaddr,sizeof(servaddr)); dg_echo(sockfd, (SA *) &cliaddr,sizeof(cliaddr)); }

UDP Echo server: dg_echo function //source code is /lib/dg_echo.c #include “unp.h” void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) { int n; socklen_t len; char mesg[MAXLINE]; for( ; ; ) { len=clilen; n=Recvfrom(sockfd, mseg, MAXLINE, 0, pcliaddr, &len); sendto(sockfd, mesg, n, 0, pcliaddr, len); } Iterative Server Implied Queuing

Summary of TCP client-server with two clients. TCP versus UDP server 1/2 connection fork client TCP server child listening Summary of TCP client-server with two clients.

Summary of UDP client-server with two clients. TCP versus UDP server 2/2 Socket receive buffer client server UDP datagram Summary of UDP client-server with two clients.

UDP Echo Client: main //source code is udpcliserv/udpcli01.c #include “unp.h” int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr; if (argc != 2) err_quit( “usage : udpcli <Ipaddress>”); bzero(&servaddr, sizeof(servaddr); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); sockfd = Socket(AF_INET, SOCK_DGRAM, 0); dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr); exit(0); }

UDP Echo Client: dg_cli function //source code is lib/dg_cli.c #include “unp.h” void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n; char sendline[MAXLINE], recvline[MAXLINE+1]; while(Fgets(sendline, MAXLINE, fp) != NULL) { sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); recvline[n] = 0; /* null terminate */ Fputs(recvline,stdout); } Did not assign an ephemeral port to UDP Socket? First time sendto is called

Lost Datagrams If the client datagram arrives at the server but the server’s reply is lost, the client will block forever in its call to recvfrom. The only way to prevent this is to place a timeout on the recvfrom Timeout not the entire solution Do not know whether client datagram never made it to server Or, Server reply never made it back Will fix later

Verifying Received Response 1/3 //Need to return IP address and port of who sent back reply //source code in udpcliserv/dgcliaddr.c #include “unp.h” void dg_cli(FILE *fp, int sock, const SA *pseraddr, socklen_t servlen) { int n; char sendline[MAXLINE], recvline[MAXLINE]; socklen_t len; struct sockaddr *preply_addr; preply_addr = Malloc(servlen); while(Fgets(sendline, MAXLINE, fp) ! = NULL) { Sendto(sockfd,sendline, strlen(sendline), 0, pservaddr, servlen); len = servlen; n = Recvfrom(sockfdm, recvline, MAXLINE, 0, preply_addr,&len); /* continued in next slide */

Verifying Received Response 2/3 //Need to return IP address and port of who sent back reply //source code in udpcliserv/dgcliaddr.c if(len != servlen || memcmp(pservaddr, preply_addr, len) != 0) { printf(“reply from %s (ignore)\n”, Sock_ntop(preply_addr, len); continue; } recvline[n] = 0; /*NULL terminate */ Fputs(recvline, stdout); //program can fail if server is multi-homed (The server has not bound an IP address to its socket, the kernel chooses the source address for the IP datagram outgoing from the server)

Verifying Received Response 3/3 Program can fail if server is multi-homed (The server has not bound an IP address to its socket, the kernel chooses the source address for the IP datagram outgoing from the server) Can this be solved otherwise? Verify respondent’s host name by looking up its name from DNS (will see later how to do that) Other solution Create a socket for every IP address configured on host Bind IP address to socket Wait for any of these sockets to become readable Reply from this socket

Server not Running Client blocks forever in the call to recvfrom. ICMP error “port unreachable” is an asynchronous error Error caused by sendto but sendto returns successfully (only means there was room for resulting IP datagram on interface output queue). ICMP error returned later  asynchronous error The basic rule is that asynchronous errors are not returned for UDP sockets unless the socket has been connected

connect function with UDP This does not result in anything like a TCP connection: there is no three-way handshake. Instead, the kernel records the IP address and port number of the peer and returns immediately to calling process With a connected UDP socket, three things change: 1. We can no longer specify the destination IP address and port for an output operation. That is, we do not use sendto but use write or send instead. (Can use sendto but fifth argument is null, and sixth is zero) 2. We do not use recvfrom but read or recv instead. Only datagrams returned by the kernel for an input operation on a connected UDP socket are those arriving from protocol address specified in connect 3. Asynchronous errors are returned to the process for a connected UDP socket

connect function with UDP } Stores peer IP address and port#from connect UDP UDP datagram ??? application peer UDP datagram from some other IP address and/or port# read write When an application calls sendto on an unconnected UDP socket, Berkeley-derived kernels temporarily connect the socket, send the datagram, and then unconnect the socket (see discussion on page 255)

UDP Echo Client: dg_cli revisited //source code is udpcliserv/dgcliconnect.c #include “unp.h” void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n; char sendline[MAXLINE], recvline[MAXLINE+1]; Connect(sockfd, (SA *) pservaddr, servlen); while(Fgets(sendline, MAXLINE, fp) != NULL) { Write(sockfd, sendline, strlen(sendline)); n = Read(sockfd, recvline, MAXLINE); recvline[n] = 0; /* null terminate */ Fputs(recvline,stdout); } } //ICMP error received after attempting to send the first datagram to server