-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_main.cpp
171 lines (138 loc) · 5.26 KB
/
server_main.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
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
#include <stdlib.h>
#include "server_functions.h"
#include "command_def.h"
using namespace std;
int main(int argc, char* argv[])
{
int portnum;
SOCKET s;
DWORD err;
TCHAR lpBuffer[256];
// Get current time
time_t raw_time;
struct tm * time_info;
raw_time = time(NULL);
time_info = localtime(&raw_time);
// Get listening port number
if(argc != 3)
{
cout << "Error (missing parameters) " << endl;
cout << "Usage: " << argv[0] << " <Device Name>" << " <Port Number>\n" << endl;
return 1;
}
else
{
// Print current time and server version
cout << "\nVersion " << VERSION << endl;
printf("Server (%s) started on %s", argv[1], asctime(time_info));
// Get given port number
portnum = atoi(argv[2]);
}
WORD wVersionRequested;
WSADATA wsaData;
int status_err;
// Using MAKEWORD macro, Winsock version request 2.2
wVersionRequested = MAKEWORD(2,2);
// Initialize Windows Socket API
status_err = WSAStartup(wVersionRequested, &wsaData);
err = GetLastError();
if(status_err != 0)
{
// Tell the user that we could not find a usable WinSock DLL.
perror("The Winsock dll not found!\n");
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
255, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (establish): " << lpBuffer << endl;
return 1;
}
else
{
// Winsock dll is found
printf("WSA status: %s\n", wsaData.szSystemStatus);
}
// Confirm that the WinSock DLL supports 2.2.
// Note that if the DLL supports versions greater than 2.2
// in addition to 2.2, it will still return 2.2 in wVersion
// since that is the version we requested
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
// Tell the user that we could not find a usable WinSock DLL.
printf("The dll does not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
WSACleanup();
return 1;
}
// The WinSock DLL is acceptable. Proceed.
// Create socket
s = establish(portnum);
err = GetLastError();
if(s == INVALID_SOCKET)
{
perror("Error establishing socket\n");
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
255, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (establish): " << lpBuffer << endl;
return 1;
}
// Print list of available server commands
Send_CommandList(s);
// Wait for clients
SOCKET new_sock;
int nTimeout = 1800000; // timeout in 30 minute
for(;;)
{
printf("\nWaiting for client to connect...\n");
if(s == INVALID_SOCKET)
{
fprintf(stderr, "Error waiting for new connection!\n");
return 1;
}
new_sock = accept(s, NULL, NULL);
err = GetLastError();
if(new_sock == INVALID_SOCKET)
{
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
255, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (accept): " << lpBuffer << endl;
return 1;
}
// Set timeout condition on socket
status_err = setsockopt(new_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*) &nTimeout, sizeof(int));
err = GetLastError();
if(status_err != 0)
{
// Get standard error message from system
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, // It´s a system error
NULL, // No string to be formatted needed
err, // Put error code here
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Do it in the standard language
lpBuffer, // Put the error message here
255, // Number of bytes to store the message (STR_ELEMS(lpBuffer)-1)
NULL);
cout << "Error (setsockopt): " << lpBuffer << endl;
return 1;
}
Process_Request(new_sock);
closesocket(new_sock);
printf("Client disconnected!\n");
}
WSACleanup();
return 0;
}