Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.

Slides:



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

Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
UDP and Multi-thread Socket Programming
Lecture 16 Overview. Creating a TCP socket int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr;
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Sockets Basics Conectionless Protocol. Today IPC Sockets Basic functions Handed code Q & A.
Tutorial 8 Socket Programming
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
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.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Network Programming Tutorial #9 CPSC 261. A socket is one end of a virtual communication channel Provides network connectivity to any other socket anywhere.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
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)
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
 Wind River Systems, Inc Chapter - 13 Network Programming.
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.
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)
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
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.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
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?
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Elementary UDP Sockets
Socket Programming in C
Review: TCP Client-Server Interaction
CHAPTER 8 ELEMENTARY UDP SOCKETS
Chapter 2 Transport Layer Protocols TCP UDP SCTP
Lecture 4 Socket Programming Issues
UDP Sockets Programming
Socket Programming in C
Lecture 11 Overview.
TCP Sockets Programming
TCP/IP Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Socket Programming with UDP
Presentation transcript:

Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode

Creating a UDP socket int socket(int family, int type, int protocol) int sock; sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0){ printf("Unable to create the UDP socket\n"); return 0; } //end-if

Binding to well known address This is typically done by server only. int mysock; struct sockaddr_in myaddr; mysock = socket(AF_INET,SOCK_DGRAM, IPPROTO_UDP); myaddr.sin_family = AF_INET; myaddr.sin_port = htons(80); myaddr.sin_addr = htonl(INADDR_ANY); bind(mysock, &myaddr, sizeof(myaddr));

Sending UDP Datagrams int sendto(int sockfd, char* buff, size_t nbytes, int flags, struct sockaddr* to, int addrlen); sockfd is a UDP socket buff is the address of the data ( nbytes long) to is the address of a sockaddr containing the destination address. Return value is the number of bytes sent, or -1 on error.

sendto() You can send 0 bytes of data! Some possible errors : EBADF, ENOTSOCK : bad socket descriptor EFAULT : bad buffer address EMSGSIZE : message too large ENOBUFS : system buffers are full

More sendto() The return value of sendto() indicates how much data was accepted by the OS for sending as a datagram – not how much data made it to the destination. There is no error condition that indicates that the destination did not get the data!

Receiving UDP Datagrams int recvfrom(int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* from, int* fromaddrlen); sockfd is a UDP socket buff is the address of a buffer ( nbytes long) from is the address of a sockaddr. Return value is the number of bytes received and put into buff, or -1 on error.

recvfrom() If buff is not large enough, any extra data is lost forever! You can receive 0 bytes of data! The sockaddr at from is filled in with the address of the sender. You should set fromaddrlen before calling. If from and fromaddrlen are NULL we don’t find out who sent the data.

More recvfrom() Same errors as sendto, but also: EINTR : System call interrupted by signal. Unless you do something special - recvfrom doesn’t return until there is a datagram available!

Typical UDP client code 1. Create UDP socket. 2. Create sockaddr with address of server. 3. Call sendto(), sending request to the server.  No call to bind() is necessary! 4. Possibly call recvfrom() (if we need a reply).

Typical UDP Server code 1. Create UDP socket and bind to well known address. 2. Call recvfrom() to get a request, noting the address of the client. 3. Process request and send reply back with sendto().

UDP Echo Server int mysock; struct sockaddr_in myaddr, cliAddr; char msgbuf[MAXLEN]; int clilen; int msglen; mysock = socket(AF_INET,SOCK_DGRAM,IP_PROTO_UDP); myaddr.sin_family = AF_INET; myaddr.sin_port = htons(S_PORT); myaddr.sin_addr = htonl(INADDR_ANY); bind(mysock, &myaddr, sizeof(myaddr)); while (1) { len = sizeof(cliaddr); msglen=recvfrom(mysock,msgbuf, MAXLEN,0,&cliAddr,&clilen); sendto(mysock,msgbuf,msglen,0,&cliAddr,clilen); } !! NEED TO CHECK FOR ERRORS !!

Debugging Debugging UDP can be difficult. Write routines to print out sockaddrs. Include code that can handle unexpected situations.

Timeout when calling recvfrom() It might be nice to have each call to recvfrom() return after a specified period of time even if there is no incoming datagram. We can do this by using SIGALRM and wrapping each call to recvfrom() with a call to alarm() ◦ Will be covered later.

Connected mode A UDP socket can be used in a call to connect(). This simply tells the OS the address of the peer. No handshake is made to establish that the peer exists. No data of any kind is sent on the network as a result of calling connect() on a UDP socket.

Connected UDP Once a UDP socket is connected: ◦ can use sendto() with a null destination address ◦ can use write() and send() ◦ can use read() and recv() ◦ only datagrams from the peer will be returned. ◦ Asynchronous errors will be returned to the process.  OS specific, some won’t do this!

Asynchronous Errors What happens if a client sends data to a server that is not running? ◦ ICMP “port unreachable” error is generated by receiving host and sent to sending host. ◦ The ICMP error may reach the sending host after sendto() has already returned! ◦ The next call dealing with the socket could return the error.

Back to UDP connect() connect() is typically used with UDP when communication is with a single peer only. Many UDP clients use connect(). Some servers also (TFTP). It is possible to disconnect and connect the same socket to a new peer.