Repository navigation
Expand file tree
/
Copy pathtcp_connection.cpp
More file actions
172 lines (144 loc) · 3.35 KB
/
Copy pathtcp_connection.cpp
File metadata and controls
172 lines (144 loc) · 3.35 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
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
162
163
164
165
166
167
168
169
170
171
172
#include <winsock2.h>
#include <iostream>
#include <tchar.h>
#include "tcp_connection.h"
#include "Debug.h"
TCPConnection::TCPConnection()
{
}
int TCPConnection::Init()
{
FILE *stream;
freopen_s(&stream, "CONOUT$", "w", stdout);
// Winsock starten
WSADATA wsa;
error_code = WSAStartup(MAKEWORD(2, 0), &wsa);
if (error_code != 0)
{
MessageBox(NULL, _T("error couldnt run WSAStartup"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
return 1;
}
DEBUG_MSG("WSAStartup" << std::endl);
// Socket erstellen
acceptSocket = socket(AF_INET, SOCK_STREAM, 0);
if (acceptSocket == INVALID_SOCKET)
{
MessageBox(NULL, _T("error couldnt create socket"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
return 1;
}
DEBUG_MSG("accepted socket" << std::endl);
// Socket binden
memset(&addr, 0, sizeof(SOCKADDR_IN));
addr.sin_family = AF_INET;
addr.sin_port = htons(2206);
addr.sin_addr.s_addr = ADDR_ANY;
error_code = ::bind(acceptSocket, (SOCKADDR*)&addr, sizeof(addr));
if (error_code == SOCKET_ERROR)
{
MessageBox(NULL, _T("error couldnt bind"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
return 1;
}
DEBUG_MSG("bound socket" << std::endl);
return 0;
}
int TCPConnection::Listen()
{
while (1)
{
// In den listen Modus
error_code = listen(acceptSocket, 10);
if (error_code == SOCKET_ERROR)
{
MessageBox(NULL, _T("Error couldnt switch to listening mode"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
return 1;
}
DEBUG_MSG("started listening" << std::endl);
// Verbindung annehmen
connectedSocket = accept(acceptSocket, NULL, NULL);
if (connectedSocket == INVALID_SOCKET)
{
MessageBox(NULL, _T("FError couldnt accept connection"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
return 1;
}
DEBUG_MSG("accepted the connection" << std::endl);
StartupTime = GetTickCount();
Running = true;
// Daten austauschen
while (error_code != SOCKET_ERROR)
{
error_code = recv(connectedSocket, text, 255, 0);
tcpmutex.lock();
content = text;
tcpmutex.unlock();
//cout <<"recv:" << content <<" " << error_code<< endl;
if (error_code == 0)
{
DEBUG_MSG("terminated" << std::endl);
MessageBox(NULL, _T("terminated"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
Running = false;
break;
}
if (error_code == SOCKET_ERROR)
{
DEBUG_MSG("error recieved a socket error" << std::endl);
MessageBox(NULL, _T("error recieved a socket error"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
Running = false;
break;
}
text[error_code] = '\0';
Sleep(1);
}
DEBUG_MSG("Socket Error" << std::endl);
MessageBox(NULL, _T("Socket Error"),
_T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
Running = false;
}
}
int TCPConnection::Exit()
{
closesocket(acceptSocket);
closesocket(connectedSocket);
WSACleanup();
return 0;
}
bool TCPConnection::ElementAt(unsigned i, unsigned j)
{
std::string copy;
bool result;
tcpmutex.lock();
if (content.length() >= j)
{
copy = content;
}
else
{
tcpmutex.unlock();
return false;
}
tcpmutex.unlock();
result = (copy.at(j) & (1 << i))!=0;
return result;
}
bool TCPConnection::IsReady()
{
if (!Running) return false;
if (GetTickCount() - 5000 < StartupTime) return false;
tcpmutex.lock();
bool viable = content.length() >= 2;
tcpmutex.unlock();
if (viable)
{
return true;
}
return false;
}
TCPConnection::~TCPConnection()
{
}