Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cnlab 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

SWE2002 - Computer Networks Lab Assessment – 3

Name:hariharan.s
Regno:22mis0340

1. The finance office of VIT wishes to make the transactions more


secure. If you are a programmer how you will implement a system to
validate the login credentials obtained from the user thereby denying
access to unauthorized users using UDP sockets? The validity of the
client must be sent as the reply message to the client and displayed
it on the standard output.
Code:

server:
import java.io.*;
import java.net.*;

public class UDPServer {


public static void main(String[] args) {
DatagramSocket serverSocket = null;

try {
serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024];


byte[] sendData;

System.out.println("Server is running...");
while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);

String receivedMessage = new


String(receivePacket.getData(), 0, receivePacket.getLength());
String[] credentials = receivedMessage.split(",");

String username = credentials[0];


String password = credentials[1];

boolean isValid = validateLogin(username,


password);

if (isValid) {
sendData = "Success: Valid login".getBytes();
} else {
sendData = "Error: Invalid login".getBytes();
}

InetAddress clientAddress =
receivePacket.getAddress();
int clientPort = receivePacket.getPort();

DatagramPacket sendPacket = new


DatagramPacket(sendData, sendData.length, clientAddress,
clientPort);
serverSocket.send(sendPacket);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}

private static boolean validateLogin(String username, String


password) {

return username.equals("admin") &&


password.equals("password");
}
}

Client:

import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) {
DatagramSocket clientSocket = null;

try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in));
System.out.print("Enter username: ");
String username = userInput.readLine();
System.out.print("Enter password: ");
String password = userInput.readLine();

String message = username + "," + password;


byte[] sendData = message.getBytes();

DatagramPacket sendPacket = new


DatagramPacket(sendData, sendData.length, serverAddress,
serverPort);
clientSocket.send(sendPacket);

byte[] receiveData = new byte[1024];


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

String response = new String(receivePacket.getData(),


0, receivePacket.getLength());
System.out.println("Server response: " + response);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
}
}

Output:
2. Write a UDP program to implement an image transfer from client
to server.
Code:

Server:

import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class UDPServer {


public static void main(String[] args) {
DatagramSocket serverSocket = null;

try {
serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024 * 64]; // Adjust


buffer size as needed

System.out.println("Server is running...");

while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);

ByteArrayInputStream byteArrayInputStream = new


ByteArrayInputStream(receivePacket.getData());
BufferedImage receivedImage =
ImageIO.read(byteArrayInputStream);

File outputFile = new File("received_image.jpg");


ImageIO.write(receivedImage, "jpg", outputFile);

System.out.println("Image received and saved.");


}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}
}

Client:
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class UDPClient {


public static void main(String[] args) {
DatagramSocket clientSocket = null;

try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;

// Load image from disk


File imageFile = new File("Cat03.jpg"); // Adjust file
path as needed
BufferedImage imageToSend = ImageIO.read(imageFile);
ByteArrayOutputStream byteArrayOutputStream = new
ByteArrayOutputStream();
ImageIO.write(imageToSend, "jpg",
byteArrayOutputStream);

byte[] sendData = byteArrayOutputStream.toByteArray();

DatagramPacket sendPacket = new


DatagramPacket(sendData, sendData.length, serverAddress,
serverPort);
clientSocket.send(sendPacket);

System.out.println("Image sent to server.");


} catch (IOException e) {
e.printStackTrace();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
}
}
Output:

3. Find the physical address of a host when its logical address is


known (ARP protocol) using UDP.

Code:

Server:

import java.io.*;
import java.net.*;

public class ARPServer {


private static final String LOCAL_IP_ADDRESS =
"192.168.1.100";
private static final String LOCAL_MAC_ADDRESS =
"00:11:22:33:44:55";

public static void main(String[] args) {


DatagramSocket serverSocket = null;

try {
serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024];

System.out.println("ARP Server is running...");

while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);

String receivedData = new


String(receivePacket.getData(), 0, receivePacket.getLength());
if (receivedData.equals(LOCAL_IP_ADDRESS)) {
InetAddress clientAddress =
receivePacket.getAddress();
int clientPort = receivePacket.getPort();

byte[] sendData =
LOCAL_MAC_ADDRESS.getBytes();
DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, clientAddress,
clientPort);
serverSocket.send(sendPacket);

System.out.println("ARP Response sent to


client.");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}
}

Client:

import java.io.*;
import java.net.*;

public class ARPClient {


public static void main(String[] args) {
DatagramSocket clientSocket = null;

try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;

String ipAddress = "192.168.1.100"; // Replace with


the IP address you want to resolve

byte[] sendData = ipAddress.getBytes();


DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, serverAddress,
serverPort);
clientSocket.send(sendPacket);

byte[] receiveData = new byte[1024];


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

String macAddress = new


String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Physical Address (MAC): " +
macAddress);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
}
}

Output:

4. Implement a DNS server and client using UDP sockets. The client
should give the URL as the input and the server should send the
respective IP address.
Code:

Server:

import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;

public class DNSServer {


private static final Map<String, String> dnsMap = new
HashMap<>();

static {
// Sample DNS entries
dnsMap.put("www.vit.ac.in", "192.168.1.1");
dnsMap.put("www.test.com", "192.168.1.2");
dnsMap.put("www.example.org", "192.168.1.3");
}

public static void main(String[] args) {


DatagramSocket serverSocket = null;

try {
serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024];

System.out.println("DNS Server is running...");

while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);

String url = new String(receivePacket.getData(),


0, receivePacket.getLength());

InetAddress clientAddress =
receivePacket.getAddress();
int clientPort = receivePacket.getPort();

String ipAddress = dnsMap.getOrDefault(url, "URL


not found");

byte[] sendData = ipAddress.getBytes();


DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, clientAddress,
clientPort);
serverSocket.send(sendPacket);

System.out.println("DNS Response sent to client: "


+ ipAddress);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}
}

Client:

import java.io.*;
import java.net.*;

public class DNSClient {


public static void main(String[] args) {
DatagramSocket clientSocket = null;

try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;

BufferedReader userInput = new BufferedReader(new


InputStreamReader(System.in));
System.out.print("Enter URL: ");
String url = userInput.readLine();

byte[] sendData = url.getBytes();


DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, serverAddress,
serverPort);
clientSocket.send(sendPacket);

byte[] receiveData = new byte[1024];


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

String ipAddress = new String(receivePacket.getData(),


0, receivePacket.getLength());
System.out.println("IP Address: " + ipAddress);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
}
}

Output:
5. Implement a UDP-based socket program for the game “Guess It”.
The game is played by the client and server. The game proceeds as
follows: The server will think of a “magic number”. The magic
number is greater than zero and less than 100. Prompt for the
“magic number” from the client. The server should print out
“Higher” if the magic number is higher than the guess and “Lower”
if the magic number is less than the guess. Give 10 chances for the
client to guess the number. When the magic number has been
guessed print "Great" and then end. If the client cannot guess the
number in 10 attempts, then print the message "Better luck next
time:)" and end.

Code:

Server:

import java.io.*;
import java.net.*;
import java.util.Random;

public class GuessItServer {


public static void main(String[] args) {
DatagramSocket serverSocket = null;

try {
serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024];

Random random = new Random();


int magicNumber = random.nextInt(100) + 1;

System.out.println("Magic number generated: " +


magicNumber);
System.out.println("Guess It Server is running...");

for (int attempts = 0; attempts < 10; attempts++) {


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

String receivedData = new


String(receivePacket.getData(), 0, receivePacket.getLength());
int guess = Integer.parseInt(receivedData.trim());

if (guess == magicNumber) {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Great! You
guessed it!");
break;
} else if (guess < magicNumber) {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Higher");
} else {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Lower");
}

if (attempts == 9) {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Better luck
next time :)");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}

private static void sendResponse(DatagramSocket socket,


InetAddress address, int port, String message) throws IOException
{
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, address, port);
socket.send(sendPacket);
}
}
Client:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class GuessItClient {


public static void main(String[] args) {
DatagramSocket clientSocket = null;

try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;

Scanner scanner = new Scanner(System.in);

for (int attempts = 0; attempts < 10; attempts++) {


System.out.print("Enter your guess (between 1 and
100): ");
int guess = scanner.nextInt();

byte[] sendData =
String.valueOf(guess).getBytes();
DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, serverAddress,
serverPort);
clientSocket.send(sendPacket);

byte[] receiveData = new byte[1024];


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

String receivedMessage = new


String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Server response: " +
receivedMessage);

if (receivedMessage.startsWith("Great")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
}
}

Output:

You might also like