Skip to content

Commit 90ae827

Browse files
committed
add: correct version
1 parent d671852 commit 90ae827

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Tutorial 2/clientserver.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include <iostream>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
using namespace std;
5+
6+
#define c1 "CLOSED"
7+
#define c2 "SYNSENT"
8+
#define c3 "CONNECTED"
9+
#define c4 "FINWAIT"
10+
#define invalid "INVALID"
11+
#define s1 "CLOSED"
12+
#define s2 "LISTENING"
13+
#define s3 "CONNECTED"
14+
#define s4 "CLOSEWAIT"
15+
#define SYN_MAX_RETRY 3
16+
#define FIN_MAX_RETRY 3
17+
18+
class Server
19+
{
20+
public:
21+
string state;
22+
Server()
23+
{
24+
state = s1;
25+
serverState();
26+
listen();
27+
}
28+
void listen()
29+
{
30+
int listen = -1;
31+
while ( listen != 1 )
32+
{
33+
cout << "Press 1 to start Server listening:\n";
34+
cin >> listen;
35+
}
36+
serverState();
37+
state = s2;
38+
}
39+
void serverState()
40+
{
41+
cout << "Server State: " << state << endl;
42+
}
43+
int sendSyn()
44+
{
45+
return rand() % 2;
46+
}
47+
int sendFyn()
48+
{
49+
return rand() % 2;
50+
}
51+
void serverConnected()
52+
{
53+
state = s3;
54+
serverState();
55+
}
56+
void serverClosed()
57+
{
58+
state = s4;
59+
serverState();
60+
cout << "CLOSE_WAIT_TIMEOUT\n";
61+
state = s1;
62+
listen();
63+
}
64+
65+
};
66+
67+
int main()
68+
{
69+
// freopen("input.in", "r", stdin);
70+
Server svr;
71+
string cstate = c1, input;
72+
int result, in, tries;
73+
while ( true )
74+
{
75+
cout << "Client State: " << cstate << endl;
76+
77+
if ( cstate == c1 )
78+
{
79+
tries = 0;
80+
cout << "Press 1 to send SYN\n";
81+
cin >> in;
82+
if ( in == 1 )
83+
{
84+
result = svr.sendSyn();
85+
if ( result == 1 )
86+
svr.serverConnected();
87+
cstate = c3;
88+
}
89+
}
90+
else if ( cstate == c2 )
91+
{
92+
if ( result == 1 )
93+
{
94+
cstate = c3;
95+
svr.serverConnected();
96+
}
97+
else
98+
{
99+
tries++;
100+
if ( tries > SYN_MAX_RETRY )
101+
cstate = c1;
102+
else
103+
{
104+
cout << "Press 1 to send SYN\n";
105+
cin >> in;
106+
if ( in == 1 )
107+
{
108+
result = svr.sendSyn();
109+
}
110+
}
111+
}
112+
}
113+
else if ( cstate == c3 )
114+
{
115+
tries = 0;
116+
cout << "Press 1 to send FIN\n";
117+
cin >> in;
118+
if ( in == 1 )
119+
{
120+
result = svr.sendFyn();
121+
cstate = c4;
122+
}
123+
}
124+
else
125+
{
126+
if ( result == 1 )
127+
{
128+
cstate = c1;
129+
svr.serverClosed();
130+
}
131+
else
132+
{
133+
tries++;
134+
if ( tries > FIN_MAX_RETRY )
135+
cstate = c1;
136+
cout << "Press 1 to send FIN\n";
137+
cin >> in;
138+
if ( in == 1 )
139+
{
140+
result = svr.sendFyn();
141+
}
142+
}
143+
}
144+
145+
if ( cstate == invalid )
146+
{
147+
cout << "Program Exiting\n";
148+
break;
149+
}
150+
}
151+
}

Tutorial 2/input.in

Whitespace-only changes.

0 commit comments

Comments
 (0)