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

Ad Java Prac 3

The document provides code for a program that demonstrates URL and datagram connections in Java. The URL connection code connects to Netflix and prints the response. The datagram connection code includes both client and server sides - the client sends a message to the server on port 12345, and the server echoes the message back to the client.

Uploaded by

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

Ad Java Prac 3

The document provides code for a program that demonstrates URL and datagram connections in Java. The URL connection code connects to Netflix and prints the response. The datagram connection code includes both client and server sides - the client sends a message to the server on port 12345, and the server echoes the message back to the client.

Uploaded by

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

Practical - 3

Objective: Write a code for program with URL, URL connection and datagrams connection.

Code:
URL Connection
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionExample {


public static void main(String[] args) {
try {
// Create a URL object
URL url = new URL("https://www.netflix.com/browse");

// Open a connection to the URL


URLConnection connection = url.openConnection();

// Read data from the URL


BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output:
Datagram Connection:
Client side
import java.io.*;
import java.net.*;

public class URLAndDatagramExample {


public static void main(String[] args) {
try {
// URL Connection Example (Previous code)

// Datagram Connection Example


DatagramSocket datagramSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
int serverPort = 12345;
String message = "Hello, Datagram Connection!";
byte[] sendData = message.getBytes();

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


serverPort);
datagramSocket.send(sendPacket);

byte[] receiveData = new byte[1024];


DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
datagramSocket.receive(receivePacket);

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


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

datagramSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Server side
import java.net.*;
import java.io.*;

public class DatagramServer {


public static void main(String[] args) {
try {
DatagramSocket serverSocket = new DatagramSocket(12345);

while (true) {
byte[] receiveData = new byte[1024];
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);

InetAddress clientAddress = receivePacket.getAddress();


int clientPort = receivePacket.getPort();

// Echo the received message back to the client


byte[] sendData = receivedMessage.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
clientAddress, clientPort);
serverSocket.send(sendPacket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output:

You might also like