source: network-game/client/Client/main.cpp@ 7d7df47

Last change on this file since 7d7df47 was 7d7df47, checked in by dportnoy <dmp1488@…>, 12 years ago

Added a directory for common code and added basic boost library code to the client

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/* UDP client in the internet domain */
2#include <sys/types.h>
3
4#include <winsock2.h>
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include <iostream>
11
12#include <boost/lambda/lambda.hpp>
13
14#include "../../common/message.h"
15
16#pragma comment(lib, "ws2_32.lib")
17
18using namespace std;
19
20void error(const char *);
21
22int boost_main()
23{
24 using namespace boost::lambda;
25 typedef istream_iterator<int> in;
26
27 for_each(in(cin), in(), cout << (_1 * 3) << " " );
28
29 return 0;
30}
31
32int main(int argc, char *argv[])
33{
34 int sock, n;
35 int length;
36 struct sockaddr_in server, from;
37 struct hostent *hp;
38 char buffer[256];
39
40 if (argc != 3) {
41 printf("Usage: server port\n");
42 exit(1);
43 }
44
45 WORD wVersionRequested;
46 WSADATA wsaData;
47 int wsaerr;
48
49 wVersionRequested = MAKEWORD(2, 2);
50 wsaerr = WSAStartup(wVersionRequested, &wsaData);
51
52 if (wsaerr != 0) {
53 printf("The Winsock dll not found.\n");
54 exit(1);
55 }else
56 printf("The Winsock dll was found.\n");
57
58 sock= socket(AF_INET, SOCK_DGRAM, 0);
59 if (sock < 0) error("socket");
60
61 server.sin_family = AF_INET;
62 hp = gethostbyname(argv[1]);
63 if (hp==0) error("Unknown host");
64
65 memcpy((char *)&server.sin_addr,
66 (char *)hp->h_addr,
67 hp->h_length);
68 server.sin_port = htons(atoi(argv[2]));
69 length=sizeof(struct sockaddr_in);
70 printf("Please enter the message: ");
71 memset(buffer, 0, 256);
72 fgets(buffer,255,stdin);
73 n=sendto(sock,buffer,
74 strlen(buffer),0,(const struct sockaddr *)&server,length);
75 if (n < 0) error("Sendto");
76 n = recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length);
77 if (n < 0) error("recvfrom");
78
79 buffer[n] = '\0';
80 cout << "Got an ack: " << endl;
81 cout << buffer << endl;
82
83 closesocket(sock);
84
85 WSACleanup();
86
87 return 0;
88}
89
90/*
91int sendMessage(short type, string contents, int sock, struct sockaddr_in *dest)
92{
93 NETWORK_MSG msg;
94
95 msg.type = type;
96 strcpy(msg.buffer, contents.c_str());
97
98 return sendto(sock, (char*)&msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(dest));
99}
100*/
101
102void error(const char *msg)
103{
104 perror(msg);
105 WSACleanup();
106 exit(1);
107}
Note: See TracBrowser for help on using the repository browser.