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.
93 lines
2.0 KiB
93 lines
2.0 KiB
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "UDPTCPNetwork.h"
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* server: only converts one message to upper cases and quits
|
|
*
|
|
*/
|
|
class Server {
|
|
private:
|
|
UDP udp;
|
|
public:
|
|
Server() { udp.Listen(12345); };
|
|
~Server() {};
|
|
void Loop();
|
|
};
|
|
|
|
|
|
void Server::Loop() {
|
|
char buffer[NET_BUFFERSIZE];
|
|
int i;
|
|
string srcaddr;
|
|
|
|
udp.ReadTimeout (&srcaddr, buffer, NET_BUFFERSIZE, 2000);
|
|
printf (" server got : %s from: '%s'\n", buffer, srcaddr.c_str());
|
|
for (i = 0; i < NET_BUFFERSIZE && buffer[i] != 0; i++)
|
|
buffer[i] = toupper (buffer[i]);
|
|
printf (" server send: %s to: '%s'\n", buffer, srcaddr.c_str());
|
|
udp.Write(srcaddr, buffer, i+1);
|
|
};
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* client: sends just one little text to the udp server
|
|
*
|
|
*/
|
|
class Client {
|
|
private:
|
|
UDP udp;
|
|
public:
|
|
Client() { udp.Listen(12346); };
|
|
~Client() {};
|
|
void Loop();
|
|
};
|
|
|
|
void Client::Loop() {
|
|
char buffer[NET_BUFFERSIZE];
|
|
string srcaddr = "localhost:12345";
|
|
|
|
sprintf (buffer, "only some test.");
|
|
|
|
printf ("client send: %s to: '%s'\n", buffer, srcaddr.c_str());
|
|
udp.Write(srcaddr, buffer, strlen (buffer));
|
|
udp.ReadTimeout(&srcaddr, buffer, NET_BUFFERSIZE, 2000);
|
|
printf ("client got : %s from: '%s'\n", buffer, srcaddr.c_str());
|
|
};
|
|
|
|
|
|
/****************************************************************************
|
|
*
|
|
* udp test application:
|
|
*
|
|
* creating a server and a client object. Where the client send some text
|
|
* to the server who converts it to upper case and sends it back to the
|
|
* client.
|
|
*
|
|
* Due to forking, both processes are running separated from each other and
|
|
* are NOT checked with waitforpid. So make sure you kill them yourself.
|
|
*
|
|
*/
|
|
int main (int argc, char **argv) {
|
|
Server server;
|
|
Client client;
|
|
|
|
pid_t pid;
|
|
|
|
pid = fork();
|
|
if (pid == 0) { // child process
|
|
client.Loop ();
|
|
}
|
|
else { // parent process
|
|
server.Loop ();
|
|
}
|
|
|
|
return 0;
|
|
};
|
|
|