parent
dd9359c709
commit
7d0b48d7bf
@ -0,0 +1,65 @@
|
|||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "UDPTCPNetwork.h"
|
||||||
|
|
||||||
|
#define DEFAULT_PORT 12345
|
||||||
|
|
||||||
|
int main (int argc, char **argv) {
|
||||||
|
TCP tcpclient;
|
||||||
|
char buffer[NET_BUFFERSIZE];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
#ifdef BUILD_WINDOWS
|
||||||
|
char iobuffer[16];
|
||||||
|
setvbuf (stdout, iobuffer, _IONBF, 16);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//
|
||||||
|
// connect to the server
|
||||||
|
if (argc == 2) {
|
||||||
|
if (tcpclient.Connect(argv[1], DEFAULT_PORT) != 1) {
|
||||||
|
printf ("connect to: %s:%s\n",argv[1], DEFAULT_PORT);
|
||||||
|
printf ("cloud not connect to server\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (argc == 3) {
|
||||||
|
printf ("connect to: %s:%s\n",argv[1], atoi(argv[2]));
|
||||||
|
if (tcpclient.Connect(argv[1], argv[2]) != 1) {
|
||||||
|
printf ("cloud not connect to server\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
printf ("\nplease use the ollowing commands:\n\n");
|
||||||
|
printf (" test-tcpclient destination\n");
|
||||||
|
printf (" test-tcpclient destination port\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// send some data
|
||||||
|
snprintf (buffer, NET_BUFFERSIZE, "nur ein kleiner Test.");
|
||||||
|
printf ("client:send '%s' to the server.\n", buffer);
|
||||||
|
if (tcpclient.Write(buffer, strlen (buffer)) != strlen (buffer)) {
|
||||||
|
printf ("could not send all data.\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// read some data (wait maximum 10x1000ms)
|
||||||
|
for (i = 10; i > 0; i--)
|
||||||
|
if (tcpclient.ReadTimeout(buffer, NET_BUFFERSIZE, 1000) > 0) {
|
||||||
|
printf ("client:got '%s' from server.\n", buffer);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// close connection
|
||||||
|
tcpclient.Close();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
|
||||||
|
#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) {
|
||||||
|
i = connection->Read(buffer, NET_BUFFERSIZE);
|
||||||
|
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();
|
||||||
|
};
|
||||||
|
|
||||||
Loading…
Reference in new issue