UDP Socket Programming in Java Example Code

In Internet the traffic is control by strict protocol.There are two types of Internet Protocol(IP)

Types of Internet Protocol(IP)

1. TCP (Transmission Control Protocol ) and
2.UDP (User or Universal  Datagram Protocol)

TCP is connection oriented – reliable protocol which transfer the data in continues streams.once a connection is established, data can be sent bidirectional. It is a two way communication.

UDP means a Datagram  is a  connection less ,independent , self contained protocol. Bulk Data  are send and receive  as packets from using UDP.

The java.net package contain three classes to help write a java program that use datagrams to send and receive packets over the network.

1 DatagramSocket :

To create the UDP Socket and to send and receive the data packet

2 DatagramPacket :

To create packet for data

3 MulticastSocket :

Datagrampacket through DatagramSocket can be broadcast to multiple recipients.

Differ type of Definition for UDP progrgam

How to make Client send message  to Server  and server receive it in packet form

write a java program  Client  send message server receive this message   using UDp transport layer

Example Program of Datagram layer client write  some message and server read it.

Source Code for  Server


Source Code for  Client


Output of client-server Program in JAVA using Datagram

First two different cmd(command prompt) window open on   screen

now we seen two different cmd window screen  on monitor now do this step

step-1   e:>javac UdpServer1.java on cmd screen-1

step-2  e:>javac UdpCLient1.java on cmd screen-1

step-3  e:>java UdpServer1on cmd screen-1

step-4  e:>java UdpCLient1 on cmd screen-2

For Better Understanding see on the below Screenshot

(Visited 1,679 times, 7 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image