Transcript Slide 1

UDP – User Datagram Protocol
IP provides a datagram (connectionless) transport service across the network.
UDP provides applications with an end-to-end datagram (connectionless)
service, packets send individually with full destination address, may not
arrive, may arrive out of order, e.g. used by ping, finger, etc.
TCP provides applications with a a virtual circuit (connection-oriented)
communication service across the network
Used by most applications (error free) HTTP, FTP, POP, SMTP, etc.
UDP – User Datagram Protocol
UDP provides applications with an end-to-end datagram
(connectionless) service, packets are sent individually
with full destination address, may not arrive, may arrive
out of order, e.g. used by ping, finger, etc
i.e. connectionless transmission is like posting letter
• You post a letter it may get there it may not – you don’t
know
• If you break a bit package into a number of letters there
may not arrive in the same order as you post them
A server using UDP (server. java)
A client uses a datagram to send a text message to the server which
echos it back using a datagram – both display in a JTextArea
In the server method main() (at end of file)
1.
Creates a new Server object to create the GUI and a datagram
socket
Server app = new Server();
2.
Calls the waitForPackets method to wait for a client datagram
app.waitForPackets();
Server’s constructor creates the GUI (extends JFrame) and a datagram
socket (using port 5000) for communication with the client
a)
JTextArea display for text output
b)
socket = new DatagramSocket( 5000 );
Method waitForPackets creates a DatagramPacket, waits for
the client to send a datagram and displays it
Step 1: create a DatagramPacket to hold received datagram
receivePacket = new DatagramPacket( data, data.length );
Step 2: wait for datagram packet
socket.receive( receivePacket );
Step 3: display the packet contents in the JTextField
display.append( "\nPacket received:" +
"\nFrom host: " + receivePacket.getAddress() +
"\nHost port: " + receivePacket.getPort() +
"\nLength: " + receivePacket.getLength() +
"\nContaining:\n\t" + new String( receivePacket.getData(), 0,
receivePacket.getLength() ) );
Datagram packets carry arrays of byte[] – the last two lines, calling
receivePacket.getData(), convert this to a String
Step 4: creates a new DataPacket from received datagram
sendPacket =
new DatagramPacket( receivePacket.getData(),
receivePacket.getLength(),
receivePacket.getAddress(),
receivePacket.getPort() );
Step 5: send the datagram to the client
socket.send( sendPacket );
Then waits for the next datagram
The client (client. java) waits for the user to enter text in a JTextFeild
and sends it in a datagram to the server
In the server method main() (at end of file)
1.
Creates a new Client object to create the GUI and a datagram
socket
Client app = new Client();
2.
Calls the waitForPackets method to wait for a packet from server
app.waitForPackets();
Client’s constructor creates the GUI (extends JFrame) and a
DatagramSocket to receive messages from the server
a)
b)
JTextField enter for text input to send to server
JTextArea display for text output
c)
Create DatagramSocket bound to any available port
socket = new DatagramSocket( );
Method waitForPackets creates a DatagramPacket, waits for
the client to send a datagram and displays it
Step 1: create a DatagramPacket to hold received datagram
receivePacket = new DatagramPacket( data, data.length );
Step 2: wait for datagram packet
socket.receive( receivePacket );
Step 3: display the packet contents in the JTextField
display.append( "\nPacket received:" +
"\nFrom host: " + receivePacket.getAddress() +
"\nHost port: " + receivePacket.getPort() +
"\nLength: " + receivePacket.getLength() +
"\nContaining:\n\t" + new String( receivePacket.getData(), 0,
receivePacket.getLength() ) );
Then waits for the next datagram
When text is entered in the JTextField method actionPerformed is called
Step 4: convert the String s entered into a array of byte[]
byte data[] = s.getBytes();
Step 2: create a DatagramPacket to send to the server (on localhost)
port 5000 (the client MUST know the port the server is listening
on)
sendPacket = new DatagramPacket( data, data.length,
InetAddress.getLocalHost(), 5000 );
Step 3: send the DatagramPacket
socket.send( sendPacket );