In Java or C++
Develop a simulation program to simulate an 8-port Ethernet switch. The switch initially has no knowledge about the hosts connected to each port. It learns frame addresses and stores-and-forwards the frames. The input text file "in.txt" contains information of the incoming frames, one frame per line. There are 4 pieces of data per line: frame ID, arrival port, frame source address, and frame destination address. The frames arrive at the switch in the order of which they appear in the input file. Destination address "X" indicate a broadcast frame. The output text file "out.txt" has 8 lines. Each line lists all the frames departing from each port, Here is an example:
"in.txt" contains 5 incoming frames:
F1 P2 B--A
F2 P6 D--C
F3 P5 E--B
F4 P7 F--D
F5 P6 D--X
"out.txt" should list the departing frames on each port:
P1: F1 F2 F5
P2: F2 F3 F5
P3: F1 F2 F5
P4: F1 F2 F5
P5: F1 F2 F5
P6: F1 F4
P7: F1 F2 F5
P8: F1 F2 F5
Look carefully at P6 in the example out.txt
Submit:
1. Source code.
2. Screen capture of program execution.
3. out.txt for this input file: in1.txt
F1 P1 A--G F2 P1 B--H F3 P1 C--I F4 P2 D--E F5 P2 D--F F6 P7 G--A F7 P8 H--B F8 P8 I--C F9 P4 E--D F10 P4 F--D F11 P7 G--X F12 P1 B--G F13 P2 D--G F14 P4 E--G F15 P8 H--G F16 P1 A--D F17 P1 B--E F18 P1 C--F F19 P2 D--H F20 P8 H--I
Ans)
Code to Copy:
#include <stdio.h>
#include<stdbool.h>
int main() {
//Declaration of frameID,arrivalPort,SourceAddress,destination as type of character
char frameID,arrivalPort,SourceAddress,DestinationAddress;
//Declaration of arrivalPortMap as character array
char arrivalPortMap[8];
//Declaration of content as character array
char content[8][100];
//Declaration of inputFile ,outputFile as type of file pointer
FILE inputFile, outputFile;
//Declaration of i and j as type of integer
int i, j;
//Declaration of isConnected as type of bool
bool isConnected;
//Declaration of k as type of integer and assign 0
int k = 0;
//iterate loop
while(k<8)
{
arrivalPortMap[k] = '*';
//assign 1 to content[k][0]
content[k][0] = '1';
//increment k by 1
k = k + 1;
}
//open input file
inputFile = fopen("in.txt", "r");
//iterate loop to read the lines from file
while (fscanf(inputFile, "F%c P%c %c--%c\n", & frameID, & arrivalPort, & SourceAddress, & DestinationAddress) != EOF) {
if (arrivalPortMap[arrivalPort - '1'] == '*')
arrivalPortMap[arrivalPort - '1'] = SourceAddress;
if (DestinationAddress == 'X') {
//Declaration of j as type of integer
//and assign 0
int j = 0;
//iterate loop
while(j<8)
{
content[j][content[j][0] - '0'] = frameID;
//increment content[j][0] by 1
content[j][0] = content[j][0] + 1;
//increment j by 1
j = j + 1;
}
continue;
}
//assign false to isConnected
isConnected = false;
//Declaration of m as type of integer
int m = 0;
//Iterate loop
while(m<8)
{
if (arrivalPortMap[m] == DestinationAddress) {
content[m][content[m][0] - '0'] = frameID;
//increment content[m][0] by 1
content[m][0] = content[m][0] + 1;
isConnected = true;
break;
}
//increment m by 1
m = m + 1;
}
//if isConnected
if (isConnected)
{
continue;
}
//Declaration of s as type of integer
//and assign 0
int s = 0;
//Iterate loop
while(s<8)
{
if (arrivalPortMap[s] != SourceAddress) {
content[s][content[s][0] - '0'] = frameID;
//increment content[s][0] by 1
content[s][0] = content[s][0] + 1;
}
//increment s by 1
s = s + 1;
}
}
//close inputFile
fclose(inputFile);
//open output file
outputFile = fopen("out.txt", "w");
//Iterate loop
for (i = 0; i < 8; i = i + 1) {
//write data to file
fprintf(outputFile, "P%d: ", i + 1);
//Display statement
printf("P%d: ", i + 1);
for (j = 1; j < content[i][0] - '0'; j = j + 1) {
//Display statement
printf("F%c ", content[i][j]);
//write data to file
fprintf(outputFile, "F%c ", content[i][j]);
}
//write data to file
fprintf(outputFile, "\n");
//Display statement
printf("\n");
}
//close output file
fclose(outputFile);
return 0;
}
Input file Image
Sample output
Output File imageif any doubts in above answer below screen shot here once check
if your satisfy above answer please give positive rating or?
please don't downvote
Thankyou!
Get Answers For Free
Most questions answered within 1 hours.