-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
161 lines (136 loc) · 4.09 KB
/
server.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
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <ctime> // time_t, time(), tm, strftime
#include <sys/socket.h> // socket(), listen(), accept()
#include <netinet/in.h> // sockaddr_in
#include <unistd.h> // read()
#include "config.h"
int main(int argCount, char *argValues[])
{
time_t timeNow = time(0);
tm *localTimeNow = localtime(&timeNow);
char logFileName[sizeof("tcpServer_2020-09-17_2221.log")];
size_t timeSize = strftime(
logFileName,
sizeof(logFileName),
"tcpServer_%F_%H%M.log",
localTimeNow);
std::ofstream logFile;
logFile.open(logFileName);
// Create socketDescriptor
int socketDescriptor = socket(
AF_INET,
SOCK_STREAM,
0);
if (-1 == socketDescriptor)
{
// Handle socket() error
std::cerr << "socket() error" << std::endl;
return -1;
}
int optionValue= 1;
int returnValue;
returnValue = setsockopt(
socketDescriptor,
SOL_SOCKET,
SO_REUSEADDR,
&optionValue,
sizeof(int));
if (returnValue < 0)
{
std::cerr << "setsockopt(SO_REUSEADDR) failed" << std::endl;
return -1;
}
// Create socketAddress
struct sockaddr_in socketAddress;
memset(&socketAddress, 0, sizeof(socketAddress));
socketAddress.sin_family = AF_INET;
socketAddress.sin_addr.s_addr = INADDR_ANY;
socketAddress.sin_port = htons(portNumber);
// Bind address to socket
int bindResult = bind(
socketDescriptor,
reinterpret_cast<sockaddr*>(&socketAddress),
sizeof(socketAddress));
if (0 != bindResult)
{
// Handle bind() error
std::cerr << "bind() error" << std::endl;
return -1;
}
// Listen for connections
listen(socketDescriptor, SOMAXCONN);
while (1)
{
sockaddr_in clientAddress;
int clientDescriptor;
socklen_t clientLength = sizeof(clientAddress);
std::cout << "Waiting for connection..." << std::flush;
// Accept new connection
clientDescriptor = accept(
socketDescriptor,
reinterpret_cast<sockaddr*>(&clientAddress),
&clientLength);
if (-1 == clientDescriptor)
{
// Handle accept() error
std::cerr << "accept() error" << std::endl;
return -1;
}
std::cout << "accepted" << std::endl;
// Set recieve timeout
#if 1
struct timeval timeValue = {120,0};
int returnValue;
returnValue = setsockopt(
clientDescriptor,
SOL_SOCKET,
SO_RCVTIMEO,
reinterpret_cast<const char*>(&timeValue),
sizeof(timeValue));
if (returnValue < 0)
{
std::cerr << "setsockopt(SO_RCVTIMEO) failed" << std::endl;
return -1;
}
#endif
while (1)
{
int byteCount;
char tcpBuffer[1024];
std::cout << "Reading..." << std::flush;
// Read in data
byteCount = read(
clientDescriptor,
tcpBuffer,
sizeof(tcpBuffer));
if (byteCount <= 0)
{
// Handle read() error
std::cerr << "read() error" << std::endl;
break;
}
if (byteCount >= sizeof(tcpBuffer))
{
byteCount = sizeof(tcpBuffer) - 1;
}
tcpBuffer[byteCount] = '\0';
// Handle data
std::cout << "(" << byteCount << ") " << tcpBuffer << std::endl;
timeNow = time(0);
localTimeNow = localtime(&timeNow);
char timeStampBuffer[sizeof("2020-09-17_22:34:00")];
timeSize = strftime(
timeStampBuffer,
sizeof(timeStampBuffer),
"%F %H:%M:%S",
localTimeNow);
logFile << timeStampBuffer << " " << tcpBuffer << std::endl;
}
// Close client socket
close(clientDescriptor);
}
logFile.close();
}