Skip to content

Commit fd9515d

Browse files
committed
Unzipping the codes
1 parent 166634f commit fd9515d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+146592
-0
lines changed

Assignment1/.DS_Store

6 KB
Binary file not shown.
-38.2 KB
Binary file not shown.

Assignment1/Assignment1-CS16B039/GBNC.cpp

Lines changed: 498 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
all : clien server srclient srserver
2+
clien:
3+
g++ GBNC.cpp -o clien -std=c++0x -pthread
4+
server:
5+
g++ server.cpp -o server -std=c++0x
6+
srclient:
7+
g++ srclient.cpp -o srclient -std=c++0x -pthread
8+
srserver:
9+
g++ srserver.cpp -o srserver -std=c++0x
10+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
******************************************************************
2+
CS16B039
3+
S SRINIVAS SAURAB
4+
README
5+
6+
******************************************************************
7+
8+
The port number is hardcoded and thus need not be mentioned.
9+
To run the source codes, first compile them using make file.
10+
After that run the executables in pairs as follows :
11+
12+
SR : ./srclient -d -s m2 -p 12345 -n 8 -L 512 -R 10 -N 10 -W 4 -B 100
13+
./srserver -d -p 2312 -N 10 -n 8 -w 4 -B 100 -e 0.5
14+
15+
GBN : ./clien -d -s machine1 -p 12345 -l 512 -r 10 -n 10 -w 4 -b 6
16+
./server -d -p 000 -n 10 -e 0.1
17+
Note : do not exculed any field in the command line input, except for -d
18+
flag which can be removed if debugging mode is not to be activated. Also the
19+
order of these parameters must be maintained same.
20+
Binary file not shown.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Server side implementation of UDP client-server model
2+
#include <bits/stdc++.h>
3+
#include <sys/types.h>
4+
#include <sys/socket.h>
5+
#include <arpa/inet.h>
6+
#include <netinet/in.h>
7+
#include <cstdlib>
8+
9+
using namespace std;
10+
11+
#define PORT 8080
12+
#define MAXLINE 1024
13+
14+
15+
int binaryToDecimal(int n)
16+
{
17+
int num = n;
18+
int dec_value = 0;
19+
20+
// Initializing base value to 1, i.e 2^0
21+
int base = 1;
22+
23+
int temp = num;
24+
while (temp)
25+
{
26+
int last_digit = temp % 10;
27+
temp = temp/10;
28+
29+
dec_value += last_digit*base;
30+
31+
base = base*2;
32+
}
33+
34+
return dec_value;
35+
}
36+
37+
int MAX_PACKETS;
38+
float rate;
39+
// Driver code
40+
int main(int argc, char const *argv[])
41+
{
42+
bool debugflag = false;
43+
if(argv[1][0] == '-' && argv[1][1] == 'd')
44+
{
45+
cout << "ywah" << endl;
46+
debugflag = true;
47+
MAX_PACKETS = atoi(argv[5]);
48+
rate = atof(argv[7]);
49+
}
50+
else
51+
{
52+
//debugflag = true;.
53+
MAX_PACKETS = atoi(argv[4]);
54+
rate = atof(argv[6]);
55+
}
56+
57+
58+
59+
int sockfd;
60+
char buffer[MAXLINE];
61+
char *hello = "Hello from server";
62+
struct sockaddr_in servaddr, cliaddr;
63+
64+
// Creating socket file descriptor
65+
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
66+
perror("socket creation failed");
67+
exit(EXIT_FAILURE);
68+
}
69+
70+
//int MAX_PACKETS = 100;
71+
int gotit[MAX_PACKETS] = {0};
72+
73+
memset(&servaddr, 0, sizeof(servaddr));
74+
memset(&cliaddr, 0, sizeof(cliaddr));
75+
76+
// Filling server information
77+
servaddr.sin_family = AF_INET; // IPv4
78+
servaddr.sin_addr.s_addr = INADDR_ANY;
79+
servaddr.sin_port = htons(PORT);
80+
81+
// Bind the socket with the server address
82+
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
83+
sizeof(servaddr)) < 0 )
84+
{
85+
perror("bind failed");
86+
exit(EXIT_FAILURE);
87+
}
88+
89+
int windowstart = 0;
90+
//int windowsize = 4;
91+
while(true){
92+
int n;
93+
socklen_t len;
94+
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
95+
MSG_WAITALL, ( struct sockaddr *) &cliaddr,
96+
&len);
97+
buffer[n] = '\0';
98+
99+
int decval = 0;
100+
101+
cout << buffer << endl;
102+
103+
string s = buffer;
104+
105+
cout << s << endl;
106+
107+
for(int i = 0; i < 32; i++)
108+
{
109+
//cout << s[i]<< endl;
110+
if(s[i] == '\0') break;
111+
else if(s[i] == '1')
112+
decval = decval * 2 + 1;
113+
else
114+
decval = decval * 2;
115+
}
116+
117+
cout << decval << endl;
118+
string p;
119+
p = to_string(decval);
120+
printf("Client : %s\n", buffer);
121+
//&& decval < windowsize + windowstart && decval >= windowstart
122+
if(((float)(rand()%10)/10.00000000) > rate)
123+
{
124+
cout << "got the packet" <<decval <<endl;
125+
gotit[decval] = 1;
126+
127+
int x = 1;
128+
int u = -1;
129+
while(x == 1 && u < MAX_PACKETS)
130+
{
131+
u++;
132+
x = gotit[u];
133+
}
134+
// if(gotit[0] == 0) u = -1;
135+
u--;
136+
if(u < decval) gotit[decval] = 0;
137+
p = to_string(u);
138+
//cout << (float)(rand() % 10) / 10 << endl;
139+
140+
sendto(sockfd, (const char *)&p[0], p.size(),
141+
MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
142+
len);
143+
if(u == MAX_PACKETS-1)
144+
{
145+
return 0;
146+
}
147+
cout << "sent" << p << endl;
148+
149+
if(windowstart == decval - 1)
150+
{
151+
windowstart ++;
152+
}
153+
}
154+
printf("Hello message sent.\n");
155+
}
156+
157+
return 0;
158+
}

0 commit comments

Comments
 (0)