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