1 Pertemuan 6 Elementary UDP Sockets Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.

Slides:



Advertisements
Similar presentations
1 Pertemuan 9 Windows Socket ( WinSock ) Matakuliah: H0483 – Network Programming Tahun: 2008 Versi: 2.0.
Advertisements

1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Hyperlink & Form Pertemuan 11 Matakuliah: L0182 / Web & Animation Design Tahun: 2008.
Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Pertemuan 13 Threads Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.
Network Layer Pertemuan 17 Matakuliah: H0484/Jaringan Komputer Tahun: 2007.
1 Pertemuan 16 First & Second Conditionals Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
1 Pertemuan 12 Binary Search Tree Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
1 Pertemuan 04 Proxy/Cache Matakuliah: H0491/Praktikum Jaringan Komputer Tahun: 2005 Versi: 1/0.
Pertemuan <<12>> Paralel dan Basis Data Terdistribusi
1 Pertemuan 22 Radix Sort Matakuliah: T0016/Algoritma dan Pemrograman Tahun: 2005 Versi: versi 2.
1 Pertemuan 15 The Business Owner’s View Matakuliah: A0194/Pengendalian Rekayasa Ulang Informasi Tahun: 2005 Versi: 1/5.
1 Pertemuan 4 HTTP Protocol Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.
1 Pertemuan 22 Expressions of Time and preference Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
Dynamic SQL Pertemuan 11 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
12 - Organisation Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 17 Third Conditional Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
08 - Employment Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 10 Arsitektur Jaringan Model OSI Matakuliah: H0174/Jaringan Komputer Tahun: 2006 Versi: 1/0.
1 Pertemuan 21 More Future Forms Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
1 Pertemuan 01 Pandangan Umum Jaringan Matakuliah: H0372/Manajemen Jaringan Tahun: 2005 Versi: 1/0.
1 Pertemuan 26 Object Relational Database Management System (Lanjutan) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 16 Object Query Language (Lanjutan bagian 3) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 23 Object database design (Lanjutan bagian 2) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 19 Layer Network Matakuliah: H0174/Jaringan Komputer Tahun: 2006 Versi: 1/0.
1 Pertemuan 07 Pendugaan Parameter Matakuliah: I0262 – Statistik Probabilitas Tahun: 2007 Versi: Revisi.
1 Pertemuan 13 BACK PROPAGATION Matakuliah: H0434/Jaringan Syaraf Tiruan Tahun: 2005 Versi: 1.
1 Pertemuan 24 Object database design (Lanjutan bagian 3) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 7 Future Continuous & Future Perfect Matakuliah: G0134/Grammar III Tahun: 2005 Versi: revisi 1.
1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.
1 Pertemuan 03 Routing Matakuliah: H0451/Praktikum Jaringan Komputer Tahun: 2006 Versi: 1/0.
06 - Advertising Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 02 LAN Matakuliah: H0451/Praktikum Jaringan Komputer Tahun: 2006 Versi: 1/0.
1 Pertemuan 14 Object Query Language (Lanjutan bagian 1) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 5 The structure part of object data model Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
07 - Advertising Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 9 Making an outline Matakuliah: G1072 – Reading 1 Tahun: 2005 Versi: revisi 0.
03 - Globalisation Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 7 Scanning Matakuliah: G1072 Reading 1 Tahun: 2005 Versi: revisi 0.
1 Pertemuan 21 Audit Reporting Matakuliah:A0274/Pengelolaan Fungsi Audit Sistem Informasi Tahun: 2005 Versi: 1/1.
1 Pertemuan 01 Teknologi Jaringan Matakuliah: H0242 / Keamanan Jaringan Tahun: 2006 Versi: 1.
1 Pertemuan 11 QUIZ Matakuliah: J0274/Akuntansi Manajemen Tahun: 2005 Versi: 01/00.
11 - Innovation Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
Pertemuan 20 Character oriented terminal
1 Pertemuan 20 Time & Condition Clauses with Future reference Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
Transport Layer Pertemuan 24 Matakuliah: H0484/Jaringan Komputer Tahun: 2007.
1 Pertemuan 3: TRAVELING THROUGH TIME ZONES Matakuliah: G0942/Listening 1 Tahun: 2005 Versi: baru.
1 Pertemuan 18 I wish, If only Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
1 Pertemuan 13 AVL Tree Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
1 Pertemuan #3 Clocks and Realtime Matakuliah: H0232/Sistem Waktu Nyata Tahun: 2005 Versi: 1/5.
1 Pertemuan 14 Phrases of agreement Matakuliah: G0134/Grammar III Tahun: 2005 Versi: revisi 1.
1 Pertemuan 9 JARINGAN LEARNING VECTOR QUANTIZATION Matakuliah: H0434/Jaringan Syaraf Tiruan Tahun: 2005 Versi: 1.
1 Pertemuan 25 Making It Happen Matakuliah: A0194/Pengendalian Rekayasa Ulang Informasi Tahun: 2005 Versi: 1/5.
13 - Organisation Matakuliah: G0622/Bahasa Inggris 1 Tahun: 2005 Versi: 1.01.
1 Pertemuan 19 Building Object Database Application (Lanjutan bagian 2) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 8 The Object Definition Language (Lanjutan) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 26 Making It Happen Matakuliah: A0194/Pengendalian Rekayasa Ulang Informasi Tahun: 2005 Versi: 1/5.
1 Pertemuan 24 Forming the Passive Matakuliah: G0134 – Grammar III Tahun: 2005 Versi: revisi 1.
1 Pertemuan 16 The Business Owner’s View Matakuliah: A0194/Pengendalian Rekayasa Ulang Informasi Tahun: 2005 Versi: 1/5.
1 Pertemuan 10 AC Circuit Power Analysis Matakuliah: H0042/Teori Rangkaian Listrik Tahun: 2005 Versi:
1 Pertemuan 23 Making It Happen Matakuliah: A0194/Pengendalian Rekayasa Ulang Informasi Tahun: 2005 Versi: 1/5.
UDP Socket Programming
Pertemuan 04 Proxy/Cache
Pertemuan 20 The Business Views of the Technology Architecture
Table Pertemuan 10 Matakuliah : L0182 / Web & Animation Design
Pertemuan 20 Building Object Database Application (Lanjutan bagian 3)
Pertemuan 22 The Business Views of the Technology Architecture
Pertemuan 7 I/O Multiplexing
Pertemuan 11 Model TCP/IP
Matakuliah : Web Design
Presentation transcript:

1 Pertemuan 6 Elementary UDP Sockets Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0

2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Menghasilkan program dengan menggunakan UDP Sockets

3 Outline Materi Recvfrom and sendto functions UDP Echo Server UDP Echo Client Lost Datagrams

4 Introduction Connectionless Unreliable Datagram protocol Beberepa aplikasi yg terkenal yg menggunakan UDP : –DNS (Domain Name System) –NFS (Network File System) SNMP (Simple Network Management Protocol)

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

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

7 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));

8 Sending UDP Datagrams 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.

9 Fungsi 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 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!!!

10 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.

11 Fungsi 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.

12 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).

13 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().

14 UDP Echo Server 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); }

15 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()

16 Fungsi recvfrom() dan alarm() signal(SIGALRM, sig_alrm); alarm(max_time_to_wait); if (recvfrom(…)<0) if (errno==EINTR) /* timed out */ else /* some other error */ else /* no error or time out - turn off alarm */ alarm(0); Lihat : Gambar 8.5 dan Gambar 8.6

17 UDP Echo Client Int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr; if(argc != 2) { printf(“usage: udpcli ”); exit(0); } 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); While() { Sendto( Recvfrom( }

18 Lost Datagrams Program UDP Client – Server pada contoh diatas : tidak handal (not reliable) Jika Server tdk running, program client di block selamanya pada saat memanggil fungsi recvfrom Jika client datagram sampai pada server, tetapi datagram yg dikirim/dibalas oleh server hilang, maka client akan di blok lagi Salah satu solusi : menambah fasilitas time-out pada client

19 Verifying Received Response

20 Server Not Running The Client Blocks forever in its call to recvfrom

21 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 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

22 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

23 Asynchronous Errors What happens if a client sends data to a server that is not running? –CMP “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.

24 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 the same socket to a new peer.