This project includes two simple Java files demonstrating how to send and receive messages (quotes) over a network using UDP (User Datagram Protocol).
This file sends quotes to a specified receiver.
-
Imports:
- Network communication libraries (
DatagramSocket,DatagramPacket,InetAddress). - Utility libraries (
Arrays,List).
- Network communication libraries (
-
Main functionality:
- Defines a list of inspirational quotes.
- Creates a socket to send data (
DatagramSocket). - Targets the local computer (
localhost) on port5555. - Sends each quote from the list every 10 seconds.
This file receives and prints quotes sent by the QuoteSender.java.
-
Imports:
- Network communication libraries (
DatagramSocket,DatagramPacket).
- Network communication libraries (
-
Main functionality:
- Creates a socket (
DatagramSocket) to listen on port5555. - Continuously listens for incoming packets (quotes).
- Upon receiving a packet, it converts the received data into readable text.
- Prints the received quotes to the console.
- Creates a socket (
-
Compile the files:
javac QuoteSender.java QuoteReceiver.java
-
Open two terminal windows:
Terminal 1 (Receiver):
java QuoteReceiver
Terminal 2 (Sender):
java QuoteSender
You will see quotes being sent from the sender and printed in the receiver console every 10 seconds.
- Java Development Kit (JDK) installed on your computer.