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
Line 
1#include "../../common/compiler.h"
2
3#include <sys/types.h>
4
5#if defined WINDOWS
6 #include <winsock2.h>
7 #include <WS2tcpip.h>
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>
14 #include <cstring>
15#endif
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string>
20
21#include <iostream>
22
23#include <boost/lambda/lambda.hpp>
24
25#include "../../common/message.h"
26
27#ifdef WINDOWS
28 #pragma comment(lib, "ws2_32.lib")
29#endif
30
31using namespace std;
32
33void initWinSock();
34void shutdownWinSock();
35void error(const char *);
36
37int main(int argc, char *argv[])
38{
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();
51
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");
72
73 cout << msgFrom.buffer << endl;
74
75 while(true) {
76 cout << "Please enter a message (or q to quit): ";
77 cin.getline(msgTo.buffer, 256);
78
79 if (strcmp(msgTo.buffer, "q") == 0) {
80 break;
81 }
82
83 n=sendMessage(&msgTo, sock, &server);
84 if (n < 0)
85 error("sendMessage");
86
87 n = receiveMessage(&msgFrom, sock, &from);
88 if (n < 0)
89 error("receiveMessage");
90
91 cout << msgFrom.buffer << endl;
92 }
93
94#if defined WINDOWS
95 closesocket(sock);
96#elif defined LINUX
97 close(sock);
98#endif
99
100 shutdownWinSock();
101
102 cout << "Thank you for playing!" << endl;
103 getchar();
104
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
131}
132
133// need to make a function like this that works on windows
134void error(const char *msg)
135{
136 perror(msg);
137 shutdownWinSock();
138 exit(1);
139}
Note: See TracBrowser for help on using the repository browser.