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

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1 A B C
Variations of the Turing Machine
Socket Programming Computer Networks, Spring 2008 Your TAs.
AP STUDY SESSION 2.
1
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.
Select from the most commonly used minutes below.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
David Burdett May 11, 2004 Package Binding for WS CDL.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
CALENDAR.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
Media-Monitoring Final Report April - May 2010 News.
Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
Break Time Remaining 10:00.
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Bits, Characters, Strings, and Structures Outline 16.1Introduction 16.2Structure Definitions.
1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to Operating originally.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
TESOL International Convention Presentation- ESL Instruction: Developing Your Skills to Become a Master Conductor by Beth Clifton Crumpler by.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
PIGEON PARTS QUIZ. 1. Name the extra membrane over the eye.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
Before Between After.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
PSSA Preparation.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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)
Advanced Sockets API-II Vinayak Jagtap
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Programming with UDP – II Covered Subjects: Creating UDP sockets Client Server Sending data Receiving data Connected mode.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Elementary UDP Sockets
CHAPTER 8 ELEMENTARY UDP SOCKETS
Chapter 8 Elementary UDP Socket
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
UDP Sockets Programming
Elementary UDP Sockets connectionless, unreliable, datagram
Presentation transcript:

UDP Sockets UDP: unreliable delivery, no connection DNS, NFS, SNMP

recvfrom and sento #include 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); Ambos retornam: número de bytes lidos ou escritos se OK, –1 p/ erro sockfd : descriptor buff : read/write buffer nbytes : number of bytes read/write flags : by now = 0 to : pointer to socket address structure addrlen : length of socket addr structure

recvfrom and sento #include 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); Ambos retornam: número de bytes lidos ou escritos se OK, –1 p/ erro To write a datagram with size zero is valid (20 bytes IPv4 + 8 bytes UDP header) recvfrom and addrlen can be both null regardless the sender recvfrom and sento can be used by TCP and T / TCP

UDP Echo Server (1) 1 #include "unp.h" 2 int 3 main(int argc, char **argv) 4 { 5 int sockfd; 6 struct sockaddr_in servaddr, cliaddr; 7 sockfd = Socket(AF_INET, SOCK_DGRAM, 0); 8 bzero(&servaddr, sizeof(servaddr)); 9 servaddr.sin_family = AF_INET; 10 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 11 servaddr.sin_port = htons(SERV_PORT); 12 Bind(sockfd, (SA *) &servaddr, sizeof(servaddr)); 13 dg_echo(sockfd, (SA *) &cliaddr, sizeof(cliaddr)); 14 }

UDP Echo Server (2) 1 #include "unp.h" 2 void 3 dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) 4 { 5 int n; 6 socklen_t len; 7 char mesg[MAXLINE]; 8 for ( ; ; ) { 9 len = clilen; 10 n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); 11 Sendto(sockfd, mesg, n, 0, pcliaddr, len); 12 } 13 }

UDP Server No EOF TCP - concorrente UDP - interativo

TCP Client Server with 2 clients

UDP Server

A single server process and a single socket Single buffer for all datagrams

UDP Client Echo 1 #include "unp.h" 2 int 3main(int argc, char **argv) 4 { 5 int sockfd; 6 struct sockaddr_in servaddr; 7 if(argc != 2) 8 err_quit("usage: udpcli "); 9 bzero(&servaddr, sizeof(servaddr)); 10 servaddr.sin_family = AF_INET; 11 servaddr.sin_port = htons(SERV_PORT); 12 Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); 13 sockfd = Socket(AF_INET, SOCK_DGRAM, 0); 14 dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr)); 15 exit(0); 16 }

UDP Client Echo 1 #include "unp.h" 2 void 3 dg_cli(FILE *fp,int sockfd,const SA *pservaddr,socklen_t,servlen) 4 { 5 int n; 6 char sendline[MAXLINE], recvline[MAXLINE + 1]; 7 while (Fgets(sendline, MAXLINE, fp) != NULL) { 8 Sendto(sockfd,sendline,strlen(sendline),0,pservaddr,servlen); 9 n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); 10 recvline[n] = 0; /* null terminate */ 11 Fputs(recvline, stdout); 12 } 13 }

Lost Datagrams UDP Client-server, non reliable delivery. If either datagram or ack is lost client- server becomes blocked

Verifying received responses Any process can send datagrams to client ephemeral port recvfrom retorns IP address of the sender

Verifying received responses 1 #include "unp.h" 2 void 3 dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) 4 { 5 int n; 6 char sendline[MAXLINE], recvline[MAXLINE + 1]; 7 socklen_t len; 8 struct sockaddr *preply_addr; 9 preply_addr = Malloc(servlen); 10 while (Fgets(sendline, MAXLINE, fp) != NULL) { 11 Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); 12 len = servlen; 13 n = Recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len); 14 if (len != servlen || memcmp(pservaddr, preply_addr, len) != 0) { 15 printf("reply from %s (ignored)\n", Sock_ntop(preply_addr, len)); 16 continue; 17 } 18 recvline[n] = 0; /* null terminate */ 19 Fputs(recvline, stdout); 20 } 21 }

Non responsive server arp who-has freebsd4 tell macosx ( ) arp reply freebsd4 is-at 0:40:5:42:d6:de ( ) macosx > freebsd4.9877: udp ( ) freebsd4 > macosx: icmp: freebsd4 udp port 9877 unreachable SENDTO – returns succes if there was space in the buffer to store datagram

Non responsive server Asynchronous error – there is no way to discover IP address and port of non responsive server when a single socket is used to send request to different servers connected UDP socket receives data from a specific peer Error returned to process

UDP Client Server

UDP Client server

UDP available information Options IP_RECVDSTADDR and IPv6_PKTINFO

UDP connect There is no three-way handshaking – There is no connection Kernel stores IP address and port number of peer stored in structure passed when connect is called Should use either write or send but no sento

UDP connect When use sento pointer to structure needs to be null as well as the length of the structure Should not use recvfrom. Use either read or recv.

UDP connect Datagrams with different protocol address than the one specifeid by connect is not passed Asynchronous error returned to connect connected UDP socket

UDP connect

Process can call connect several times: Specify another IP address o Disconnect socket – call connect with parameter sin_family = AF_UNSPEC

dg_cli com connect 1 #include "unp.h" 2 void 3 dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) 4 { 5 int n; 6 char sendline[MAXLINE], recvline[MAXLINE + 1]; 7 Connect(sockfd, (SA *) pservaddr, servlen); 8 while (Fgets(sendline, MAXLINE, fp) != NULL) { 9 Write(sockfd, sendline, strlen(sendline)); 10 n = Read(sockfd, recvline, MAXLINE); 11 recvline[n] = 0; /* null terminate */ 12 Fputs(recvline, stdout); 13 } 14 }

dg_cli com connect macosx % tcpdump macosx > freebsd4.9877: udp ( ) freebsd4 > macosx: icmp: freebsd4 udp port 9877 unreachable

UDP flow control 1 #include "unp.h" 2 #define NDG 2000 /* datagrams to send */ 3 #define DGLEN 1400 /* length of each datagram */ 4 void 5 dg_cli(FILE *fp,int sockfd,const SA *pservaddr,socklen_t servlen) 6 { 7 int i; 8 char sendline[DGLEN]; 9 for (i = 0; i < NDG; i++) { 10 Sendto(sockfd, sendline, DGLEN, 0, pservaddr, servlen); 11 } 12 }

UDP flow control 1 #include "unp.h" 2 static void recvfrom_int(int); 3 static int count; 4 void 5 dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) 6 { 7 socklen_t len; 8 char mesg[MAXLINE]; 9 Signal(SIGINT, recvfrom_int); 10 for ( ; ; ) { 11 len = clilen; 12 Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); 13 count++; 14 } 15 } 16 static void 17 recvfrom_int(int signo) 18 { 19 printf("\nreceived %d datagrams\n", count); 20 exit(0); 21 }

UDP flow control freebsd % netstat -s -p udp udp: datagrams received 0 with incomplete header 0 with bad data length field 0 with bad checksum 0 with no checksum 832 dropped due to no socket 16 broadcast/multicast datagrams dropped due to no socket 1971 dropped due to full socket buffers 0 not for hashed pcb delivered datagrams output

UDP flow control freebsd % udpserv06 Iniciando o servidor Neste ponto, executa-se o cliente ^C Digita-se o caracter de interrupção depois que o cliente recebe 30 datagramas freebsd % netstat -s -p udp udp: datagrams received 0 with incomplete header 0 with bad data length field 0 with bad checksum 0 with no checksum 832 dropped due to no socket 16 broadcast/multicast datagrams dropped due to no socket 3941 dropped due to full socket buffers 0 not for hashed pcb delivered datagrams output

UDP receive buffer Use option SO_RCVBUF 1 #include "unp.h" 2 static void recvfrom_int(int); 3 static int count; 4 void 5 dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) 6 { 7 int n; 8 socklen_t len; 9 char mesg[MAXLINE]; 10 Signal(SIGINT, recvfrom_int); 11 n = 220 * 1024; 12 Setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)); 13 for ( ; ; ) { 14 len = clilen; 15 Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len); 16 count++; 17 } 18 }...

UDP receive buffer static void 20 recvfrom_int(int signo) 21 { 22 printf("\nreceived %d datagrams\n", count); 23 exit(0); 24 }

UDP and TCP Server using select 1 #include "unp.h" 2 int 3 main(int argc, char **argv) 4 { 5 int listenfd, connfd, udpfd, nready, maxfdp1; 6 char mesg[MAXLINE]; 7 pid_t childpid; 8 fd_set rset; 9 ssize_t n; 10 socklen_t len; 11 const int on = 1; 12 struct sockaddr_in cliaddr, servaddr; 13 void sig_chld(int); 14 /* create listening TCP socket */ 15 listenfd = Socket(AF_INET, SOCK_STREAM, 0);...

16 bzero(&servaddr, sizeof(servaddr)); 17 servaddr.sin_family = AF_INET; 18 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 19 servaddr.sin_port = htons(SERV_PORT); 20 Setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); 21 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 22 Listen(listenfd, LISTENQ); 23 /* create UDP socket */ 24 udpfd = Socket(AF_INET, SOCK_DGRAM, 0); 25 bzero(&servaddr, sizeof(servaddr)); 26 servaddr.sin_family = AF_INET; 27 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 28 servaddr.sin_port = htons(SERV_PORT); 29 Bind(udpfd, (SA *) &servaddr, sizeof(servaddr));... UDP and TCP Server using select

Signal(SIGCHLD, sig_chld); /* must call waitpid() */ 31 FD_ZERO(&rset); 32 maxfdp1 = max(listenfd, udpfd) + 1; 33 for ( ; ; ) { 34 FD_SET(listenfd, &rset); 35 FD_SET(udpfd, &rset); 36 if ( (nready = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0) { 37 if (errno == EINTR) 38 continue; /* back to for() */ 39 else 40 err_sys("select error"); 41 }... UDP and TCP Server using select

if (FD_ISSET(listenfd, &rset)) { 43 len = sizeof(cliaddr); 44 connfd = Accept(listenfd, (SA *) &cliaddr, &len); 45 if ( (childpid = Fork()) == 0) { /*child process */ 46 Close(listenfd); /* close listening socket */ 47 str_echo(connfd); /* process the request */ 48 exit(0); 49 } 50 Close(connfd); /* parent closes connected socket */ 51 } 52 if (FD_ISSET(udpfd, &rset)) { 53 len = sizeof(cliaddr); 54 n = Recvfrom(udpfd, mesg, MAXLINE, 0, (SA *) &cliaddr, &len); 55 Sendto(udpfd, mesg, n, 0, (SA *) &cliaddr, len); 56 } 57 } 58 } UDP and TCP Server using select