-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsimpletcpserver.cpp
More file actions
277 lines (237 loc) · 7.53 KB
/
Copy pathsimpletcpserver.cpp
File metadata and controls
277 lines (237 loc) · 7.53 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
Copyright (C) 2014 kaytat
Originally derived from the example posted here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms737593%28v=vs.85%29.aspx
Since there is no explict license, the Microsoft LPL applies:
http://msdn.microsoft.com/en-us/cc300389.aspx
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "simpletcpserver.h"
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
int SimpleTcpServer::setup()
{
WSADATA wsaData;
int iResult;
struct addrinfo *result = NULL;
struct addrinfo hints;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (listenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind( listenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(listenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(listenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
dumpLocalIp();
return 0;
}
int SimpleTcpServer::waitForClient()
{
printf("%s ...\n", __FUNCTION__);
// Accept a client socket
clientSocket = accept(listenSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
// No longer need server socket
closesocket(listenSocket);
listenSocket = INVALID_SOCKET;
// Dump socket info
struct sockaddr peerInfo;
int peerInfoSize = sizeof(peerInfo);
int ret = getpeername(clientSocket, &peerInfo, &peerInfoSize);
if (ret == 0) {
WCHAR addrStr[256];
DWORD addrStrSize = sizeof(addrStr);
ret = WSAAddressToString(
&peerInfo,
peerInfoSize,
NULL,
addrStr,
&addrStrSize);
if (ret == 0) {
printf("Peer addr: %ls\n", addrStr);
}
else {
printf("Error: WSAAddressToString %d\n", WSAGetLastError());
}
}
else {
printf("Error: getpeername %d\n", WSAGetLastError());
}
return 0;
}
#define PI 3.14159265
// A utility to generate a sine wave. For testing.
short getSineSample()
{
static int sineCounter = 0;
double rad, result;
rad = sineCounter++;
// Assuming an output rate of 48khz, this should
// generate a 1khz sine tone
rad = fmod(rad, 48.0);
rad = rad * 2.0 * PI;
result = sin(rad);
// sin's range is [-1,+1] scale that to something close to
// the range of a short.
return (short)(result * 32000.0);
}
int SimpleTcpServer::sendData(const char* buf, int length)
{
int iSendResult;
short* bufferToSend;
short* tmpBuffer;
int samplesToSend = 0;
int numCaptureSamples = length / 2;
const short* captureSamples = (const short*)buf;
// Super-simple stereo to mono conversion by averaging the left and right sample
if (mono || sampleRateDivisor != 1) {
if (length > bufferSize) {
printf("Error: capture buffer too large: %d %d\n", length, bufferSize);
return 0;
}
bufferToSend = (short*)scratchBuffer;
tmpBuffer = (short*)scratchBuffer;
int s1, s2;
while (numCaptureSamples > 0) {
numCaptureSamples -= 2;
// TODO: eliminate all these 'if's in loop
if (divisorCounter == 0) {
if (mono) {
s1 = (*captureSamples++);
s2 = (*captureSamples++);
// Average left and right. Assume 16 bits per sample, stereo
*tmpBuffer++ = (short)((s1 + s2) / 2);
samplesToSend++;
}
else {
*tmpBuffer++ = *captureSamples++;
*tmpBuffer++ = *captureSamples++;
samplesToSend += 2;
}
if (sampleRateDivisor != 1) {
divisorCounter++;
}
}
else {
// Skip 2 samples assuming stereo
captureSamples += 2;
divisorCounter++;
if (divisorCounter == sampleRateDivisor) {
divisorCounter = 0;
}
}
}
}
else {
bufferToSend = (short*)buf;
samplesToSend = numCaptureSamples;
}
// Receive until the peer shuts down the connection
iSendResult = send(clientSocket, (char*)bufferToSend, samplesToSend * 2, 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
return 0;
}
int SimpleTcpServer::shutdown()
{
if (clientSocket != INVALID_SOCKET) {
// shutdown the connection since we're done
int iResult = ::shutdown(clientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
closesocket(clientSocket);
}
if (listenSocket != INVALID_SOCKET) {
closesocket(listenSocket);
}
// cleanup
WSACleanup();
return 0;
}
void SimpleTcpServer::dumpLocalIp()
{
int i;
struct hostent *remoteHost;
struct in_addr addr;
char **pAlias;
char localHostName[80];
if (gethostname(localHostName, sizeof(localHostName)) == SOCKET_ERROR) {
printf("Error: gethostname %d\n", WSAGetLastError());
return;
}
remoteHost = gethostbyname(localHostName);
if (remoteHost == NULL) {
printf("Error: gethostbyaddr %d\n", WSAGetLastError());
return;
}
printf("Official name: %s\n", remoteHost->h_name);
for (i = 0, pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
printf("Alternate name #%d: %s\n", ++i, *pAlias);
}
if (remoteHost->h_addrtype == AF_INET) {
i = 0;
while (remoteHost->h_addr_list[i] != 0) {
addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
printf("\n\n\tIPv4 Address #%d: %s\n\n\n", i, inet_ntoa(addr));
}
}
else if (remoteHost->h_addrtype == AF_INET6) {
printf("Remotehost is an IPv6 address\n");
}
}