source: network-game/client/Client/main.cpp@ 94ebbd9

Last change on this file since 94ebbd9 was 0dde5da, checked in by dportnoy <dmp1488@…>, 12 years ago

Added a makefile for the client and made the WSA functions only work on Windows platforms

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