Date & Time Server Using UDP: 1.0 Rationale
Date & Time Server Using UDP: 1.0 Rationale
1.0 Rationale
Explain your project and working
2.0 Aim/Benefits of the Micro-Project
Aim –
Develop Date & Time server using UDP.
Benefits –
1. Client receives date and time after every second which helps in knowing the exact date and
time.
2. Useful in future for developing any date & time related project.
3. Works on connection less strategy.
Sr.
Title of Book/Website Quantity
No.
1 Complete Java reference 1
2 https://www.geeksforgeeks.org/java-net-datagramsocket-class-java/ -
UDP − UDP stands for User Datagram Protocol, a connection-less protocol that allows for
packets of data to be transmitted between applications.
Java DatagramSocket
Datagram socket is a type of network socket which provides connection-less point for
sending and receiving packets. Every packet sent from a datagram socket is individually routed
and delivered. It can also be used for sending and receiving broadcast messages. Datagram
Sockets is the java’s mechanism for providing network communication via UDP instead of TCP.
The DatagramPacket object is the data container, while the DatagramSocket is the
mechanism used to send or receive the DatagramPackets.
It creates a datagram socket and binds it with the available Port Number on the
localhost machine.
It creates a datagram socket and binds it with the given Port Number.
It creates a datagram socket and binds it with the specified port number and host
address.
DatagramSocket Methods
5. int getPort( )
Returns the number of the port to which the socket is connected. It returns –1 if
the socket is not connected to a port.
6. boolean isBound( )
Returns true if the socket is bound to an address. Returns false otherwise.
7. boolean isConnected( )
Returns true if the socket is connected to a server. Returns false otherwise.
8. void setSoTimeout(int millis) throws SocketException
Sets the time-out period to the number of milliseconds passed in millis
9. bind()
Binds this socket to specified address and port number.
10. connect()
Connects to the specified address and port. After connecting to the remote host,
this socket can send or receive packet from this remote host only. If a connection cannot
be made to the specified remote host, the calls to send() or receive() would throw
PortUnreachable Exception.
11. disconnect()
Disconnects the socket. If the socket is not connected, then this method has no
effect.
12. getSoTimeout()
Returns the timeout parameter if specified, or 0 which indicates infinite time.
13. setSendBufferSize()
Used to set a limit to maximum size of the packet that can be sent from this
socket. It sets the SO_SNDBUF option, which is used by network implementation to set
size of underlying network buffers. Increasing the size may allow to queue the packets
before sending when send rate is high.
14. getLocalAddress()
Returns the local address to which this socket is bound.
15. getLocalSocketAddress()
Returns the address of the machine this socket is bound to, i.e. local machine
socket address.
16. getRemoteSocketAddress()
Returns the socket address (IP address+port number) to which this socket is
connected.
Java DatagramPacket
1. buf : This is the actual message that is to be delivered or received. Datagram packets receive
or send data in stuffed in a byte array. If this is used in constructor for sending the message,
than this represents the message to be delivered, when used for receiving purpose, it
represents the buffer where the received message will be stored.
3. length : It is the actual size of packet to receive. This must be less than or equal to the size
of buffer array or else there will be overflow as received message wont fit into the array.
6. SocketAddress address : The information about the address and port can be represented
wih the help of socket address. Same function as the above two combined
1. public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
DatagramPacket Methods
1. getAddress() : Returns the IP address to which this packet is sent to or from which it was
received.
2. getPort() : Returns the port to which this packet is sent to or from which it was received.
This method is specifically used on the server from getting the port of the client who sent
the request.
3. getData() : Returns the data contained in this packet as a byte array. The data starts from the
offset specified and is of length specified.
Sr. Name of
No. Resources Specifications Qty
System with
Computer 1. Windows 7 or more
1 1
System 2. Minimum storage 500 GB
3. Processor @2.93GHz core 2 duo
3 Website https://www.geeksforgeeks.org/working-udp-datagramsockets-java/amp/ -
https://www.codejava.net/java-se/networking/java-udp-client-server-program-
4 Website -
example
Server Side :
import java.net.*;
import java.util.*;
public class udpserver
{
public static void main(String args[])throws Exception
{
DatagramSocket ds=new DatagramSocket();
InetAddress ia=InetAddress.getByName("localhost");
byte[] b=new byte[1024];
while(true)
{
Thread.sleep(1000);
System.out.println("Sending.............");
Date d=new Date();
String str=d + "";
b=str.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,ia,5000);
ds.send(dp);
}
}
}
Client Side:
import java.net.*;
public class udpclient
{
public static void main(String args[])throws Exception
{
byte[] b=new byte[1024];
String str;
DatagramSocket ds=new DatagramSocket(5000);
while(true)
{
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
str=new String(dp.getData());
System.out.println(str);
}
}
}
Output :
Server Side:
Client Side: