0% found this document useful (0 votes)
240 views

Java DatagramSocket and Datagram Packet - Javatpoint

This document discusses Java DatagramSocket and DatagramPacket classes for connectionless socket programming. DatagramSocket represents a socket for sending and receiving datagram packets, which are data packets that may arrive in any order and are not guaranteed to be delivered. DatagramPacket represents a message that can be sent or received via a DatagramSocket. Constructors for DatagramSocket bind it to a port, while DatagramPacket constructors are used to send or receive packets. Code examples demonstrate using DatagramSocket and DatagramPacket to send and receive a datagram packet.

Uploaded by

Arul Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

Java DatagramSocket and Datagram Packet - Javatpoint

This document discusses Java DatagramSocket and DatagramPacket classes for connectionless socket programming. DatagramSocket represents a socket for sending and receiving datagram packets, which are data packets that may arrive in any order and are not guaranteed to be delivered. DatagramPacket represents a message that can be sent or received via a DatagramSocket. Constructors for DatagramSocket bind it to a port, while DatagramPacket constructors are used to send or receive packets. Code examples demonstrate using DatagramSocket and DatagramPacket to send and receive a datagram packet.

Uploaded by

Arul Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java DatagramSocket and Datagram Packet - javatpoint https://www.javatpoint.

com/DatagramSocket-and-DatagramPacket

1 of 4 2/28/2018, 5:41 PM
Java DatagramSocket and Datagram Packet - javatpoint https://www.javatpoint.com/DatagramSocket-and-DatagramPacket

Java DatagramSocket and DatagramPacket


Java DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.

Java DatagramSocket class


Java DatagramSocket class represents a connection-less socket for sending and
receiving datagram packets.

A datagram is basically an information but there is no guarantee of its content, arrival or


arrival time.

Commonly used Constructors of DatagramSocket class


DatagramSocket() throws SocketEeption: it creates a datagram socket and
binds it with the available Port Number on the localhost machine.

DatagramSocket(int port) throws SocketEeption: it creates a datagram socket


and binds it with the given Port Number.

DatagramSocket(int port, InetAddress address) throws SocketEeption: it


creates a datagram socket and binds it with the specified port number and host
address.

Java DatagramPacket class


Java DatagramPacket is a message that can be sent or received. If you send multiple
packet, it may arrive in any order. Additionally, packet delivery is not guaranteed.

Commonly used Constructors of DatagramPacket class


DatagramPacket(byte[] barr, int length): it creates a datagram packet. This
constructor is used to receive the packets.

DatagramPacket(byte[] barr, int length, InetAddress address, int port): it


creates a datagram packet. This constructor is used to send the packets.

Example of Sending DatagramPacket by


DatagramSocket

//DSender.java

import java.net.*;

2 of 4 2/28/2018, 5:41 PM
Java DatagramSocket and Datagram Packet - javatpoint https://www.javatpoint.com/DatagramSocket-and-DatagramPacket

public class DSender{

public static void main(String[] args) throws Exception {

DatagramSocket ds = new DatagramSocket();

String str = "Welcome java";

InetAddress ip = InetAddress.getByName("127.0.0.1");

DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);

ds.send(dp);

ds.close();

Example of Receiving DatagramPacket by


DatagramSocket

//DReceiver.java

import java.net.*;

public class DReceiver{

public static void main(String[] args) throws Exception {

DatagramSocket ds = new DatagramSocket(3000);

byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, 1024);

ds.receive(dp);

String str = new String(dp.getData(), 0, dp.getLength());

System.out.println(str);

ds.close();

download this example

← prev next →

3 of 4 2/28/2018, 5:41 PM
Java DatagramSocket and Datagram Packet - javatpoint https://www.javatpoint.com/DatagramSocket-and-DatagramPacket

Please Share

Learn Latest Tutorials

On CentOS On Mac

Framework7 Phalcon

4 of 4 2/28/2018, 5:41 PM

You might also like