[a845faf] | 1 | #include "../../common/compiler.h"
|
---|
| 2 |
|
---|
[1912323] | 3 | #include <sys/types.h>
|
---|
| 4 |
|
---|
[e08572c] | 5 | #if defined WINDOWS
|
---|
[a845faf] | 6 | #include <winsock2.h>
|
---|
| 7 | #include <WS2tcpip.h>
|
---|
[e08572c] | 8 | #elif defined LINUX
|
---|
| 9 | #include <sys/types.h>
|
---|
| 10 | #include <unistd.h>
|
---|
| 11 | #include <sys/socket.h>
|
---|
| 12 | #include <netinet/in.h>
|
---|
| 13 | #include <netdb.h>
|
---|
[a845faf] | 14 | #endif
|
---|
[1912323] | 15 |
|
---|
| 16 | #include <stdio.h>
|
---|
| 17 | #include <stdlib.h>
|
---|
[a845faf] | 18 | #include <string>
|
---|
[1912323] | 19 |
|
---|
| 20 | #include <iostream>
|
---|
| 21 |
|
---|
[7d7df47] | 22 | #include <boost/lambda/lambda.hpp>
|
---|
| 23 |
|
---|
| 24 | #include "../../common/message.h"
|
---|
| 25 |
|
---|
[a845faf] | 26 | #ifdef WINDOWS
|
---|
| 27 | #pragma comment(lib, "ws2_32.lib")
|
---|
| 28 | #endif
|
---|
[1912323] | 29 |
|
---|
| 30 | using namespace std;
|
---|
| 31 |
|
---|
| 32 | void error(const char *);
|
---|
[7d7df47] | 33 |
|
---|
[1912323] | 34 | int main(int argc, char *argv[])
|
---|
| 35 | {
|
---|
[aee34b9] | 36 | int sock, n;
|
---|
| 37 | struct sockaddr_in server, from;
|
---|
| 38 | struct hostent *hp;
|
---|
| 39 | char buffer[256];
|
---|
[a845faf] | 40 | NETWORK_MSG msgTo, msgFrom;
|
---|
[aee34b9] | 41 |
|
---|
[1912323] | 42 | if (argc != 3) {
|
---|
[aee34b9] | 43 | cout << "Usage: server port" << endl;
|
---|
[1912323] | 44 | exit(1);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | WORD wVersionRequested;
|
---|
| 48 | WSADATA wsaData;
|
---|
| 49 | int wsaerr;
|
---|
| 50 |
|
---|
| 51 | wVersionRequested = MAKEWORD(2, 2);
|
---|
| 52 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
| 53 |
|
---|
| 54 | if (wsaerr != 0) {
|
---|
[aee34b9] | 55 | cout << "The Winsock dll not found." << endl;
|
---|
[1912323] | 56 | exit(1);
|
---|
| 57 | }else
|
---|
[aee34b9] | 58 | cout << "The Winsock dll was found." << endl;
|
---|
[1912323] | 59 |
|
---|
| 60 | sock= socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[aee34b9] | 61 | if (sock < 0)
|
---|
| 62 | error("socket");
|
---|
[1912323] | 63 |
|
---|
| 64 | server.sin_family = AF_INET;
|
---|
| 65 | hp = gethostbyname(argv[1]);
|
---|
[aee34b9] | 66 | if (hp==0)
|
---|
| 67 | error("Unknown host");
|
---|
| 68 |
|
---|
| 69 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
| 70 | server.sin_port = htons(atoi(argv[2]));
|
---|
| 71 |
|
---|
[8efe484] | 72 | strcpy(msgTo.buffer, "Hello");
|
---|
| 73 | n=sendMessage(&msgTo, sock, &server);
|
---|
| 74 | if (n < 0)
|
---|
| 75 | error("sendMessage");
|
---|
| 76 |
|
---|
| 77 | n = receiveMessage(&msgFrom, sock, &from);
|
---|
| 78 | if (n < 0)
|
---|
| 79 | error("receiveMessage");
|
---|
| 80 |
|
---|
| 81 | cout << msgFrom.buffer << endl;
|
---|
| 82 |
|
---|
[6c92572] | 83 | while(true) {
|
---|
[8efe484] | 84 | cout << "Please enter a message (or q to quit): ";
|
---|
[6c92572] | 85 | cin.getline(msgTo.buffer, 256);
|
---|
| 86 |
|
---|
[8efe484] | 87 | if (strcmp(msgTo.buffer, "q") == 0) {
|
---|
[6c92572] | 88 | break;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | n=sendMessage(&msgTo, sock, &server);
|
---|
| 92 | if (n < 0)
|
---|
| 93 | error("sendMessage");
|
---|
| 94 |
|
---|
| 95 | n = receiveMessage(&msgFrom, sock, &from);
|
---|
| 96 | if (n < 0)
|
---|
| 97 | error("receiveMessage");
|
---|
[aee34b9] | 98 |
|
---|
[6c92572] | 99 | cout << msgFrom.buffer << endl;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[1912323] | 102 | closesocket(sock);
|
---|
| 103 |
|
---|
| 104 | WSACleanup();
|
---|
| 105 |
|
---|
[8efe484] | 106 | cout << "Thank you for playing!" << endl;
|
---|
| 107 | getchar();
|
---|
| 108 |
|
---|
[1912323] | 109 | return 0;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[a845faf] | 112 | // need to make a function like this that works on windows
|
---|
[1912323] | 113 | void error(const char *msg)
|
---|
| 114 | {
|
---|
| 115 | perror(msg);
|
---|
| 116 | WSACleanup();
|
---|
| 117 | exit(1);
|
---|
| 118 | } |
---|