This repository was archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsever.cpp
More file actions
130 lines (106 loc) · 2.15 KB
/
Copy pathsever.cpp
File metadata and controls
130 lines (106 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Christopher Bennett
* Sever Side
*/
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <unistd.h>
using namespace std;
using boost::asio::ip::tcp;
#define addCode 1
#define subCode 2
#define divCode 3
#define multCode 4
#define notValid 5
//Functions
bool checkCode(int c)
{
switch (c)
{
case addCode:
return true;
break;
case subCode:
return true;
break;
case divCode:
return true;
break;
case multCode:
return true;
break;
case notValid:
return false;
break;
}
return false;
}
float calcAns(int c, float num1, float num2)
{
float ans;
switch (c)
{
case addCode:
ans = (num1 + num2);
cout << "Pre Answer is:\t" << (ans) << "\n";
break;
case subCode:
ans = (num1 - num2);
cout << "Pre Answer is:\t" << (ans) << "\n";
break;
case divCode:
cout << "Division" << "\n";
cout << "Answer without ans var:\t" << (ans = num1 / num2) << "\n";
cout << "Pre Answer is:\t" << (ans) << "\n";
break;
case multCode:
ans = (num1 * num2);
cout << "Pre Answer is:\t" << (ans) << "\n";
break;
default:
ans = -1.112;
break;
}
return ans;
}
int main()
{
bool goAgain;
boost::asio::io_context io_context;
tcp::endpoint endpoint(tcp::v4(), 5678); //setting port
tcp::acceptor acceptor(io_context, endpoint);
tcp::iostream stream;
boost::system::error_code ec;
cout << "Waiting for client to connect\n";
acceptor.accept(stream.socket(), ec);
cout<< "Client Connected\n";
if (!ec){
while (true)
{
int code;
stream >> code;
cout << code << "\n";
if (checkCode(code)){
cout << "Code Accepted" << "\n";
}else cout << "Invalid Code" << "\n";
float num1, num2;
string ansRequest, iterationRequest;
stream << "one" << endl;
stream >> num1;
cout << num1 << "\n";
stream << "two" << endl;
stream >> num2;
cout << num2 << "\n";
float ans = calcAns(code, num1, num2);
cout << "The answer is: " << ans << "\n";
stream >> ansRequest;
if (ansRequest == "ans"){
stream << ans << endl;
}
//cout << "Ans Request: \t" << ansRequest << "\n";
if (!stream) break;
}
}
}