UDP Sockets Programming

Slides:



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

Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
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.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
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.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
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.
Elementary TCP Sockets
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.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
 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.
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.
Lecture 15 Overview.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
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.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
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?
Introduction to Sockets
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 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.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
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.
Sockets and Beginning Network Programming
UNIX Sockets COS 461 Precept 1.
Elementary UDP Sockets
Socket Programming in C
Review: TCP Client-Server Interaction
CHAPTER 8 ELEMENTARY UDP SOCKETS
Transport layer API: Socket Programming
Chapter 2 Transport Layer Protocols TCP UDP SCTP
Network Programming CSC- 341
Lecture 4 Socket Programming Issues
Socket Programming in C
Lecture 11 Overview.
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Lecture 2 Socket Programming
Elementary UDP Sockets connectionless, unreliable, datagram
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Socket Programming with UDP
Presentation transcript:

UDP Sockets Programming Creating UDP sockets. Client Server Sending data. Receiving data. Connected Mode.

Creating a UDP socket int sock; sock = socket( PF_INET, SOCK_DGRAM, int socket(int family,int type,int proto); int sock; sock = socket( PF_INET, SOCK_DGRAM, 0); if (sock<0) { /* ERROR */ }

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

Accept() sockfd is the passive mode TCP socket. int accept( int sockfd, struct sockaddr* cliaddr, socklen_t *addrlen); sockfd is the passive mode TCP socket. Cliaddr is a pointer to allocated space. Addrlen is a value-result argument, must be set to the size of cliaddr, will be set on return to be the number of bytes in cliaddr set by the call to accept.

Sending UDP Datagrams sockfd is a UDP socket ssize_t sendto( int sockfd, void *buff, size_t nbytes, int flags, const struct sockaddr* to, socklen_t 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 O.S. 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 ssize_t recvfrom( int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr* from, socklen_t *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 Create UDP socket. Create sockaddr with address of server. Call sendto(), sending request to the server. No call to bind() is necessary! Possibly call recvfrom() (if we need a reply).

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

UDP Echo Server NEED TO CHECK FOR ERRORS!!! int mysock; struct sockaddr_in myaddr, cliaddr; char msgbuf[MAXLEN]; socklen_t clilen; int msglen; mysock = socket(PF_INET,SOCK_DGRAM,0); 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. Use trace, strace, ptrace, truss, etc. 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()

recvfrom()and alarm() signal(SIGALRM, sig_alrm); alarm(max_time_to_wait); if (recvfrom(…)<0) if (errno==EINTR) /* timed out */ else /* some other error */ /* no error or time out - turn off alarm */ alarm(0);

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

Connected UDP Once a UDP socket is connected: can use sendto() with a null dest. 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 send 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 (TFTP). It is possible to disconnect and connect to another peer.