Computer Networks Lab(ACSE0652)
Experiment No.-9
Aim - Implementation of stop and wait protocol in any language like C++ , Java or Python.
Code-
Server:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(1234);
System.out.println("Server waiting for connection…
Socket socket = serverSocket.accept();
System.out.println("Connection established.");
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)
String message;
do {
message = in.readLine();
System.out.println("Client: " + message);
out.println("ACK");
} while (!message.equals("bye"));
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Anurag Anand 2101330100058
CSE-VI-D
Computer Networks Lab(ACSE0652)
Client:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 1234);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String message;
do {
System.out.print("Client: ");
message = userInput.readLine();
out.println(message);
String response = in.readLine();
System.out.println("Server: " + response);
} while (!message.equals("bye"));
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Anurag Anand 2101330100058
CSE-VI-D
Computer Networks Lab(ACSE0652)
Output-
Server
Client
Anurag Anand 2101330100058
CSE-VI-D
Computer Networks Lab(ACSE0652)
Experiment No.-10
Aim - Write a program in java to find the IP address of the system.
Code -
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPAddressFinder {
public static void main(String[] args) {
try {
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("IP Address of the system: " + localhost.getHostAddress());
} catch (UnknownHostException e) {
System.out.println("Unable to determine the IP address of the system.");
e.printStackTrace();
}
}
}
Output-
Anurag Anand 2101330100058
CSE-VI-D
Computer Networks Lab(ACSE0652)
Experiment No.-11
Aim - Write a program in java to find the IP address of the any site if name is given.
Code –
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPAddressFinder {
public static void main(String[] args) {
String domainName = "google.com";
try {
InetAddress ipAddress = InetAddress.getByName(domainName);
System.out.println("IP Address of " + domainName + " is: " +
ipAddress.getHostAddress());
} catch (UnknownHostException e) {
System.out.println("Unable to find IP address for " + domainName);
e.printStackTrace();
}
}
}
Output-
Anurag Anand 2101330100058
CSE-VI-D