-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
91 lines (75 loc) · 2.5 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
#include <iostream>
#include <string>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
class Client {
public:
Client(const std::string& server_ip, const std::string& port) {
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
throw std::runtime_error("WSAStartup failed: " + std::to_string(iResult));
}
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd == INVALID_SOCKET) {
WSACleanup();
throw std::runtime_error("Socket creation failed: " + std::to_string(WSAGetLastError()));
}
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(std::stoi(port));
server_addr.sin_addr.s_addr = inet_addr(server_ip.c_str());
if (server_addr.sin_addr.s_addr == INADDR_NONE) {
closesocket(sockfd);
WSACleanup();
throw std::runtime_error("Invalid address.");
}
iResult = connect(sockfd, (SOCKADDR*)&server_addr, sizeof(server_addr));
if (iResult == SOCKET_ERROR) {
closesocket(sockfd);
sockfd = INVALID_SOCKET;
WSACleanup();
throw std::runtime_error("Unable to connect to server!");
}
}
void run() {
std::string command;
while (true) {
std::cout << "reQ: ";
std::getline(std::cin, command);
if (command == "..out") {
break;
}
send(sockfd, command.c_str(), command.length(), 0);
char buffer[1024];
int bytes_received = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
if (bytes_received > 0) {
buffer[bytes_received] = '\0';
std::cout << "re$: " << buffer << std::endl;
} else {
std::cerr << "Connection closed by server." << std::endl;
break;
}
}
closesocket(sockfd);
}
~Client() {
closesocket(sockfd);
WSACleanup();
}
private:
SOCKET sockfd;
};
int main(int argc, char *argv[]) {
if( argc < 2 ) {
std::cout << "error Ip";
return 0;
}
try {
Client client(argv[1], "6969");
client.run();
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}