Repository navigation
Expand file tree
/
Copy pathclient.cpp
More file actions
59 lines (44 loc) · 1.32 KB
/
Copy pathclient.cpp
File metadata and controls
59 lines (44 loc) · 1.32 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
#include <iostream>
#include <netdb.h>
#include <sys/socket.h>
#define IP "127.0.0.1"
#define PORT "3490"
int main() {
addrinfo hints{};
addrinfo* res = nullptr;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int status = getaddrinfo(IP, PORT, &hints, &res);
if (status != 0) {
std::cerr << "getaddrinfo failed: " << gai_strerror(status) << '\n';
return 1;
}
//std::cout << "we got da fkin address info!" << '\n';
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == -1) {
std::cerr << "dumb socket not created \n";
freeaddrinfo(res);
return 1;
}
//std::cout << "socket created :> \n";
if (connect(sockfd, res->ai_addr, res->ai_addrlen) == -1) {
std::cerr << "connection failed :/ \n";
freeaddrinfo(res);
return 1;
}
std::cout << "connected to the server :> \n";
std::string msg = "hey fang :D";
send(sockfd, msg.data(), msg.size(), 0);
char buffer[1024];
int n = recv(sockfd, buffer, sizeof(buffer), 0);
if (n > 0) {
std::cout << "received: " << std::string(buffer, n) << '\n';
}
else if(n == 0) {
std::cout << "connection closed by server\n";
}
else {
std::cerr << "recv failed\n";
}
return 0;
}