CN JAVA PROGRAMS – 21.10.
2024
1. Write a java program for implementing server side simple Stop and wait protocol
SERVER CODE
import [Link].*;
import [Link].*;
public class StopAndWaitServer {
private static final int PORT = 9876;
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(PORT)) {
[Link]("Server is listening on port " + PORT);
while (true) {
Socket socket = [Link]();
[Link]("Client connected");
DataInputStream in = new DataInputStream([Link]());
DataOutputStream out = new DataOutputStream([Link]());
String message = [Link]();
[Link]("Received: " + message);
String response = "Echo: " + message;
[Link](response);
[Link]("Sent: " + response);
[Link]();
[Link]("Client disconnected");
}
} catch (IOException e) {
[Link]();
}
}
}
CLIENT CODE
import [Link].*;
import [Link].*;
public class StopAndWaitClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 9876;
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT)) {
[Link]("Connected to server");
DataInputStream in = new DataInputStream([Link]());
DataOutputStream out = new DataOutputStream([Link]());
String[] messages = {"Hello", "How are you?", "Goodbye"};
for (String message : messages) {
[Link](message);
[Link]("Sent: " + message);
String response = [Link]();
[Link]("Received: " + response);
} catch (IOException e) {
[Link]();
OUTPUT :
2. Write a java program for implementing client side simple Stop and wait protocol
SERVER CODE – Same as 1st program
CLIENT CODE
import [Link].*;
import [Link].*;
public class StopAndWaitClient1 {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 9876;
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT)) {
[Link]("Connected to server");
DataInputStream in = new DataInputStream([Link]());
DataOutputStream out = new DataOutputStream([Link]());
String[] messages = {"Hello", "How are you?", "Goodbye"};
for (String message : messages) {
[Link](message);
[Link]("Sent: " + message);
String response = [Link]();
[Link]("Received: " + response);
} catch (IOException e) {
[Link]("Error: " + [Link]());
OUTPUT:
3. Write a java program for Implementing sliding window protocol using GOBACKN
SERVER CODE
import [Link].*;
import [Link].*;
import [Link];
public class GoBackNServer {
private static final int PORT = 9876;
private static final int WINDOW_SIZE = 4;
private static final int TOTAL_PACKETS = 10;
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket()) {
[Link]("Server is running...");
int nextPacketToSend = 0;
int base = 0;
while (base < TOTAL_PACKETS) {
while (nextPacketToSend < base + WINDOW_SIZE && nextPacketToSend < TOTAL_PACKETS) {
String message = "Packet " + nextPacketToSend;
byte[] buffer = [Link]();
DatagramPacket packet = new DatagramPacket(buffer, [Link],
[Link]("localhost"), PORT);
[Link](packet);
[Link]("Sent: " + message);
nextPacketToSend++;
[Link](2000);
try {
byte[] ackBuffer = new byte[1024];
DatagramPacket ackPacket = new DatagramPacket(ackBuffer, [Link]);
[Link](ackPacket);
String ackMessage = new String([Link](), 0, [Link]());
int ackNumber = [Link]([Link](" ")[1]);
if (ackNumber >= base) {
base = ackNumber + 1;
[Link]("Received ACK for Packet: " + ackNumber);
} catch (SocketTimeoutException e) {
[Link]("Timeout occurred, resending packets from: " + base);
nextPacketToSend = base;
}
}
String terminationMessage = "Transmission complete.";
byte[] buffer = [Link]();
DatagramPacket terminationPacket = new DatagramPacket(buffer, [Link],
[Link]("localhost"), PORT);
[Link](terminationPacket);
[Link](terminationMessage);
} catch (IOException e) {
[Link]();
CLIENT CODE
import [Link].*;
import [Link].*;
public class GoBackNClient {
private static final int PORT = 9876;
public static void main(String[] args) {
try (DatagramSocket socket = new DatagramSocket(PORT)) {
[Link]("Client is running...");
int expectedPacketNumber = 0;
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, [Link]);
[Link](packet);
String message = new String([Link](), 0, [Link]());
if ([Link]("Transmission complete.")) {
[Link](message);
break;
int receivedPacketNumber = [Link]([Link](" ")[1]);
if (receivedPacketNumber == expectedPacketNumber) {
[Link]("Received: " + message);
expectedPacketNumber++;
} else {
[Link]("Duplicate or out-of-order packet received: " + message);
String ackMessage = "ACK " + (expectedPacketNumber - 1);
byte[] ackBuffer = [Link]();
DatagramPacket ackPacket = new DatagramPacket(ackBuffer, [Link], [Link](),
[Link]());
[Link](ackPacket);
[Link]("Sent: " + ackMessage);
} catch (IOException e) {
[Link]();
}
}
OUTPUT:
4. Write a java program for Implement character stuffing, byte stuffing and CRC -12
import [Link];
public class DataLinkProtocol {
private static final char FLAG = 0x7D;
private static final byte ESCAPE_BYTE = (byte) 0x7D;
private static final int CRC_POLYNOMIAL = 0x80F;
public static String characterStuffing(String data) {
StringBuilder stuffedData = new StringBuilder();
[Link](FLAG);
for (char c : [Link]()) {
if (c == FLAG) {
[Link](FLAG);
}
[Link](c);
}
[Link](FLAG);
return [Link]();
}
public static byte[] byteStuffing(byte[] data) {
ByteArrayOutputStream stuffedData = new ByteArrayOutputStream();
for (byte b : data) {
if (b == ESCAPE_BYTE) {
[Link](ESCAPE_BYTE);
}
[Link](b);
}
return [Link]();
}
public static int computeCRC(byte[] data) {
int crc = 0;
for (byte b : data) {
crc ^= (b << 4);
for (int i = 0; i < 8; i++) {
if ((crc & 0x800) != 0) {
crc = (crc << 1) ^ CRC_POLYNOMIAL;
} else {
crc <<= 1;
}
}
}
return crc & 0xFFF;
}
public static void main(String[] args) {
String characterData = "Hello, this is a test message with a flag: \u007D";
byte[] byteData = {0x01, 0x02, (byte) 0x7D, 0x03, 0x04};
String stuffedCharData = characterStuffing(characterData);
[Link]("Character Stuffed Data: " + stuffedCharData);
byte[] stuffedByteData = byteStuffing(byteData);
[Link]("Byte Stuffed Data: ");
for (byte b : stuffedByteData) {
[Link]("0x%02X ", b);
}
[Link]();
int crcValue = computeCRC(byteData);
[Link]("CRC-12 Value: 0x%03X%n", crcValue);
}
}
OUTPUT:
5. Write a java program to implement Client Server communication using UDP.
SERVER CODE
import [Link].*;
import [Link].*;
public class UDPServer {
private static final int PORT = 9876;
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(PORT);
[Link]("UDP Server is running on port " + PORT);
while (true) {
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, [Link]);
[Link](receivePacket);
String message = new String([Link](), 0, [Link]());
[Link]("Received: " + message);
String responseMessage = "Echo: " + message;
byte[] sendBuffer = [Link]();
InetAddress clientAddress = [Link]();
int clientPort = [Link]();
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, [Link],
clientAddress, clientPort);
[Link](sendPacket);
[Link]("Sent: " + responseMessage);
}
} catch (IOException e) {
[Link]();
} finally {
if (socket != null && ![Link]()) {
[Link]();
}
}
}
}
CLIENT CODE
import [Link].*;
import [Link].*;
import [Link];
public class UDPClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 9876;
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("Enter message (type 'exit' to quit): ");
String message = [Link]();
byte[] sendBuffer = [Link]();
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, [Link],
[Link](SERVER_ADDRESS), SERVER_PORT);
[Link](sendPacket);
if ([Link]("exit")) {
break;
}
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, [Link]);
[Link](receivePacket);
String response = new String([Link](), 0, [Link]());
[Link]("Received from server: " + response);
}
} catch (IOException e) {
[Link]();
} finally {
if (socket != null && ![Link]()) {
[Link]();
}
}
}
}
OUTPUT:
6. Write a java program to implement ARP using UDP
SERVER CODE
import [Link];
import [Link].*;
import [Link];
public class ARPServer {
private static final int PORT = 9876;
private static final HashMap<String, String> arpTable = new HashMap<>();
static {
[Link]("[Link]", "00:1A:2B:3C:4D:5E");
[Link]("[Link]", "00:1A:2B:3C:4D:5F");
[Link]("[Link]", "00:1A:2B:3C:4D:60");
}
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(PORT);
[Link]("ARP Server is running on port " + PORT);
while (true) {
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, [Link]);
[Link](receivePacket);
String requestMessage = new String([Link](), 0, [Link]());
[Link]("Received request for IP: " + [Link]());
String ipAddress = [Link]();
String macAddress = [Link](ipAddress, "Not found");
String responseMessage = macAddress;
byte[] sendBuffer = [Link]();
InetAddress clientAddress = [Link]();
int clientPort = [Link]();
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, [Link],
clientAddress, clientPort);
[Link](sendPacket);
[Link]("Sent response: " + responseMessage);
}
} catch (IOException e) {
[Link]();
} finally {
if (socket != null && ![Link]()) {
[Link]();
}
}
}
}
CLIENT CODE
import [Link];
import [Link].*;
import [Link];
public class ARPClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 9876;
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
Scanner scanner = new Scanner([Link]);
while (true) {
[Link]("Enter IP address to resolve (type 'exit' to quit): ");
String ipAddress = [Link]();
if ([Link]("exit")) {
break;
}
byte[] sendBuffer = [Link]();
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, [Link],
[Link](SERVER_ADDRESS), SERVER_PORT);
[Link](sendPacket);
[Link]("Sent request for IP: " + ipAddress);
byte[] receiveBuffer = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, [Link]);
[Link](receivePacket);
String response = new String([Link](), 0, [Link]());
[Link]("Received MAC address: " + response);
}
} catch (IOException e) {
[Link]();
} finally {
if (socket != null && ![Link]()) {
[Link]();
}
}
}
}
OUTPUT:
7. Write a java program for a client to write a message and the server to receive it.
SERVER CODE
import [Link].*;
import [Link].*;
public class TCPServer {
private static final int PORT = 9876;
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
[Link]("Server is running on port " + PORT);
while (true) {
Socket clientSocket = [Link]();
[Link]("Client connected: " + [Link]());
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
String message = [Link]();
[Link]("Received message: " + message);
[Link]();
}
} catch (IOException e) {
[Link]();
} finally {
if (serverSocket != null && ![Link]()) {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
}
}
CLIENT CODE
import [Link].*;
import [Link].*;
import [Link];
public class TCPClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 9876;
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
[Link]("Connected to server at " + SERVER_ADDRESS + ":" + SERVER_PORT);
PrintWriter out = new PrintWriter([Link](), true);
Scanner scanner = new Scanner([Link]);
[Link]("Enter message to send: ");
String message = [Link]();
[Link](message);
[Link]("Message sent: " + message);
} catch (IOException e) {
[Link]();
} finally {
if (socket != null && ![Link]()) {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
}
}
OUTPUT:
8. Write a java program for a client that first writes to the server then server will receive and
print that text. Now server also writes to the client and client will receive and print that text.
Implement this scenario
SERVER CODE
import [Link].*;
import [Link].*;
public class TCPServer1 {
private static final int PORT = 9876;
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
[Link]("Server is running on port " + PORT);
while (true) {
Socket clientSocket = [Link]();
[Link]("Client connected: " + [Link]());
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
String clientMessage = [Link]();
[Link]("Received message from client: " + clientMessage);
String responseMessage = "Server received: " + clientMessage;
PrintWriter out = new PrintWriter([Link](), true);
[Link](responseMessage);
[Link]("Sent response to client: " + responseMessage);
[Link]();
}
} catch (IOException e) {
[Link]();
} finally {
if (serverSocket != null && ![Link]()) {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
}
}
CLIENT CODE
import [Link].*;
import [Link].*;
import [Link];
public class TCPClient1 {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 9876;
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
[Link]("Connected to server at " + SERVER_ADDRESS + ":" + SERVER_PORT);
PrintWriter out = new PrintWriter([Link](), true);
Scanner scanner = new Scanner([Link]);
[Link]("Enter message to send: ");
String message = [Link]();
[Link](message);
[Link]("Message sent: " + message);
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
String serverResponse = [Link]();
[Link]("Received response from server: " + serverResponse);
} catch (IOException e) {
[Link]();
} finally {
if (socket != null && ![Link]()) {
try {
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
}
}
OUTPUT: