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.
158 lines
3.4 KiB
158 lines
3.4 KiB
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "UDPTCPNetwork.h"
|
|
|
|
#define DEFAULT_PORT 12345
|
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
|
|
#else
|
|
|
|
void server () {
|
|
TCP tcpserver;
|
|
TCP *connection;
|
|
SSLSocket ssl;
|
|
int i, timeout;
|
|
pid_t pid;
|
|
time_t time_start = time (NULL);
|
|
time_t time_now = time (NULL);
|
|
char buffer[NET_BUFFERSIZE];
|
|
|
|
//
|
|
// start the server
|
|
if (tcpserver.Listen(DEFAULT_PORT) != 1) {
|
|
printf ("cloud not start the tcp server\n");
|
|
exit (1);
|
|
}
|
|
|
|
//
|
|
// init SSL
|
|
if (ssl.SetCertificat("cert.pem", "privkey.pem") != 1) {
|
|
printf ("SetCertificat error:%s\n", strerror(errno));
|
|
exit (1);
|
|
}
|
|
|
|
//
|
|
// check for connections
|
|
for (;time_now - time_start < 10; time_now = time(NULL)) {
|
|
connection = tcpserver.Accept();
|
|
if (connection != NULL) {
|
|
//
|
|
// someone connected - create new process
|
|
// take care of parallel processing (parent is always the server)
|
|
//
|
|
printf (" server: got a connection forking new process\n");
|
|
pid = fork();
|
|
if (pid == 0) {
|
|
//
|
|
// child process - always close server since it will handeled
|
|
// by the parent process. Make sure the client exits and never
|
|
// returns.
|
|
|
|
tcpserver.Close();
|
|
if (ssl.Accept(connection->GetSocket(), 0) != 1) {
|
|
printf ("could not establish SSL connection:%s\n", strerror(errno));
|
|
exit (1);
|
|
}
|
|
i = ssl.Read(buffer, NET_BUFFERSIZE);
|
|
if (i > 0) {
|
|
int c;
|
|
|
|
printf (" server: got: '%s'\n", buffer);
|
|
for (c = 0; c < i; c++) buffer[c] = toupper(buffer[c]);
|
|
ssl.Write(buffer, i);
|
|
}
|
|
//
|
|
// just delete the class object, it will close the client connection
|
|
ssl.Close();
|
|
delete (connection);
|
|
|
|
//
|
|
// exit child process
|
|
exit (1);
|
|
}
|
|
else {
|
|
//
|
|
// parent process - just close the client connection
|
|
// it will be handeled by the child process.
|
|
delete (connection);
|
|
}
|
|
}
|
|
usleep (25000);
|
|
}
|
|
};
|
|
|
|
|
|
void client () {
|
|
TCP tcpclient;
|
|
SSLSocket ssl;
|
|
char buffer[NET_BUFFERSIZE];
|
|
int i, res;
|
|
|
|
sleep (1); // wait one second to start the server
|
|
|
|
//
|
|
// connect to the server
|
|
if (tcpclient.Connect ("localhost", DEFAULT_PORT) != 1) {
|
|
printf ("cloud not connect to server\n");
|
|
exit (1);
|
|
}
|
|
|
|
res = ssl.Connect(tcpclient.GetSocket(), 100);
|
|
if (res == -1) {
|
|
printf ("could not establish SSL connection:errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str());
|
|
exit (1);
|
|
}
|
|
|
|
//
|
|
// send some data
|
|
snprintf (buffer, NET_BUFFERSIZE, "nur ein kleiner Test.");
|
|
printf ("client:send '%s' to the server.\n", buffer);
|
|
|
|
if (ssl.Write(buffer, strlen (buffer)) != strlen (buffer)) {
|
|
printf ("could not send all data. errno:%s sslerror:%s\n", strerror(errno), ssl.GetSSLErrorText(ssl.sslerror).c_str());
|
|
exit (1);
|
|
}
|
|
|
|
//
|
|
// read some data (wait maximum 10x1000ms)
|
|
for (i = 10; i > 0; i--)
|
|
if (ssl.Read(buffer, NET_BUFFERSIZE) > 0) {
|
|
printf ("client:got '%s' from server.\n", buffer);
|
|
break;
|
|
}
|
|
|
|
//
|
|
// close connection
|
|
ssl.Close();
|
|
tcpclient.Close();
|
|
};
|
|
|
|
#endif
|
|
|
|
int main (int argc, char **argv) {
|
|
pid_t pid;
|
|
|
|
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
|
|
#else
|
|
pid = fork();
|
|
if (pid == 0) { // child process
|
|
printf ("start client\n");
|
|
client();
|
|
printf ("start client\n");
|
|
client();
|
|
printf ("start client\n");
|
|
client();
|
|
printf ("start client\n");
|
|
client();
|
|
}
|
|
else { // parent process
|
|
server();
|
|
}
|
|
#endif
|
|
|
|
return 0;
|
|
};
|
|
|