Skip to content

Commit b2fe575

Browse files
authored
Add files via upload
1 parent 07aa184 commit b2fe575

File tree

10 files changed

+718
-0
lines changed

10 files changed

+718
-0
lines changed

README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
## ✅ Network Programming
2+
3+
![Network-Programming](https://socialify.git.ci/anuj-das-10/Network-Programming/image?description=1&descriptionEditable=CSC-DSE-502-L%20-%3E%20Network%20Programming%20LAB%20(Solutions)&font=Inter&forks=1&language=1&name=1&owner=1&pattern=Signal&stargazers=1&theme=Dark)
4+
5+
6+
### 🤔Not familiar with GitHub Interface?
7+
- 🔥Visit our Website: <a href="https://gcc-x-csd.github.io/"> GCC❌CSD </a>
8+
9+
- 🔥Let's Connect! 👇
10+
<br/> <br/>
11+
<a href="https://twitter.com/CyBeRNaTiCS_">
12+
<img width="25px" src="https://www.vectorlogo.zone/logos/twitter/twitter-tile.svg" />
13+
</a>&ensp;
14+
<a href="https://www.linkedin.com/in/anuj-das-10">
15+
<img width="25px" src="https://www.vectorlogo.zone/logos/linkedin/linkedin-icon.svg" />
16+
</a>&ensp;
17+
<a href="https://github.com/anuj-das-10">
18+
<img width="25px" src="https://www.vectorlogo.zone/logos/github/github-icon.svg" />
19+
</a>&ensp;
20+
<a href="https://www.instagram.com/lord_anuj_10_/">
21+
<img width="25px" src="https://www.vectorlogo.zone/logos/instagram/instagram-icon.svg" />
22+
</a>&ensp;
23+
<a href="https://www.facebook.com/lordanuj.10/">
24+
<img width="25px" src="https://www.vectorlogo.zone/logos/facebook/facebook-official.svg" />
25+
</a>
26+
27+
***
28+
<br/>
29+
30+
## 📜Practical Questions
31+
32+
#### Q1) &ensp; Simulate Cyclic Redundancy Check (CRC) error detection algorithm for noisy channel.
33+
34+
- ###### [See Solution using Java]()
35+
#
36+
37+
38+
#### Q2) &ensp; Simulate and implement stop and wait protocol for noisy channel.
39+
40+
- ###### [See Solution using Java]()
41+
#
42+
43+
44+
#### Q3) &ensp; Simulate and implement Go-Back-N Sliding Window Protocol.
45+
46+
- ###### [See Solution using Java]()
47+
#
48+
49+
50+
#### Q4) &ensp; Simulate and implement Distance Vector Routing Algorithm.
51+
52+
- ###### [See Solution using Java]()
53+
#
54+
55+
56+
#### Q5) &ensp; Simulate and implement Dijkstra Algorithm for shortest path routing.
57+
58+
- ###### [See Solution using Java]()
59+
#
60+
61+
62+
#### Q6) &ensp; Program to find the address of the local machine.
63+
64+
- ###### [See Solution using Java]()
65+
#
66+
67+
68+
#### Q7) &ensp; Program that prints the address of www.youtube.com
69+
70+
- ###### [See Solution using Java]()
71+
#
72+
73+
74+
#### Q8) &ensp; Program that prints all the addresses of www.youtube.com
75+
76+
- ###### [See Solution using Java]()
77+
#
78+
79+
80+
#### Q9) &ensp; Write a program to implement following methods:
81+
```
82+
public String getHostName()
83+
public String getHostAddress()
84+
public byte[] getAddress()
85+
```
86+
87+
- ###### [See Solution using Java]()
88+
89+
#
90+
91+
92+
93+
<br/>
94+
<br/>
95+
<br/>
96+
<br/>
97+
98+
99+
## 🤖Join Community!
100+
<h4>
101+
- Stuck at any question?<br/>
102+
- Confusions regarding any solution provided? <br/>
103+
- Want to discuss something regarding above topics?<br/>
104+
- Want to connect with other students?
105+
</h4>
106+
107+
- ### <img width="18px" src="https://www.vectorlogo.zone/logos/reactjs/reactjs-icon.svg" alt="join"> JOIN HERE !!
108+
&ensp;&ensp; &ensp;<a href="https://discord.gg/kEUXUv4W9f">
109+
<img width="150px" src="https://www.vectorlogo.zone/logos/discordapp/discordapp-official.svg" alt="discord">
110+
</a>&ensp;
111+
112+
<br/>
113+
<br/>
114+
115+
116+
## 🔁Connect With Me!
117+
<a href="https://twitter.com/CyBeRNaTiCS_">
118+
<img width="30px" src="https://www.vectorlogo.zone/logos/twitter/twitter-tile.svg" />
119+
</a>&ensp;
120+
<a href="https://www.linkedin.com/in/anuj-das-10">
121+
<img width="30px" src="https://www.vectorlogo.zone/logos/linkedin/linkedin-icon.svg" />
122+
</a>&ensp;
123+
<a href="https://github.com/anuj-das-10">
124+
<img width="30px" src="https://www.vectorlogo.zone/logos/github/github-icon.svg" />
125+
</a>&ensp;
126+
<a href="https://www.instagram.com/lord_anuj_10_/">
127+
<img width="30px" src="https://www.vectorlogo.zone/logos/instagram/instagram-icon.svg" />
128+
</a>&ensp;
129+
<a href="https://www.facebook.com/lordanuj.10/">
130+
<img width="30px" src="https://www.vectorlogo.zone/logos/facebook/facebook-official.svg" />
131+
</a>
132+
133+
<br/>
134+
<br/>
135+
136+
<h4 align="center">Give this Repository a STAR⭐</h4>
137+
<h5 align="center">(If you find this repository helpful)
138+
<br/> Thank You!!💞
139+
<hr/>
140+
</h5>
141+
<h4 align="center">Made with 💖 by <a href="https://twitter.com/CyBeRNaTiCS_">Anuj Das</a></h4>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// 1. Simulate Cyclic Redundancy Check (CRC) Error Detection Algorithm for Noisy Channel.
2+
3+
import java.util.Scanner;
4+
5+
class CRC_ErrorDetectionAlgorithm {
6+
// It returns the Remainder (i.e., CRC Check Bits)
7+
public static String xorOperation(String modified_message, String polynomial) {
8+
int size_in_bits = 16;
9+
int shift = modified_message.length() - polynomial.length();
10+
while(shift >= 0) {
11+
modified_message = Integer.toBinaryString(Integer.parseInt(modified_message,2)^(Integer.parseInt(polynomial,2)<<shift));
12+
shift = modified_message.length() - polynomial.length();
13+
}
14+
15+
if(modified_message.length() < size_in_bits) {
16+
while(modified_message.length() != size_in_bits) {
17+
modified_message = "0" + modified_message;
18+
}
19+
}
20+
// System.out.println(modified_message);
21+
return modified_message;
22+
}
23+
24+
// It generates the encoded data/message to be transmitted (i.e., by adding CRC Check Bits to the Original Message)
25+
public static String generateCRC_CheckBits(String modified_message, String polynomial) {
26+
String remainder = xorOperation(modified_message,polynomial);
27+
remainder = remainder.substring(remainder.length() - modified_message.length());
28+
29+
int CRC_CheckBits[] = new int[modified_message.length()];
30+
for(int i = 0; i < modified_message.length(); i++) {
31+
CRC_CheckBits[i] = Character.getNumericValue(modified_message.charAt(i)) + Character.getNumericValue(remainder.charAt(i));
32+
}
33+
34+
String msgToBeTransmitted = "";
35+
for(int i = 0; i < CRC_CheckBits.length; i++) {
36+
msgToBeTransmitted = msgToBeTransmitted.concat(Integer.toString(CRC_CheckBits[i]));
37+
}
38+
// System.out.print(msgToBeTransmitted);
39+
return msgToBeTransmitted;
40+
}
41+
42+
// If CRC generator is of 'n' bits then it appends 'n-1' zeroes at the end of the original data/message and returns the modified data/message
43+
public static String modifyMessage(String message, String polynomial) {
44+
for(int i = 0; i < polynomial.length() - 1; i++) {
45+
message += "0";
46+
}
47+
return message;
48+
}
49+
50+
51+
public static void main(String[] args) {
52+
Scanner sc = new Scanner(System.in);
53+
System.out.print("Enter the Data(Message) to be Encrypted: "); // Original Message
54+
String message = sc.next(); // Test --> 1010000
55+
56+
System.out.print("Enter the CRC Generator(Polynomial): "); // CRC Generator
57+
String polynomial = sc.next(); // Test --> 1001
58+
59+
String modified_message = modifyMessage(message,polynomial);
60+
System.out.println("Modified Data(Message): " + modified_message);
61+
62+
String msgToBeTransmitted = generateCRC_CheckBits(modified_message, polynomial);
63+
System.out.println("\nData(Message) is ready to be Transmitted: " + msgToBeTransmitted);
64+
65+
System.out.print("Enter the Data(Message) to be Transmitted: ");
66+
String send_data = sc.next();
67+
68+
String checkAllZeroes = xorOperation(send_data,polynomial);
69+
70+
// It checks if the remainder contains only zeroes --> If it contains only zeros then the data/message is accepted else considered as error in Transmission
71+
for(int i = 0; i < checkAllZeroes.length(); i++) {
72+
if(checkAllZeroes.charAt(i) == '1') {
73+
System.out.println("Error in Transmission!");
74+
return;
75+
}
76+
}
77+
78+
System.out.println("No! Error in Transmission!");
79+
}
80+
}

Solutions/Q-02/StopAndWaitARQ.java

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
2+
3+
/*
4+
5+
This program simulates the stop-and-wait protocol for a noisy channel by sending messages, receiving ACKs or corrupted messages, and
6+
handling timeouts. In a real implementation, you would send the messages and receive ACKs through a network connection, and use a timer
7+
to handle timeouts. However, this program uses simulated methods to send messages, start timers, and receive messages to make it easier
8+
to test and understand the stop-and-wait protocol.
9+
10+
*/
11+
import java.util.Random;
12+
13+
class StopAndWaitARQ {
14+
// Constants for the program
15+
private static final int MESSAGE_LENGTH = 10;
16+
private static final int TIMEOUT = 1000;
17+
private static final double ERROR_RATE = 0.1;
18+
19+
// Variables for the program
20+
private int sequenceNumber;
21+
private boolean waitingForAck;
22+
private boolean corrupted;
23+
24+
public StopAndWaitARQ() {
25+
// Initialize variables
26+
sequenceNumber = 0;
27+
waitingForAck = false;
28+
corrupted = false;
29+
}
30+
31+
public void send() {
32+
// Set the waiting flag and send the message
33+
waitingForAck = true;
34+
System.out.println("Sending message with sequence number: " + sequenceNumber);
35+
sendMessage(sequenceNumber);
36+
37+
// Start the timeout timer
38+
startTimer(TIMEOUT);
39+
}
40+
41+
public void receive(int receivedSeqNum) {
42+
// If the message is corrupted, ignore it
43+
if (isCorrupted()) {
44+
return;
45+
}
46+
47+
// If we are waiting for an ACK and the received message is the ACK we were expecting,
48+
// clear the waiting flag and send the next message
49+
if (waitingForAck && receivedSeqNum == sequenceNumber) {
50+
waitingForAck = false;
51+
sequenceNumber = (sequenceNumber + 1) % 2;
52+
send();
53+
}
54+
}
55+
56+
public void timeout() {
57+
// If the timeout occurs while we are waiting for an ACK, resend the message
58+
if (waitingForAck) {
59+
send();
60+
}
61+
}
62+
63+
private void sendMessage(int seqNum) {
64+
// Simulate sending the message over the channel
65+
// In a real implementation, you would send the message through a network connection
66+
System.out.println("Simulating sending message with sequence number: " + seqNum);
67+
}
68+
69+
private void startTimer(int timeout) {
70+
// Simulate starting the timeout timer
71+
// In a real implementation, you would start a timer that would call the timeout() method
72+
// after the specified number of milliseconds
73+
System.out.println("Simulating starting timeout timer for " + timeout + "ms");
74+
}
75+
76+
private boolean isCorrupted() {
77+
// Simulate a noisy channel by randomly corrupting some of the messages
78+
Random rand = new Random();
79+
double r = rand.nextDouble();
80+
if (r < ERROR_RATE) {
81+
System.out.println("Message Corrupted!");
82+
return true;
83+
}
84+
return false;
85+
}
86+
87+
public static void main(String[] args) {
88+
StopAndWaitARQ sender = new StopAndWaitARQ();
89+
sender.send();
90+
91+
// Simulate receiving messages in a separate thread
92+
Thread receiver = new Thread(() -> {
93+
// Simulate receiving messages and sending ACKs
94+
Random rand = new Random();
95+
while (true) {
96+
try {
97+
// Sleep for a random amount of time before receiving the next message
98+
Thread.sleep(rand.nextInt(2 * TIMEOUT));
99+
100+
// Simulate receiving a message
101+
int seqNum = rand.nextInt(2);
102+
System.out.println("Received message with sequence number: " + seqNum);
103+
sender.receive(seqNum);
104+
} catch (InterruptedException e) {
105+
e.printStackTrace();
106+
}
107+
}
108+
});
109+
receiver.start();
110+
111+
// Simulate timeouts in a separate thread
112+
Thread timer = new Thread(() -> {
113+
// Simulate timeouts
114+
Random rand = new Random();
115+
while (true) {
116+
try {
117+
// Sleep for a random amount of time before the next timeout
118+
Thread.sleep(rand.nextInt(2 * TIMEOUT));
119+
120+
// Call the timeout method
121+
sender.timeout();
122+
} catch (InterruptedException e) {
123+
e.printStackTrace();
124+
}
125+
}
126+
});
127+
timer.start();
128+
129+
}
130+
}
131+
132+
133+

0 commit comments

Comments
 (0)