| 1 | #include <iostream> |
|---|
| 2 | #include <string> |
|---|
| 3 | #include "PacketManager.h" |
|---|
| 4 | #include "Client.h" |
|---|
| 5 | |
|---|
| 6 | #ifdef WIN32 |
|---|
| 7 | #include <windows.h> |
|---|
| 8 | #define usleep(x) Sleep((x)/1000) |
|---|
| 9 | #else |
|---|
| 10 | #include <unistd.h> |
|---|
| 11 | #endif |
|---|
| 12 | |
|---|
| 13 | using namespace network; |
|---|
| 14 | |
|---|
| 15 | void sender(){ |
|---|
| 16 | |
|---|
| 17 | network::PacketGenerator pck; |
|---|
| 18 | const int PORT = 55556; |
|---|
| 19 | std::cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << std::endl; |
|---|
| 20 | std::string str; |
|---|
| 21 | std::getline(std::cin, str); |
|---|
| 22 | std::cout << "You entered: " << str << std::endl; |
|---|
| 23 | if(str.compare("")==0) |
|---|
| 24 | str="127.0.0.1"; |
|---|
| 25 | |
|---|
| 26 | Client client( str, PORT ); |
|---|
| 27 | if ( client.establishConnection() ) |
|---|
| 28 | std::cout << "connection established" << std::endl; |
|---|
| 29 | else std::cout << "problems establishing connection" << std::endl; |
|---|
| 30 | |
|---|
| 31 | while (true) { |
|---|
| 32 | client.tick(0); |
|---|
| 33 | std::cout << "your message: "; |
|---|
| 34 | std::getline( std::cin, str ); |
|---|
| 35 | client.sendChat( str ); |
|---|
| 36 | std::cout << "sent message" << std::endl; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | void listener(){ |
|---|
| 44 | |
|---|
| 45 | const int PORT = 55556; |
|---|
| 46 | std::cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << std::endl; |
|---|
| 47 | std::string str; |
|---|
| 48 | std::getline(std::cin, str); |
|---|
| 49 | std::cout << "You entered: " << str << std::endl; |
|---|
| 50 | if(str.compare("")==0) |
|---|
| 51 | str="127.0.0.1"; |
|---|
| 52 | |
|---|
| 53 | Client client( str, PORT ); |
|---|
| 54 | if ( client.establishConnection() ) |
|---|
| 55 | std::cout << "connection established" << std::endl; |
|---|
| 56 | else std::cout << "problems establishing connection" << std::endl; |
|---|
| 57 | |
|---|
| 58 | while (true) { |
|---|
| 59 | client.tick(0); |
|---|
| 60 | usleep(100); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | int main(int argc, char **argv[]){ |
|---|
| 69 | std::string in; |
|---|
| 70 | std::cout << "Please choose: sender(s) oder listener(l)" << std::endl; |
|---|
| 71 | std::getline(std::cin, in); |
|---|
| 72 | if(in.compare("l")==0) |
|---|
| 73 | listener(); |
|---|
| 74 | else if(in.compare("s")==0) |
|---|
| 75 | sender(); |
|---|
| 76 | else |
|---|
| 77 | std::cout << "wrong answer" << std::endl; |
|---|
| 78 | } |
|---|