-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCNetwork.cpp
More file actions
181 lines (137 loc) · 3.78 KB
/
CNetwork.cpp
File metadata and controls
181 lines (137 loc) · 3.78 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
173
174
175
176
177
178
179
180
181
#include "CNetwork.h"
CNetwork::CNetwork() : m_sock(0)
{
CNetwork cnetwork;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
cnetwork.CreateConnect();
}
CNetwork::~CNetwork()
{
::closesocket(m_sock);
}
void CNetwork::CreateConnect()
{
WSADATA wsaData;
CNetwork cnetwork;
int iResult;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
cnetwork.ConnectFail(iResult);
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server adress and port
iResult = getaddrinfo(SERVER_NAME, DEFAULT_PORT, &hints, &result);
if (iResult != 0)
{
cnetwork.ConnectFail(iResult);
WSACleanup();
}
SOCKET ConnectSocket = INVALID_SOCKET;
// Attempt to connect to the first address returned by
// the call to getaddrinfo
ptr = result;
// Create a SOCKET for connection to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET)
{
cnetwork.ConnectFail(ConnectSocket);
WSACleanup();
}
// Connect to server
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}
// Should really try the next address returned by getaddrinfo
// if the connect call failed
// But for this simple example we just free the resources
// returned by getaddrinfo and print an error message
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET)
{
cnetwork.ConnectFail(ConnectSocket);
WSACleanup();
}
cnetwork.SendData(ConnectSocket);
}
void CNetwork::SendData(SOCKET ConnectSocket)
{
CNetwork cnetwork;
char *sendbuf = "this is a test";
int iResult;
// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR)
{
closesocket(ConnectSocket);
WSACleanup();
}
cnetwork.CloseConnect(ConnectSocket, iResult);
cnetwork.ReceiveData(ConnectSocket, iResult);
}
void CNetwork::CloseConnect(SOCKET ConnectSocket, int Result)
{
// shutdown the connection for sending since no more data will be sent
// the client can still use the ConnectSocket for receiving data
Result = shutdown(ConnectSocket, SD_SEND);
if (Result == SOCKET_ERROR)
{
closesocket(ConnectSocket);
WSACleanup();
}
if (Result == SOCKET_ERROR)
{
closesocket(ConnectSocket);
WSACleanup();
}
if (Result == INVALID_SOCKET)
{
closesocket(ConnectSocket);
WSACleanup();
}
if (ConnectSocket == INVALID_SOCKET)
{
closesocket(ConnectSocket);
WSACleanup();
}
closesocket(ConnectSocket);
WSACleanup();
}
void CNetwork::ReceiveData(SOCKET ConnectSocket, int Result)
{
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
// Receive data until the server closes the connect
do
{
Result = recv(ConnectSocket, recvbuf, recvbuflen, 0);
//if (Result > 0)
} while (Result > 0);
}
int CNetwork::CloseConnect(SOCKET ConnectSocket)
{
closesocket(ConnectSocket);
return 1;
}
int CNetwork::ConnectFail(SOCKET ConnectSocket)
{
CNetwork cnetwork;
cnetwork.CloseConnect(ConnectSocket);
return 1;
}
int CNetwork::ConnectFail(int Result)
{
CNetwork cnetwork;
WSACleanup();
return 1;
}