0
Java Network Programming
Datagram (UDP) Sockets
• Unlike TCP/IP sockets, datagram sockets are
connectionless
• Connection between client and server...
Datagram (UDP) Sockets (contd)
• The server does not create an individual Socket
object for each client
• Instead of a Ser...
UDP Server
• Process involves the following nine steps
1. Create a DatagramSocket object
DatagramSocket datagramSocket =
n...
UDP Server (Contd)
3. Create a DatagramPacket object for the
incoming datagram
• The constructor for this object requires ...
UDP Server (Contd)
4. Accept an incoming datagram
datagramSocket.receive(inPacket);
5. Retrieve the sender's address and p...
UDP Server (contd)
6. Retrieve the data from the buffer
String message = new String(inPacket.getData(),
0,inPacket.getLeng...
UDP Server (contd)
DatagramPacket outPacket =
new DatagramPacket(response.getBytes(),
response.length(),clientAddress, cli...
UDP Client
• Setting up the corresponding client requires the
eight steps listed below
1. Create a DatagramSocket object
D...
UDP Client (contd)
3. Send the datagram message
datagramSocket.send(outPacket);
4. Create a buffer for incoming datagrams
...
UDP Client (contd)
6. Accept an incoming datagram
datagramSocket.receive(inPacket);
6. Retrieve the data from the buffer
S...
Upcoming SlideShare
Loading in...5
×

Easy Steps to implement UDP Server and Client Sockets

1,344

Published on

Easy Steps to implement UDP Server and Client Sockets

in Network programming using Java

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,344
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
40
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Easy Steps to implement UDP Server and Client Sockets"

  1. 1. Java Network Programming
  2. 2. Datagram (UDP) Sockets • Unlike TCP/IP sockets, datagram sockets are connectionless • Connection between client and server is not maintained throughout the duration of the dialogue • Each datagram packet is sent as an isolated transmission • Datagram (UDP) sockets provide a faster means of transmitting data than TCP/IP sockets, but they are unreliable.
  3. 3. Datagram (UDP) Sockets (contd) • The server does not create an individual Socket object for each client • Instead of a ServerSocket object, the server creates a DatagramSocket object • As does each client when it wants to send datagram(s) to the server • DatagramPacket objects are created and sent at both ends, rather than simple Strings.
  4. 4. UDP Server • Process involves the following nine steps 1. Create a DatagramSocket object DatagramSocket datagramSocket = new DatagramSocket(1234); 2.Create a buffer for incoming datagrams byte[] buffer = new byte[256];
  5. 5. UDP Server (Contd) 3. Create a DatagramPacket object for the incoming datagram • The constructor for this object requires two arguments: • the previously-created byte array • the size of this array DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
  6. 6. UDP Server (Contd) 4. Accept an incoming datagram datagramSocket.receive(inPacket); 5. Retrieve the sender's address and port from the packet InetAddress clientAddress = inPacket.getAddress(); int clientPort = inPacket.getPort();
  7. 7. UDP Server (contd) 6. Retrieve the data from the buffer String message = new String(inPacket.getData(), 0,inPacket.getLength()); 7. Create the response datagram – Create a DatagramPacket object, using an overloaded form of the constructor thattakes four arguments: • the byte array containing the response message; • the size of the response; • the client's address; • the client's port number.
  8. 8. UDP Server (contd) DatagramPacket outPacket = new DatagramPacket(response.getBytes(), response.length(),clientAddress, clientPort); 8.Send the response datagram datagramSocket.send(outPacket); 9.Close the DatagramSocket datagramSocket.close();
  9. 9. UDP Client • Setting up the corresponding client requires the eight steps listed below 1. Create a DatagramSocket object DatagramSocket datagramSocket = new DatagramSocket(); 2. Create the outgoing datagram • This step is exactly as for step 7 of the server program DatagramPacket outPacket = new DatagramPacket(message.getBytes(), message.length(), host, PORT);
  10. 10. UDP Client (contd) 3. Send the datagram message datagramSocket.send(outPacket); 4. Create a buffer for incoming datagrams byte[] buffer = new byte[256]; 5.Create a DatagramPacket object for the incoming datagrams DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
  11. 11. UDP Client (contd) 6. Accept an incoming datagram datagramSocket.receive(inPacket); 6. Retrieve the data from the buffer String message = new String(inPacket.getData(), 0,inPacket.getLength()); 8. Close the DatagramSocket datagramSocket.close();
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×