-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClient.cpp
161 lines (156 loc) · 4.52 KB
/
Client.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <sys/socket.h>
const unsigned int BUFFER_SIZE = 1024, DEFAULT_PORT = 8080, MAX_EVENTS = 64;
int getNumericValue(std::string value)
{
bool error = 0;
int result = 0;
for (char c : value) {
if (c < 48 || c > 57)
{
error = 1;
break;
}
result *= 10;
result += c - 48;
}
if (error)
{
std::cout << "Wrong input: expected correct digit, not " << value << '\n';
exit(0);
}
return result;
}
void closeAll(int epoll, int curSocket)
{
if (close(epoll) == -1)
{
std::cout << "Error occurred while closing the epoll\n";
exit(-1);
}
if (close(curSocket) == -1)
{
std::cout << "Error occurred while closing the socket\n";
exit(-1);
}
}
int main(int argc, char* argv[], char *envp[])
{
if (argc > 2)
{
std::cout << "Wrong input: expected port or no arguments\n";
return 0;
}
int curSocket = socket(AF_INET, SOCK_STREAM, 0);
if (curSocket == -1)
{
std::cout << "Unable to create a socket\n";
return -1;
}
int epoll = epoll_create1(0);
if (epoll == -1)
{
std::cout << "Unable to create epoll\n";
if (close(curSocket) == -1)
{
std::cout << "Error occurred while closing the socket\n";
return -1;
}
return -1;
}
int port = DEFAULT_PORT;
if (argc == 2)
{
port = getNumericValue(argv[1]);
}
struct epoll_event curClient, clients[MAX_EVENTS];
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(curSocket, (sockaddr*) &address, sizeof(address)) == -1)
{
std::cout << "Unable to connect client socket\n";
closeAll(epoll, curSocket);
return -1;
}
curClient.events = EPOLLIN;
curClient.data.fd = curSocket;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, curSocket, &curClient) == -1)
{
std::cout << "Unable to add current client to epoll\n";
closeAll(epoll, curSocket);
return -1;
}
curClient.data.fd = 0;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, 0, &curClient) == -1)
{
std::cout << "Unable to add current client to epoll\n";
closeAll(epoll, curSocket);
return -1;
}
char response[BUFFER_SIZE] = {};
int messageSize = 0;
std::cout << "Connected. Type in the message\n";
while (true)
{
if (feof(stdin))
{
std::cout << '\n';
closeAll(epoll, curSocket);
return -1;
}
int clientsAmount = epoll_wait(epoll, clients, MAX_EVENTS, -1);
if (clientsAmount == -1)
{
std::cout << "Error occurred during epoll waiting\n";
closeAll(epoll, curSocket);
return -1;
}
for (int i = 0; i < clientsAmount; ++i)
{
if (clients[i].data.fd == curSocket)
{
int requestSize = 0;
std::cout << "Response is: ";
while (requestSize < messageSize)
{
int received = recv(curSocket, response, BUFFER_SIZE, 0);
if (received == -1 || received == 0)
{
std::cout << "Error occurred while receiving a message\n";
closeAll(epoll, curSocket);
return -1;
}
requestSize += received;
for (int i = 0; i < received; ++i)
{
std::cout << response[i];
}
}
std::cout << '\n';
} else {
std::cin >> response;
messageSize = strlen(response);
int responseSize = 0;
while (responseSize < messageSize)
{
int sended = send(curSocket, response + responseSize, messageSize - responseSize, 0);
if (sended == -1)
{
std::cout << "Error occurred on sending the message\n";
closeAll(epoll, curSocket);
return -1;
}
responseSize += sended;
}
}
}
}
closeAll(epoll, curSocket);
return 0;
}