Home > UDP/IP in Java

UDP/IP in Java

Page 1
1 Netprog 2002 TCP/IP
UDP/IP in Java
Based on Java Network Programming and Distributed Computing

Page 2
2 Netprog 2002 TCP/IP
UDP Advantages
•Less overhead (no connection
•More efficient (no guaranteed delivery) •Real-time applications (no error
checking or flow-control)
•E.g., weather, time, video, audio, games
•Data reception from more than one

Page 3
3 Netprog 2002 TCP/IP
Internet Addresses
•java.net.InetAddress class
• You get an address by using static methods:
ad = InetAddress.getByName(hostname); myAddress = InetAddress.getLocalHost();

Page 4
4 Netprog 2002 TCP/IP
Printing Internet Addresses
•You get information from an
InetAddress by using methods:
ad.getHostName(); ad.getHostAddress();

Page 5
5 Netprog 2002 TCP/IP
UDP Sockets Programming
•Sending/Receiving data.
•java.net.DatagramPacket class
•Creating UDP sockets.
•Client •Server
•java.net.DatagramSocket class

Page 6
6 Netprog 2002 TCP/IP
Creating a UDP packet
// to receive data from a remote machine DatagramPacket packet = new DatagramPacket(new byte[256], 256); // to send data to a remote machine DatagramPacket packet = new DatagramPacket( new byte[128], 128, address, port );

Page 7
7 Netprog 2002 TCP/IP
Creating UDP sockets
•A UDP socket can be used both for
reading and writing packets.
•Write operations are asynchronous;
however, read operations are blocking.
•Since there is no guaranteed delivery, a
single-threaded application could stall.

Page 8
8 Netprog 2002 TCP/IP
Creating UDP Sockets
// A client datagram socket: DatagramSocket clientSocket = new DatagramSocket(); // A server datagram socket: DatagramSocket serverSocket = new DatagramSocket(port);

Page 9
9 Netprog 2002 TCP/IP
Listening for UDP Packets
// create datagram packet . . . // create datagram server socket . . . boolean finished = false; while ( ! finished ) { serverSocket.receive (packet); // process the packet } serverSocket.close();

Page 10
10 Netprog 2002 TCP/IP
Processing UDP Packets
ByteArrayInputStream bin = new ByteArrayInputStream( packet.getData() ); DataInputStream din = new DataInputStream(bin); // read the contents of the packet

Page 11
11 Netprog 2002 TCP/IP
Sending UDP Packets
// create datagram packet . . . // create datagram client socket . . . boolean finished = false; while ( ! finished ) { // write data to packet buffer clientSocket.send (packet); // see if there is more to send }

Page 12
12 Netprog 2002 TCP/IP
Sending UDP packets
•When you receive a packet, the ip and
port number of the sender are set in the DatagramPacket.
•You can use the same packet to reply,
by overwriting the data, using the method:

Page 13
13 Netprog 2002 TCP/IP
Non-blocking I/O receiving UDP packets
•You can set a time-out in milliseconds
to determine how long a read operation blocks, before throwing an exception.
•If the duration given in milliseconds is
exceeded, an exception is thrown:

Page 14
14 Netprog 2002 TCP/IP
Typical UDP client code
•Create UDP socket to contact server
(with a given hostname and service port number)
•Create UDP packet. •Call send(packet), sending request to
the server.
•Possibly call receive(packet) (if we
need a reply).

Page 15
15 Netprog 2002 TCP/IP
Typical UDP Server code
•Create UDP socket listening to a well
known port number.
•Create UDP packet buffer •Call receive(packet) to get a request,
noting the address of the client.
•Process request and send reply back
with send(packet).

Page 16
16 Netprog 2002 TCP/IP
•Debugging UDP can be difficult. •Write routines to print out addresses. •Use a debugger. •Include code that can handle
unexpected situations.

Page 17
17 Netprog 2002 TCP/IP
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 send() has already returned!
•The next call dealing with the socket could
return the error.
Search more related documents:UDP/IP in Java
Download Document:UDP/IP in Java

Set Home | Add to Favorites

All Rights Reserved Powered by Free Document Search and Download

Copyright © 2011
This site does not host pdf,doc,ppt,xls,rtf,txt files all document are the property of their respective owners. complaint#nuokui.com