Computer Network Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Experiment- 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();

2101330100129 Khushi pant


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();

2101330100129 Khushi pant


Output-

Server

Client

2101330100129 Khushi pant


Experiment- 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-

2101330100129 Khushi pant


Experiment- 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();
}
}
}

2101330100129 Khushi pant


2101330100129 Khushi pant

You might also like