You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.4 KiB
69 lines
1.4 KiB
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "UDPTCPNetwork.h"
|
|
|
|
#define DEFAULT_PORT 12345
|
|
|
|
int main(int argc, char **argv) {
|
|
TCP tcpserver;
|
|
TCP *connection = NULL;
|
|
int i, timeout;
|
|
pid_t pid;
|
|
time_t time_start = time (NULL);
|
|
time_t time_now = time (NULL);
|
|
char buffer[NET_BUFFERSIZE];
|
|
|
|
#ifdef BUILD_WINDOWS
|
|
char iobuffer[16];
|
|
setvbuf (stdout, iobuffer, _IONBF, 16);
|
|
#endif
|
|
|
|
|
|
//
|
|
// start the server
|
|
if (tcpserver.Listen(DEFAULT_PORT) != 1) {
|
|
printf ("cloud not start the tcp server\n");
|
|
exit (1);
|
|
}
|
|
|
|
printf ("server started\n");
|
|
|
|
//
|
|
// check for connections
|
|
while(1) {
|
|
if (connection == NULL) {
|
|
connection = tcpserver.Accept();
|
|
if (connection != NULL)
|
|
printf (" server: got a new connection\n");
|
|
}
|
|
|
|
if (connection != NULL) {
|
|
memset (buffer, 0x0, NET_BUFFERSIZE);
|
|
i = connection->ReadTimeout(buffer, NET_BUFFERSIZE, 100);
|
|
if (i >= 0) {
|
|
int c;
|
|
|
|
printf (" server: (child) got: '%s'\n", buffer);
|
|
for (c = 0; c < i; c++) buffer[c] = toupper(buffer[c]);
|
|
connection->Write(buffer, i);
|
|
}
|
|
else {
|
|
printf (" server: (child) got: nothing (i:%d)\n", i);
|
|
delete connection;
|
|
connection = NULL;
|
|
}
|
|
}
|
|
else {
|
|
//
|
|
// parent process - just close the client connection
|
|
// it will be handeled by the child process.
|
|
printf ("no connection\n");
|
|
}
|
|
usleep (25000);
|
|
}
|
|
tcpserver.Close();
|
|
};
|
|
|