0% found this document useful (0 votes)
36 views11 pages

Date & Time Server Using UDP: 1.0 Rationale

document
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views11 pages

Date & Time Server Using UDP: 1.0 Rationale

document
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Date & Time server using UDP

Date & Time server using UDP

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.

3.0 Course Outcomes Addressed

a) Develop Java programs using networking concepts.

1 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

4.0 Literature Review

Sr.
Title of Book/Website Quantity
No.
1 Complete Java reference 1
2 https://www.geeksforgeeks.org/java-net-datagramsocket-class-java/ -

5.0 Actual Methodology Followed

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.

2 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

Constructors of DatagramSocket class

1. DatagramSocket() throws SocketException

It creates a datagram socket and binds it with the available Port Number on the
localhost machine.

2. DatagramSocket(int port) throws SocketException

It creates a datagram socket and binds it with the given Port Number.

3. DatagramSocket(int port, InetAddress address) throws SocketException

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

DatagramSocket Methods

1. void send(DatagramPacket packet) throws IOException


The send() method sends packet to the port specified by packet. Sends a datagram
packet from this socket. It should be noted that the information about the data to be sent,
the address to which it is sent etc are all handled by packet itself.
2. void receive(DatagramPacket packet) throws IOException
The receive method waits for a packet to be received from the port specified by
packet and returns the result. It is used to receive the packet from a sender. When a
packet is successfully received, the buffer of the packet is filled with received message.
The packet also contains valuable information like the senders address and the port
number. This method waits till a packet is received.
3. InetAddress getInetAddress( )
If the socket is connected, then the address is returned. Otherwise, null is
returned.
4. int getLocalPort( )
Returns the number of the local port.

3 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

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.

4 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

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

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.

The important parameters used in constructors are:

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.

2. offset : It represents the offset into the buffer array.

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.

4. InetAddress address : This is the destination to which the message is to be delivered.

5. port : This is the port to which the message will be delivered.

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

5 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

Constructors used for Sending purpose:

1. public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)

2. public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)

3. public DatagramPacket(byte[] buf, int length, InetAddress address, int port)

4. public DatagramPacket(byte[] buf, int length, SocketAddress address)

Constructors used for Receiving purpose:

1. public DatagramPacket(byte[] buf, int offset, int length)

2. public DatagramPacket(byte[] buf, int length)

DatagramPacket Methods

1. getAddress() : Returns the IP address to which this packet is sent to or from which it was
received.

Syntax :public InetAddress getAddress()

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.

Syntax : public int getPort()

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.

Syntax : public byte[] getData()

4. getOffset() : Returns the offset specified.

Syntax : public int getOffset()

5. getLength() : Returns the length of the data to send or receive

Syntax : public int getLength()

6 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

6. setData() : Used to set the data of this packet.

Syntax : public void setData(byte[] buf,int offset,int length)

6.0 Actual Resources Used

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

2 Book Complete Java reference 1

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

7 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

7.0 Outputs of the Micro-Project

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);
}
}
}

8 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

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);
}
}
}

9 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

Output :

Server Side:

Client Side:

10 Dr.D.Y.Patil Polytechnic Kolhapur


Date & Time server using UDP

8.0 Skill Developed / Learning outcome of this Micro-Project

a) Analyzed and implemented client server programming.

b) Implemented User Datagram Protocol in java programming.

c) Analyzed User Datagram Protocol

9.0 Application of this Micro-Project

1. UDP is used to create connectionless network.

2. Used to send date and time.

3. Used as Date & Time server.

11 Dr.D.Y.Patil Polytechnic Kolhapur

You might also like