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