0% found this document useful (0 votes)
19 views9 pages

Cyber Final

The document provides Java code for a flow control sender and receiver using sockets, where the sender sends data in chunks based on the receiver's available buffer. It also includes methods for handling triple duplicate acknowledgments in congestion control and examples of server-client interactions with message counting and acknowledgment features. Additionally, it contains sample questions and answers regarding server-client communication and threading in Java.

Uploaded by

saadgamer123546
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)
19 views9 pages

Cyber Final

The document provides Java code for a flow control sender and receiver using sockets, where the sender sends data in chunks based on the receiver's available buffer. It also includes methods for handling triple duplicate acknowledgments in congestion control and examples of server-client interactions with message counting and acknowledgment features. Additionally, it contains sample questions and answers regarding server-client communication and threading in Java.

Uploaded by

saadgamer123546
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
You are on page 1/ 9

For flow control sender

import java.io.*;

import java.net.*;

public class Sender {

public static void main(String[] args) {

try (Socket socket = new Socket("localhost", 8080);

DataOutputStream out = new DataOutputStream(socket.getOutputStream());

DataInputStream in = new DataInputStream(socket.getInputStream())) {

String dataToSend = "This is a large data message being sent in chunks.";

byte[] data = dataToSend.getBytes();

int chunkSize = 10;

int offset = 0;

while (offset < data.length) {

int availableBuffer = in.readInt();

System.out.println("Receiver available buffer: " + availableBuffer);

int bytesToSend = Math.min(availableBuffer, chunkSize);

if (bytesToSend > 0) {

out.write(data, offset, bytesToSend);

offset += bytesToSend;

System.out.println("Sent: " + new String(data, offset - bytesToSend, bytesToSend));


}

if (availableBuffer == 0) {

System.out.println("Waiting for receiver to free up buffer...");

Thread.sleep(100);

System.out.println("Data sent completely!");

} catch (Exception e) {

e.printStackTrace();

FLow Control receiver


import java.io.*;

import java.net.*;

public class Receiver {

public static void main(String[] args) {

try (ServerSocket serverSocket = new ServerSocket(8080);

Socket socket = serverSocket.accept();

DataInputStream in = new
DataInputStream(socket.getInputStream());

DataOutputStream out = new


DataOutputStream(socket.getOutputStream())) {
int bufferSize = 20;

byte[] buffer = new byte[bufferSize];

int bufferUsed = 0;

while (true) {

int availableBuffer = bufferSize - bufferUsed;

out.writeInt(availableBuffer);

if (availableBuffer > 0) {

int bytesToRead = Math.min(availableBuffer,


in.available());

int bytesRead = in.read(buffer, bufferUsed,


bytesToRead);

if (bytesRead > 0) {

bufferUsed += bytesRead;

System.out.println("Received: " + new


String(buffer, 0, bufferUsed));

Thread.sleep(200);

bufferUsed = 0;

} catch (Exception e) {

e.printStackTrace();
}

Method to Handle Triple Duplicate Acknowledgment


public void handleTripleDuplicateAck() {

System.out.println("\n\nHandling Triple Dup Ack based congestion:


cwnd value will be halved.");

ssthresh = cwnd / 2;

if (ssthresh == 0) ssthresh = 1;

cwnd = ssthresh;

retransmitPacket();

congestion = false;

Example Small Questions

Question 1: Write the portion of the server code that waits for a
client connection and prints the port numbers for the server and
client.

Answer:

java

Copy code

ServerSocket ss = new ServerSocket(5000);

System.out.println("Server is connected at port no: " +


ss.getLocalPort());
System.out.println("Waiting for the client\n");

Socket s = ss.accept();

System.out.println("Client request is accepted at port no: " +


s.getPort());

System.out.println("Server’s Communication Port: " + s.getLocalPort());

Question 2: Write the portion of the client code that sends messages to
the server until the message "stop" is entered.

Answer:

java

Copy code

DataOutputStream output = new DataOutputStream(s.getOutputStream());

BufferedReader read = new BufferedReader(new


InputStreamReader(System.in));

String str = "";

while (!str.equals("stop")) {

str = read.readLine();

output.writeUTF(str);

Question 3: What will be the output if the client sends the following
messages: "Hi", "How are you?", "stop"?

Server Output:
plaintext

Copy code

Client Says: Hi

Client Says: How are you?

Client Says: stop

Question 4: Modify the server code to count the number of messages


received before the client sends "stop."

Answer:

java

Copy code

int messageCount = 0;

while (!str.equals("stop")) {

str = input.readUTF();

messageCount++;

System.out.println("Client Says: " + str);

System.out.println("Total messages received: " + (messageCount - 1));

Question 1: Write the portion of the server code that accepts a client
connection and assigns it to a new thread.

Answer:

java
Copy code

Socket com_socket = handshake.accept();

System.out.println("A new client is connected " + com_socket);

DataOutputStream dos = new


DataOutputStream(com_socket.getOutputStream());

DataInputStream dis = new DataInputStream(com_socket.getInputStream());

Thread new_tunnel = new ClientHandler(com_socket, dis, dos);

new_tunnel.start();

Question 2: What will the server output be if a client sends the


messages "Date", "Time", and "Exit" in sequence?

Server Output:

plaintext

Copy code

Server connected at 5000

Server is connecting

Wait for the client

A new client is connected


Socket[addr=/127.0.0.1,port=XXXXX,localport=5000]

A new thread is assigning

Client Socket[addr=/127.0.0.1,port=XXXXX,localport=5000] sends Date

Client Socket[addr=/127.0.0.1,port=XXXXX,localport=5000] sends Time


Client Socket[addr=/127.0.0.1,port=XXXXX,localport=5000] sends Exit

Closing the connection

Question 3: Write the portion of the ClientHandler class that processes


requests for "Date" and "Time."

Answer:

java

Copy code

Date date = new Date();

switch (received) {

case "Date":

toreturn = fordate.format(date);

dos_tunnel.writeUTF(toreturn);

break;

case "Time":

toreturn = fortime.format(date);

dos_tunnel.writeUTF(toreturn);

break;

default:

dos_tunnel.writeUTF("Invalid input");
break;

Question 4: How would you modify the client to print an acknowledgment


after sending a message to the server?

Answer: Add the following line after dos.writeUTF(outLine) in the


client code:

java

Copy code

You might also like