-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpClient.cpp
110 lines (96 loc) · 2.54 KB
/
TcpClient.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
#include "TcpClient.h"
TcpClient::TcpClient(int numberToSend, int flag): m_numberToSend(numberToSend), m_flag(flag)
{
if (WSAStartup(MAKEWORD(2, 2), &m_wsa))
std::cout<< "WSAStartup failed: "<< WSAGetLastError();
inet_pton(AF_INET, "88.212.241.115",&m_addr.sin_addr);
m_addr.sin_port = htons(2013);
m_addr.sin_family = AF_INET;
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket<=0)
std::cout << "Socket failed: " << WSAGetLastError();
if (!connect(m_socket, (SOCKADDR*)&m_addr, sizeof(m_addr)))
readWriteLoop();
}
void TcpClient::readWriteLoop()
{
bool isEnd = false;
std::string sendbuff;
switch (m_flag)
{
case 0:
{
while (!isEnd)
{
sendbuff = std::to_string(m_numberToSend) + "\n";
char buff[4]{};
std::cout << "try send " << sendbuff << '\n';
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (send(m_socket, sendbuff.c_str(), sendbuff.length(), 0) < 0)
{
this->restart();
}
sendbuff.clear();
int bytes_recv;
bool digitEnds = false;
while ((bytes_recv = recv(m_socket, buff, sizeof(buff), 0)) &&
bytes_recv != SOCKET_ERROR)
{
if (this->isdigit(buff[0]))
sendbuff.push_back(buff[0]);
//else if (!sendbuff.empty())
if (bytes_recv == 4)
digitEnds = true;
if (digitEnds)
isEnd = true;
}
if (bytes_recv <= 0 && !isEnd)
{
//std::cout << '\n' << bytes_recv << " SOCKET_ERROR \n";
this->restart();
}
}
std::cout << "On number " << m_numberToSend << " get number " << sendbuff<< '\n';
m_storage->addData(std::atoi(sendbuff.c_str()));
} break;
case 1:
{
sendbuff.append("Check ");
sendbuff.append(std::to_string(m_numberToSend));
sendbuff.append("\n");
char buff[1024]{};
std::cout << "try send " << sendbuff << '\n';
if (send(m_socket, sendbuff.c_str(), sendbuff.length(), 0) < 0)
{
this->restart();
}
sendbuff.clear();
int bytes_recv;
while ((bytes_recv = recv(m_socket, buff, sizeof(buff), 0)) &&
bytes_recv != SOCKET_ERROR)
{
std::cout << buff;
}
}
}
}
int TcpClient::isdigit(int Char)
{
if (Char >= 48 && Char<=57)
return 1;
else
return 0;
}
void TcpClient::restart()
{
shutdown(m_socket, SD_BOTH);
closesocket(m_socket);
WSACleanup();
if (WSAStartup(MAKEWORD(2, 2), &m_wsa))
std::cout << "WSAStartup failed: " << WSAGetLastError();
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket <= 0)
std::cout << "Socket failed: " << WSAGetLastError();
if (connect(m_socket, (SOCKADDR*)&m_addr, sizeof(m_addr)))
std::cout << "Connection failed: " << WSAGetLastError();
}