Hi guys,
I'm doing a program that reads from two files and write to 16 files. The idea of the program is to rout packets across a network that contains 16 nodes. I still working in the routing mechanism but I just wrote the codes for the source node and the destination nodes.
I have to read from the first file the source node (for example in the given file dest.txt node 0) and its destination IP, and from the other file I have to get the node number of the destination IP. After that, I have to create txt files for every node and write from it receives the packets.
Here is part of the first and the second file:
1) dest.txt
Destinations of Node 0 :
========================
255.148.255.62
255.148.255.75
255.148.255.82
128.65.255.68
255.64.128.153
255.64.128.61
Destinations of Node 1 :
========================
255.148.255.75
255.148.255.82
128.65.255.112
64.128.255.120
64.128.255.154
2) arp.txt
Node 0 : 255.148.255.36 00:16:76:BF:23:B2
Node 1 : 255.148.255.62 00:A6:9C:19:5A:D4
Node 2 : 255.148.255.75 00:2A:7C:23:25:D4
Node 3 : 255.148.255.82 00:8B:A9:51:3A:F5
Node 4 : 128.65.255.68 00:92:76:E4:F1:12
So far, I wrote this codes, however, when I run it, I don't get any txt output file!
Note that any destination node can receive more than one packet from different nodes.
I hope I make it clear!
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
ifstream file;
file.open ("arp.txt");
if (!file)
{
cout << "Unable to open file"; // if file doesn't exist
exit(1);
}
// read all the data in the arp file
char l;
int i;
int k;
string IPd;
string ip0, ip1, ip2, ip3, ip4 ,ip5, ip6, ip7;
string ip8, ip9, ip10, ip11, ip12, ip13, ip14, ip15;
string mac0, mac1, mac2, mac3, mac4, mac5, mac6, mac7;
string mac8, mac9, mac10, mac11, mac12, mac13, mac14, mac15;
ofstream f0, f1, f2, f3 ,f4 ,f5 ,f6 ,f7;
ofstream f8 ,f9, f10, f11, f12 ,f13, f14 ,f15;
file >> ip0 >> ip0 >> l >> ip0 >> mac0;
file >> ip1 >> ip1 >> l >> ip1 >> mac1;
file >> ip2 >> ip2 >> l >> ip2 >> mac2;
file >> ip3 >> ip3 >> l >> ip3 >> mac3;
file >> ip4 >> ip4 >> l >> ip4 >> mac4;
file >> ip5 >> ip5 >> l >> ip5 >> mac5;
file >> ip6 >> ip6 >> l >> ip6 >> mac6;
file >> ip7 >> ip7 >> l >> ip7 >> mac7;
file >> ip8 >> ip8 >> l >> ip8 >> mac8;
file >> ip9 >> ip9 >> l >> ip9 >> mac9;
file >> ip10 >> ip10 >> l >> ip10 >> mac10;
file >> ip11 >> ip11 >> l >> ip11 >> mac11;
file >> ip12 >> ip12 >> l >> ip12 >> mac12;
file >> ip13 >> ip13 >> l >> ip13 >> mac13;
file >> ip14 >> ip14 >> l >> ip14 >> mac14;
file >> ip15 >> ip15 >> l >> ip15 >> mac15;
file.close ();
ifstream dest;
dest.open ("destination.txt");
if (!file)
{
cout << "Unable to open file"; // if file doesn't exist
exit(1);
}
while (!dest.eof ())
{
next_N: dest >> IPd >> IPd >> IPd >> i;
string IPs; // find the IP address of the source node
if (i == 0){ IPs = ip0;}
else if (i == 1){ IPs = ip1; }
else if (i == 2){ IPs = ip2; }
else if (i == 3){ IPs = ip3; }
else if (i == 4){ IPs = ip4; }
else if (i == 5){ IPs = ip5; }
else if (i == 6){ IPs = ip6; }
else if (i == 7){ IPs = ip7; }
else if (i == 8){ IPs = ip8; }
else if (i == 9){ IPs = ip9; }
else if (i == 10){ IPs = ip10; }
else if (i == 11){ IPs = ip11; }
else if (i == 12){ IPs = ip12; }
else if (i == 13){ IPs = ip13; }
else if (i == 14){ IPs = ip14; }
else if (i == 15){ IPs = ip15; }
dest >> IPd >> IPd >> IPd;
// find the node number of the destination IP
here: if (IPd == ip0) {
k = 0;
f0.open ("Node0.txt");
f0 << "I received a packet from Node " << i << "\n"; // write all the packets that node0 recived and from where
}
else if (IPd == ip1) {
k = 1;
f1.open ("Node1.txt");
f1 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip2) {
k = 2;
f2.open ("Node2.txt");
f2 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip3) {
k = 3;
f3.open ("Node3.txt");
f3 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip4) {
k = 4;
f4.open ("Node4.txt");
f4 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip5) {
k = 5;
f5.open ("Node5.txt");
f5 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip6) {
k = 6;
f6.open ("Node6.txt");
f6 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip7) {
k = 7;
f7.open ("Node7.txt");
f7 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip8) {
k = 8;
f8.open ("Node8.txt");
f8 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip9) {
k = 9;
f9.open ("Node9.txt");
f9 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip10) {
k = 10;
f10.open ("Node10.txt");
f10 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip11) {
k = 11;
f11.open ("Node11.txt");
f11 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip12) {
k = 12;
f12.open ("Node12.txt");
f12 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip13) {
k = 13;
f13.open ("Node13.txt");
f13 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip14) {
k = 14;
f14.open ("Node14.txt");
f14 << "I received a packet from Node " << i << "\n";
}
else if (IPd == ip15) {
k = 15;
f15.open ("Node15.txt");
f15 << "I received a packet from Node " << i << "\n";
}
string ss ("Destinations");
file >> IPd; // read the next destination address
if ( ss.compare(IPd)!=0)
{
goto here;
}
else
{
goto next_N;
}
}
dest.close ();
f0.close();
f1.close();
f2.close();
f3.close();
f4.close();
f5.close();
f6.close();
f7.close();
f8.close();
f9.close();
f10.close();
f11.close();
f12.close();
f13.close();
f14.close();
f15.close();
return 0;
}
Thanks & If there is any way I can improve my codes please suggest it :)