0% found this document useful (0 votes)
1 views15 pages

Java Programs & Outputs (Network)

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

Java Programs & Outputs (Network)

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

1.

ERROR DETECTION / ERROR CORRECTION USING HAMMING CODE

package abc;
import [Link];
public class Abc {
public static void main(String[] args)
{ Scanner sc = new Scanner([Link]);

// Input 7-bit data


[Link]("Input data: ");
int[] d = new int[7];
for (int i = 0; i < 7; i++)
{ d[i] = [Link]();
}

// Calculate parity bits


int[] p = new int[4];
p[0] = d[0] ^ d[1] ^ d[3] ^ d[4] ^ d[6]; // p0
p[1] = d[0] ^ d[2] ^ d[3] ^ d[5] ^ d[6]; // p1
p[2] = d[1] ^ d[2] ^ d[3]; // p2
p[3] = d[4] ^ d[5] ^ d[6]; // p3

// Build 11-bit code


int[] code = new int[11];
code[0] = p[0];
code[1] = p[1];
code[2] = d[0];
code[3] = p[2];
code[4] = d[1];
code[5] = d[2];
code[6] = d[3];
code[7] = p[3];
code[8] = d[4];
code[9] = d[5];
code[10] = d[6];

[Link]("Generated code: ");


printArray(code);

// Let user introduce an error for testing


[Link]("Enter the position to alter to check for
error detection at the receiver end: ");
int pos = [Link]();
int[] sentCode = [Link]();

if (pos >= 1 && pos <= 11)


{ sentCode[pos - 1] ^= 1;
}

[Link]("Sent code is: ");


printArray(sentCode);

// Extract bits
int[] pr = new int[4];
int[] rd = new int[7];

pr[0] = sentCode[0];
pr[1] = sentCode[1];
rd[0] = sentCode[2];
pr[2] = sentCode[3];
rd[1] = sentCode[4];
rd[2] = sentCode[5];
rd[3] = sentCode[6];
pr[3] = sentCode[7];
rd[4] = sentCode[8];
rd[5] = sentCode[9];
rd[6] = sentCode[10];

// Calculate syndrome
int[] s = new int[4];
s[0] = pr[0] ^ rd[0] ^ rd[1] ^ rd[3] ^ rd[4] ^ rd[6];
s[1] = pr[1] ^ rd[0] ^ rd[2] ^ rd[3] ^ rd[5] ^ rd[6];
s[2] = pr[2] ^ rd[1] ^ rd[2] ^ rd[3];
s[3] = pr[3] ^ rd[4] ^ rd[5] ^ rd[6];

int errorPos = s[0] * 1 + s[1] * 2 + s[2] * 4 + s[3] * 8;

if (errorPos == 0) {
[Link]("No error detected.");
} else {
[Link]("Error is at location " + errorPos);
sentCode[errorPos - 1] ^= 1;
}

[Link]("Corrected code is: ");


printArray(sentCode);

[Link]("Original data sent: ");


for (int bit : d) {
[Link](bit);
}
[Link]();
}

private static void printArray(int[] arr)


{ for (int bit : arr) {
[Link](bit);
}
[Link]();
}
}
OUTPUT:

2. a. IMPLEMENTATION OF STOP AND WAIT PROTOCOL

STOP WAIT SENDER

package stopwaitsender1;

import [Link].*;
import [Link].*;
import [Link];

public class StopWaitSender1 {


public static void main(String[] args) throws IOException
{ String packet, ack, msg = "";
int n, i = 0, sequence = 0;

ServerSocket ss = new ServerSocket(2004);


[Link]("Waiting for connection...");
Socket s = [Link]();
[Link]("Receiver connected");

Scanner br = new Scanner([Link]);


Scanner msgFromReceiver = new Scanner([Link]());
PrintStream dos = new PrintStream([Link]());

[Link]("Enter the data to send: ");


packet = [Link]();
n = [Link]();

while (i < n + 1)
{ if (i < n) {
msg = [Link](sequence);
msg = [Link]([Link](i, i + 1));
} else {
msg = "end";
[Link](msg);
break;
}
[Link](msg);
[Link]("data sent > " + msg);
[Link]("waiting for ack...");

ack = [Link]();

if ([Link]([Link](sequence)))
{ i++;
[Link]("receiver > packet received\n");
sequence = (sequence == 0) ? 1 : 0;
} else {
[Link]("Timeout. Resending data...\n");
}
}

[Link]("All data sent. Exiting.");


[Link]();
[Link]();
}
}

STOP WAIT RECEIVER

package stopwaitreceiver1;

import [Link].*;
import [Link].*;
import [Link];

public class StopWaitReceiver1 {


public static void main(String[] args) throws IOException
{ int sequence = 0;
String packet, data = "";

Socket s = new Socket("localhost", 2004);


[Link]("Connection established:");

PrintStream dos = new PrintStream([Link]());


Scanner msgFromSender = new Scanner([Link]());

[Link]("Connected");

while (true) {
packet = [Link]();
if (![Link]("end")) {
if ([Link]([Link](0, 1)) == sequence)
{ data += [Link](1);
[Link]("\nreceiver > " + packet);
[Link]([Link](sequence));
sequence = (sequence == 0) ? 1 : 0;
} else {
[Link]("\nreceiver > " + packet + "
duplicate data");
[Link]([Link]((sequence == 0) ? 1 : 0));
}
[Link]("data received = " + data);
} else {
[Link]("connection ended...");
[Link]();
break;
}
}
}
}

OUTPUT(SENDER): OUTPUT (RECEIVER):


2. b. IMPLEMENTATION OF SLIDING WINDOW PROTOCOL

SLIDING WINDOW SENDER

package swsender2;

import [Link].*;
import [Link].*;
import [Link];

public class SWsender2 {


public static void main(String[] args) throws IOException
{ int SWS = 8; // Sender Window Size
int LAR = 0; // Last Acknowledgment Received
int LFS = 0; // Last Frame Sent

ServerSocket ss = new ServerSocket(500);


[Link]("Waiting for connection...");
Socket s = [Link]();
[Link]("Receiver connected.");

Scanner fromReceiver = new Scanner([Link]());


PrintStream toReceiver = new PrintStream([Link]());
Scanner userInput = new Scanner([Link]);

while (true) {
[Link]("Type your message: ");
String message = [Link]();

if ([Link]().equalsIgnoreCase("quit"))
{ [Link](message);
[Link]("Connection closed.");
[Link](0);
}

char[] chars = [Link]();


int i = 0, j = 0;
int sent = 1;

while (i < [Link]()) {


// Sending frames within window
while (i < [Link]() && i < SWS * sent)
{ if (LFS - LAR < SWS) {
[Link](chars[i]);
LFS++;
[Link]("Sent: " + chars[i]);
i++;
}
}

// Receiving acknowledgments
while (j < [Link]() && j < SWS * sent)
{ String ack = [Link]();
[Link](ack);
LAR++;
j++;
}

sent++;
[Link]();
}
}
}
}

SLIDING WINDOW RECEIVER

package swreceiver2;

import [Link].*;
import [Link].*;
import [Link];

public class SWreceiver2 {


public static void main(String[] args) throws IOException
{ int RWS = 8; // Receiver Window Size
int LAF = 0; // Largest Acceptable Frame
int LFR = 0; // Last Frame Received

InetAddress host = [Link]();


Socket s = new Socket(host, 500);

Scanner fromSender = new Scanner([Link]());


PrintStream toSender = new PrintStream([Link]());

[Link]("Connected to sender.");

while (true) {
int i = 0;

while (i < RWS) {


if (LAF - LFR <= RWS) {
String t = [Link]();
if ([Link]("quit")) {
[Link]("Sender ended the connection.");
[Link](0);
}

[Link]("Received: " + t);


LFR++;
[Link]("Acknowledgment for " + t);
LAF++;
}
i++;
}
[Link]();
}
}
}

OUTPUT(SENDER): OUTPUT(RECEIVER):

4. a. SOCKET PROGRAM FOR ECHO CLIENT AND ECHO SERVER COMMANDS

ECHO CLIENT

package echoclient2;

import [Link].*;
import [Link].*;
import [Link];

public class Echoclient2 {


public static void main(String[] args) throws IOException
{ InetAddress obj = [Link]();
Socket s = new Socket(obj, 500);

Scanner msgFromServer = new Scanner([Link]());


PrintStream p = new PrintStream([Link]());
Scanner input = new Scanner([Link]);

[Link]("TYPE YOUR MESSAGE TO SERVER AND TYPE


'quit' TO EXIT");

while (true)
{ [Link]("You: ");
String t = [Link]();

[Link](t);

if ([Link]("quit")) {
break;
}

String response = [Link]();


[Link]("Server echoed: " + response);
}

[Link]();
[Link]("Client closed");
}
}

ECHO SERVER

package echoserver2;

import [Link].*;
import [Link].*;
import [Link];

public class Echoserver2 {


public static void main(String[] args) throws IOException
{ ServerSocket ss = new ServerSocket(500);
Socket s = [Link]();
[Link]("Server is ready");

Scanner msgFromClient = new Scanner([Link]());


PrintStream p = new PrintStream([Link]());

while (true) {
if (![Link]()) break;

String t = [Link]();
[Link]("Received: " + t);

if ([Link]("quit"))
{ break;
}

[Link](t); // Echo the message back


}

[Link]();
[Link]();
[Link]("Server closed");
}
}
OUTPUT(SERVER): OUTPUT(CLIENT):

4. b. SOCKET PROGRAM FOR PING AND TRACE-ROUTE COMMANDS

PING SERVER

package pingserver1;

import [Link].*;
import [Link];

public class Pingserver1 {


public static void main(String[] args)
{ try {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the IP Address to be pinged: ");
String ip = [Link]();

Runtime runtime = [Link]();


Process process = [Link]("ping " + ip);
Scanner result = new Scanner([Link]());

while ([Link]())
{ [Link]([Link]());
}

} catch (Exception e) {
[Link]("Error: " + [Link]());
}
}
}

TCP CLIENT

package tcp2client1;

import [Link].*;
import [Link].*;
import [Link];

public class Tcp2client1 {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8001);
Socket socket = [Link]();

PrintStream out = new PrintStream([Link]());


Scanner inFromClient = new Scanner([Link]());
Scanner input = new Scanner([Link]);

while (true) {
[Link]("Enter the message to send: ");
String message = [Link]();
[Link](message);

if ([Link]("end"))
{ [Link]();
[Link]();
break;
}

String response = [Link]();


[Link]("Message received: " + response);

if ([Link]("end"))
{ [Link]();
[Link]();
break;
}
}
}
}

OUTPUT:
5. ENCRYPTION AND DECRYPTION

package rc4cipher;

import [Link];

public class RC4Cipher {

private static final int STATE_ARRAY_LENGTH = 256;

// Key Scheduling Algorithm (KSA)


public static int[] ksa(String key) {
int[] state = new int[STATE_ARRAY_LENGTH];
int[] keyTable = new int[STATE_ARRAY_LENGTH];

// Fill state and key table


for (int i = 0; i < STATE_ARRAY_LENGTH; i++) {
state[i] = i;
keyTable[i] = [Link](i % [Link]());
}

int j = 0;
for (int i = 0; i < STATE_ARRAY_LENGTH; i++) {
j = (j + state[i] + keyTable[i]) % STATE_ARRAY_LENGTH;
// Swap
int temp = state[i];
state[i] = state[j];
state[j] = temp;
}

return state;
}

// Pseudo-Random Generation Algorithm (PRGA)


public static byte[] rc4EncryptDecrypt(byte[] input, int[] state)
{ int i = 0, j = 0;
int[] s = [Link]();
byte[] output = new byte[[Link]];

for (int k = 0; k < [Link]; k++) {


i = (i + 1) % STATE_ARRAY_LENGTH;
j = (j + s[i]) % STATE_ARRAY_LENGTH;

// Swap
int temp = s[i];
s[i] = s[j];
s[j] = temp;

int t = (s[i] + s[j]) % STATE_ARRAY_LENGTH;


int keystreamByte = s[t];
output[k] = (byte) (input[k] ^ keystreamByte);
}
return output;
}

public static void main(String[] args)


{ Scanner scanner = new
Scanner([Link]);

[Link]("Enter key (max 5 characters): ");


String key = [Link]();
if ([Link]() > 5) {
key = [Link](0, 5); // Restrict to 5 chars
}

[Link]("Enter plaintext: ");


String plainText = [Link]();
byte[] plainBytes = [Link]();

int[] stateTable = ksa(key);

// Encrypt
byte[] encrypted = rc4EncryptDecrypt(plainBytes, stateTable);
[Link]("Encrypted string: ");
for (byte b : encrypted)
{ [Link]((char) b + " ");
}
[Link]();

// Decrypt
byte[] decrypted = rc4EncryptDecrypt(encrypted, stateTable);
[Link]("Decrypted string: ");
for (byte b : decrypted)
{ [Link]((char) b);
}
[Link]();

[Link]();
}
}

OUTPUT:
3. STUDY OF SOCKET PROGRAMMING (CLIENT-SERVER MODEL)

TCP SERVER

package tcp1server1;

import [Link].*;
import [Link].*;
import [Link];

public class Tcp1server1 {


public static void main(String[] args) throws IOException
{ ServerSocket ss = new ServerSocket(8000);
[Link]("Server started. Waiting for client...");

Socket s = [Link]();
[Link]("Client connected!");

Scanner in = new Scanner([Link]);


PrintStream dos = new PrintStream([Link]());

while (true) {
[Link]("Enter message to send: ");
String str = [Link]();
[Link](str);

if ([Link]("end"))
{ [Link]();
break;
}
}
}
}

TCP CLIENT

package tcp1client1;

import [Link].*;
import [Link].*;
import [Link];

public class Tcp1client1 {


public static void main(String[] args) throws IOException
{ Socket s = new Socket("localhost", 8000);

Scanner in = new Scanner([Link]());

while (true) {
String str = [Link]();
[Link]("Message Received: " + str);

if ([Link]("end")) {
[Link]();
break;
}
}
}
}

OUTPUT(SERVER):

OUTPUT(CLIENT):

You might also like