forked from operasfantom/os-net-multiplexing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
91 lines (71 loc) · 2.19 KB
/
Copy pathclient.cpp
File metadata and controls
91 lines (71 loc) · 2.19 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
#include <iostream>
#include <vector>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <cstdio>
#include <sys/epoll.h>
#include "utils.h"
using std::cin;
using std::cout;
using std::stoi;
using std::cerr;
using std::string;
using std::vector;
const int CONNECTIONS_TOTAL = 64;
void get_args(char *argv[], string &host, int &port) {
try {
port = stoi(argv[2]);
} catch (...) {
cerr << "Invalid port\n";
exit(EXIT_FAILURE);
}
host = argv[1];
}
const int BUFFER_SIZE = 42;
int main(int argc, char *argv[]) {
if (argc < 3) {
cout << "Usage: ./os-net-multiplexing-client [host] [port]";
exit(EXIT_FAILURE);
}
int port;
string host;
int ed = epoll_create1(0);
get_args(argv, host, port);
//creating socket file descriptor
int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
utils::check(fd, "in socket");
struct sockaddr_in server{};
server.sin_family = AF_INET;
server.sin_port = htons(port);
socklen_t s_len = sizeof(sockaddr_in);
utils::check(inet_pton(AF_INET, host.data(), &server.sin_addr), "in inet_pton");
int connect_result = (connect(fd, (sockaddr *) (&server), sizeof(server)) == -1 && errno != EINPROGRESS) ? -1 : 0;
utils::check(connect_result, "in connect");
struct epoll_event ee{}, ee_1{};
utils::add_epoll(ed, fd, &ee, EPOLLOUT, EPOLL_CTL_ADD);
int descriptors = epoll_wait(ed, &ee_1, 1, -1);
utils::check(descriptors, "in epoll_wait");
vector<char> msg(BUFFER_SIZE);
for (char& x: msg) {
x = 'a';
}
msg.back() = '\0';
if (!(ee.events & EPOLLOUT)) {
return EXIT_FAILURE;
}
utils::send_msg(&msg[0], BUFFER_SIZE, fd);
utils::add_epoll(ed, fd, &ee, EPOLLIN, EPOLL_CTL_MOD);
descriptors = epoll_wait(ed, &ee_1, 1, -1);
utils::check(descriptors, "in epoll_wait");
if (!(ee.events & EPOLLIN)) {
return EXIT_FAILURE;
}
utils::receive_msg(&msg[0], BUFFER_SIZE, fd);
utils::print_msg(msg);
utils::check(shutdown(fd, SHUT_RDWR), "in descriptor shutdown");
utils::check(close(fd), "in descriptor close");
return 0;
}