0% found this document useful (0 votes)
4 views

Assignment 6

The document outlines the implementation of a socket program using UDP for both server and client-side programming. It explains the characteristics of UDP, the steps involved in creating a UDP server and client, and provides example code for both. The conclusion emphasizes UDP's suitability for real-time applications due to its connectionless and lightweight nature, despite its lack of reliability.

Uploaded by

hyperlink215
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 6

The document outlines the implementation of a socket program using UDP for both server and client-side programming. It explains the characteristics of UDP, the steps involved in creating a UDP server and client, and provides example code for both. The conclusion emphasizes UDP's suitability for real-time applications due to its connectionless and lightweight nature, despite its lack of reliability.

Uploaded by

hyperlink215
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT 6:

AIM:WRITE A SOCKET PROGRAM USING UDP.

SERVER SIDE PROGRAMMING:

THEORY:

Server-Side Programming Using UDP (User Datagram Protocol)

1. Introduction to UDP

UDP (User Datagram Protocol) is a connectionless, lightweight transport layer protocol in the
TCP/IP suite. Unlike TCP, UDP does not establish a connection before data transmission and
does not provide error correction or retransmission. It is widely used for real-time
applications such as video streaming, VoIP, and online gaming.

2. Features of UDP

 Connectionless: No prior connection establishment is needed.

 Low Overhead: Faster than TCP since it lacks handshaking and error control.

 Unreliable: No guarantee of delivery, order, or duplicate prevention.

 Supports Broadcast & Multicast: Suitable for one-to-many communication.

 Message-Oriented: Preserves data boundaries, unlike TCP.

3. Working of UDP in Server-Side Programming

UDP-based server-side programming involves a simple request-response model. The server


listens for incoming datagrams from clients and responds accordingly.

4. Steps in UDP Server-Side Programming

Step 1: Create a UDP Socket

 Use the socket() function to create a UDP socket.

Step 2: Bind the Socket

 Bind the socket to an IP address and port using the bind() function.

Step 3: Receive Data

 Use the recvfrom() function to receive datagrams from clients.

Step 4: Process the Data

 Process the received data as per the application’s requirements.

Step 5: Send Response


 Use the sendto() function to send a response to the client.

Step 6: Close the Socket

 Close the socket when the server is shut down.

CODE:

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

public class UDPServerSide {

public static void main(String[] args) {

DatagramSocket serverSocket = null;

try {

serverSocket = new DatagramSocket(9876);

System.out.println("Server is listening on port 9876...");

while (true) {

byte[] receiveData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveData,


receiveData.length);

serverSocket.receive(receivePacket);

String message = new String(receivePacket.getData(), 0, receivePacket.getLength());

System.out.println("Received message: " + message);

InetAddress clientAddress = receivePacket.getAddress();

int clientPort = receivePacket.getPort();


String responseMessage = "Hello from the server!";

byte[] sendData = responseMessage.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,


clientAddress, clientPort);

serverSocket.send(sendPacket);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (serverSocket != null && !serverSocket.isClosed()) {

serverSocket.close();

OUTPUT:

CLIENT SIDE PROGRAMMING:

THEORY:

Client-Side Programming Using UDP (User Datagram Protocol)

1. Introduction to UDP
UDP (User Datagram Protocol) is a lightweight, connectionless transport layer protocol used
for fast and efficient data transfer. Unlike TCP, UDP does not establish a connection before
data transmission, making it ideal for real-time applications like video streaming, VoIP, and
online gaming.

2. Features of UDP

 Connectionless Communication: No handshake before data transfer.

 Fast and Efficient: Minimal overhead, making it suitable for real-time applications.

 Unreliable Transport: No guarantee of message delivery or order.

 Message-Oriented: Maintains message boundaries.

 Supports Broadcasting & Multicasting: Allows communication with multiple devices.

3. Working of UDP in Client-Side Programming

A UDP client sends messages (datagrams) to a server without establishing a connection. The
client does not wait for acknowledgment and does not retry if data is lost.

4. Steps in UDP Client-Side Programming

Step 1: Create a UDP Socket

 Use the socket() function to create a UDP socket.

Step 2: Prepare the Data

 Encode and format the data for transmission.

Step 3: Send Data to Server

 Use the sendto() function to send a datagram to the server.

Step 4: Receive Response

 Use the recvfrom() function to receive a response from the server.

Step 5: Process the Response

 Decode and process the received message.

Step 6: Close the Socket

 Close the socket when communication is complete.

CODE:

import java.net.DatagramPacket;

import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClientSide {

public static void main(String[] args) {

DatagramSocket clientSocket = null;

try {

clientSocket = new DatagramSocket();

InetAddress serverAddress = InetAddress.getByName("localhost");

String message = "Hello from the client!";

byte[] sendData = message.getBytes();

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,


serverAddress, 9876);

clientSocket.send(sendPacket);

byte[] receiveData = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveData,


receiveData.length);

clientSocket.receive(receivePacket);

String responseMessage = new String(receivePacket.getData(), 0,


receivePacket.getLength());

System.out.println("Received response from server: " + responseMessage);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (clientSocket != null && !clientSocket.isClosed()) {


clientSocket.close();

OUTPUT:

CONCLUSION:

UDP (User Datagram Protocol) is a connectionless, fast, and lightweight transport layer
protocol used in socket programming for real-time and high-speed applications. Unlike TCP,
UDP does not guarantee reliability, sequencing, or error correction, making it suitable for
scenarios where speed is prioritized over accuracy.
LO MAPPING:

LO 4 ACHIEVED.

You might also like