Experiment 10
Experiment 10
Communicatin
1. Connectionless: UDP is connectionless, meaning it does not establish a direct connection before
sending data. Each UDP packet (datagram) is independent and can be sent without prior setup.
2. Unreliable: UDP does not guarantee delivery of packets. There is no mechanism for acknowledgment
of receipt or retransmission of lost packets. Hence, UDP is termed as an unreliable protocol.
3. Low Overhead: UDP has less overhead compared to TCP because it lacks features like connection
setup, acknowledgment, and congestion control. This makes UDP faster and more efficient for
applications where timely delivery is more critical than reliability.
4. Broadcast and Multicast: UDP supports broadcasting and multicasting, allowing a packet to be sent to
multiple destinations simultaneously.
5. Usage Scenarios:
- Real-time Applications: Suitable for applications requiring low latency and where some packet loss is
acceptable, such as VoIP (Voice over IP), online gaming, and live video streaming.
- DNS (Domain Name System): UDP is used for DNS queries where quick responses are prioritized
over reliability.
- Broadcasting: Useful for sending data to all devices on a network simultaneously.
1. Connection-Oriented: TCP establishes a connection between two devices before transmitting data. It
ensures reliable, ordered, and error-checked delivery of data between applications.
2. Reliable: TCP ensures reliability through mechanisms like acknowledgment of data receipt,
retransmission of lost packets, and error detection.
3. Higher Overhead: TCP has higher overhead compared to UDP due to its connection establishment,
acknowledgment, and flow control mechanisms.
4. Ordered Delivery: TCP guarantees that data sent from one end will be received in the same order by
the other end.
5. Usage Scenarios:
- File Transfer: Suitable for applications requiring reliable and error-free data transfer, such as file
downloads/uploads and email.
- Web Browsing: TCP is used for HTTP (Hypertext Transfer Protocol) connections, ensuring that web
pages and resources are delivered correctly and in order.
- Database Access: TCP is often used for database transactions where data integrity and order are
crucial.
Comparison:
- Reliability: TCP ensures reliable data delivery with acknowledgment and retransmission mechanisms,
while UDP does not guarantee delivery or ordering of packets.
- Overhead: TCP has higher overhead due to its connection-oriented nature and additional mechanisms
for reliability and flow control. UDP, being connectionless, has lower overhead.
- Speed: UDP is generally faster than TCP because it lacks the overhead of establishing and maintaining a
connection. It is suitable for applications where speed and lower latency are critical.
- Use Cases: TCP is preferred for applications where data integrity and ordered delivery are essential,
such as file transfer and web browsing. UDP is used for real-time applications where some packet loss
can be tolerated, such as streaming media and online gaming.
Key Differences in Code:
- Connection Handling: TCP requires explicit connection setup (`ServerSocket.accept()` and `Socket`
instantiation) and teardown (`close()` method calls), whereas UDP operates on a connectionless basis
(`DatagramSocket` is standalone for both client and server).
- Data Handling: TCP uses streams (`InputStream` and `OutputStream`) for data transmission, ensuring
data integrity and ordered delivery. UDP uses packets (`DatagramPacket`) for data transmission, offering
minimal overhead but without reliability guarantees.
- Error Handling: TCP includes built-in mechanisms for error detection and recovery (e.g.,
acknowledgment, retransmission), whereas UDP leaves error handling and recovery to the application
layer if needed.
Algorithm
One-Way Communicatin:
In One-Way Communicatin, one side sends data and the other side receives data sequentially (not
simultaneously).
import java.net.*;
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String receivedMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Received from client: " + receivedMessage);
}
}
}
Client Side (Sender)
import java.net.*;