Elementary UDP Sockets connectionless, unreliable, datagram

Slides:



Advertisements
Similar presentations
UDP Sockets UDP: unreliable delivery, no connection DNS, NFS, SNMP.
Advertisements

Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
I/O Multiplexing: select and poll
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.
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.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 TCP Sockets Computer Network Programming. 2 TCP Echo Server We will write a simple echo server and client –client read a line of text from standard.
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.
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
I/O Multiplexing Capability of tell the kernel that wants to be notified when one or more I/O conditions are ready. For example, I/O data is available.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Protocol Programs that communicate across a network must agree on a protocol on how they will communicate High-level decisions must be made on which program.
Lecture 8 UDP Sockets & I/O Multiplexing
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
ECE453 – Introduction to Computer Networks Lecture 15 – Transport Layer (II)
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.
Chapter 5 TCP Client/Server Example. TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and.
Elementary TCP Sockets
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
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
Elementary TCP Sockets
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.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
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)
2: Application Layer1 Homework r Collect Homework 1 r Assign Homework 2 m Problems Ch 2#1,4,6,7 (two graded) m Due Wednesday, 10 September.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Chapter18 broadcasting. contents Introduction broadcast address unicast versus broadcast dg_cli function using broadcasting Race conditions.
Chapter 14 Unix domain protocol. contents Introduction unix domain socket address structure socketpair socket function unix domain stream client-server.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
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 ,
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
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.
Today’s topic: UDP Reliable communication over UDP.
Review: –Concurrent server and multiplexed server How they work? Which one is better?
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
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.
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.
1 School of Computing Science Simon Fraser University CMPT 471: Computer Networking II Introduction Instructor: Dr. Mohamed Hefeeda.
CSCD433 Advanced Networks Winter 2017 Lecture 14
I/O Multiplexing.
Chapter 5. TCP Client-Server Example
CS 1652 Jack Lange University of Pittsburgh
Elementary UDP Sockets
Network Programming CSC- 341
Chapter4 Elementary TCP Socket
CSCD433 Advanced Networks Spring 2016 Lecture 16a
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
Lecture 11 Overview.
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
TCP Client-Server Example
Internet Networking recitation #8
Socket Programming with UDP
Presentation transcript:

Elementary UDP Sockets connectionless, unreliable, datagram recvfrom and sendto functions UDP echo server UDP echo client Verify received responses connect function with UDP Rewrite dg_cli function with connect Lack of flow control with UDP Determine outgoing interface with UDP TCP and UDP echo server using select

recvfrom and sendto Functions #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 UDP Client socket ( ) sendto ( ) recvfrom ( ) close ( ) UDP Server socket ( ) bind ( ) recvfrom ( ) sendto ( )

UDP Echo Server: main Function #include "unp.h" int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr, cliaddr; sockfd = Socket(AF_INET, SOCK_DGRAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = 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)); } udpcliserv/udpserv01.c

UDP Echo Server: dg_echo Function 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, mesg, MAXLINE, 0, pcliaddr, &len); Sendto(sockfd, mesg, n, 0, pcliaddr, len); }

Comparing TCP and UDP with Two Clients fork fork client server listening server client child server child TCP TCP TCP connection connection TCP client-server with two clients client server client UDP UDP UDP socket receive buffer datagram datagram UDP client-server with two clients Most TCP servers are concurrent and most UDP servers are iterative.

UDP Echo Client: main Function #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); } udpcliserv/udpcli01.c

UDP Echo Client: dg_cli Function 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); }

Problems with UDP Sockets Lost datagrams (client request or server reply): recvfrom blocks forever place a timeout, but don’t know whether request or reply gets lost Malicious datagrams inter-mixed with server replies: ignore any received datagrams not from that server allocate another socket address structure and compare returned address

Problems with UDP Sockets (Cont.) For multi-homed server, verifying address may not work (server reply may go through another outgoing interface) solution 1: verify server domain name, instead solution 2: multi-homed UDP server creates one socket for every interface (IP addr), bind IP addresses to sockets, use select across all sockets

Problems with UDP Sockets (Cont.) Server not running: ICMP port unreachable error (asynchronous error) asynchronous errors not returned for UDP sockets unless the socket has been connected (reason?: considering a client sending 3 datagrams to 3 servers, recvfrom has no way to know the destination of the datagram causing the error) recvfrom blocks forever solution: call connect on a UDP socket

Problems with UDP Sockets (Cont.) Lack of flow control: considering dg_cli in a client sendto 2000 1400-byte datagrams to the server client may overrun the server (e.g. 96% loss rate: mostly lost due to receive buffer overflow, some due to network congestion) use netstat -s to check the loss solution: use SO_RCVBUF option to enlarge buffer, use request-reply model instead of bulk transfer

connect Function with UDP connect on UDP socket: no 3-way handshake, kernel only records the connected dest address For a connected UDP socket (compared to unconnected UDP socket): no longer specify dest IP addr/port for output, use write/send instead of sendto use read/recv instead of recvfrom asynchronous errors are returned to the process for a connected UDP socket Used only if the client/server uses the UDP socket to communicate with exactly one peer

connect Function with UDP (cont.) connect multiple times for a UDP socket: specify a new IP address/port to communicate unconnect the socket (AF_UNSPEC) Call sendto for two datagrams on an unconnected UDP socket (temporary connecting): steps in kernel: connect/output/unconnect, connect/output/unconnect (too much overhead) solution: connect before write multiple datagrams

dg_cli Function That Calls connect 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); }

connect to Determine Outgoing Interface with UDP No way to know the outgoing interface of an unconnected UDP socket Side effect of connect on UDP socket: kernel chooses the local IP address by searching routing table process calls getsockname to obtain the local IP addr and port

Combined TCP and UDP Echo Server Using select A single server using select to multiplex a TCP socket and a UDP socket: create listening TCP socket create UDP socket establish signal handler for SIGCHLD prepare for select call select handle new client connection handle arrival of datagram

TCP and UDP Echo Server Using Select udpcliserv/udpservselect.c #include "unp.h" int main(int argc, char **argv) { int listenfd, connfd, udpfd, nready, maxfdp1; char mesg[MAXLINE]; pid_t childpid; fd_set rset; ssize_t n; socklen_t len; const int on = 1; struct sockaddr_in cliaddr, servaddr; void sig_chld(int); /* 4create listening TCP socket */ listenfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ); /* 4create UDP socket */ udpfd = Socket(AF_INET, SOCK_DGRAM, 0); Bind(udpfd, (SA *) &servaddr, sizeof(servaddr));

Signal(SIGCHLD, sig_chld);/* must call waitpid() */ FD_ZERO(&rset); maxfdp1 = max(listenfd, udpfd) + 1; for ( ; ; ) { FD_SET(listenfd, &rset); FD_SET(udpfd, &rset); if ( (nready = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0) { if (errno == EINTR) continue; /* back to for() */ else err_sys("select error"); } if (FD_ISSET(listenfd, &rset)) { len = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &len);

if ( (childpid = Fork()) == 0) { /* child process */ Close(listenfd); /* close listening socket */ str_echo(connfd); /* process the request */ exit(0); } Close(connfd); /* parent closes connected socket */ if (FD_ISSET(udpfd, &rset)) { len = sizeof(cliaddr); n = Recvfrom(udpfd, mesg, MAXLINE, 0, (SA *) &cliaddr, &len); Sendto(udpfd, mesg, n, 0, (SA *) &cliaddr, len);

Summary Lots of features in TCP are lost with UDP: detecting lost packets, retransmitting, verifying responses as being from correct peer, flow control, etc. Some reliability can be added UDP sockets may generate asynchronous errors reported only to connected sockets Use a request-reply model