Atlas 0.7.0
Networking protocol for the Worldforge system.
simple_client.cpp
1/* Simple Atlas-C++ Client
2 *
3 * Part of the Atlas-C++ Tutorial
4 *
5 * Copyright 2000 Stefanus Du Toit.
6 *
7 * This file is covered by the GNU Free Documentation License.
8 */
9
10// cout, cerr
11#include <iostream>
12// sleep()
13#include <unistd.h>
14// Atlas negotiation
15#include <Atlas/Net/Stream.h>
16// The DebugBridge
17#include "DebugBridge.h"
18
19#include "sockbuf.h"
20
21extern "C" {
22 #include <stdio.h>
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <unistd.h>
28}
29
30#include <map>
31#include <list>
32
33using namespace Atlas;
34using namespace std;
35
36// This sends a very simple message to c
37void helloWorld(Codec<std::iostream> & c)
38{
39 cout << "Sending hello world message... " << flush;
40 c.streamMessage(Bridge::mapBegin);
41 c.mapStringItem("hello", "world");
42 c.mapEnd();
43 cout << "done." << endl;
44}
45
46int main(int argc, char** argv)
47{
48 int cli_fd = socket(PF_INET, SOCK_STREAM, 0);
49 if (cli_fd < 0) {
50 cerr << "ERROR: Could not open socket" << endl << flush;
51 exit(1);
52 }
53
54 cout << "Connecting..." << flush;
55
56 struct sockaddr_in sin;
57 sin.sin_family = AF_INET;
58 sin.sin_port = htons(6767);
59 sin.sin_addr.s_addr = htonl(0x7f000001);
60
61
62 int res = connect(cli_fd, (struct sockaddr *)&sin, sizeof(sin));
63
64 sockbuf cli_buf(cli_fd);
65 iostream connection(&cli_buf);
66
67 if (res == -1) {
68 cerr << "ERROR: Connection failed" << endl << flush;
69 exit(1);
70 }
71
72 // The DebugBridge puts all that comes through the codec on cout
73 DebugBridge bridge;
74 // Do client negotiation with the server
75 Net::StreamConnect conn("simple_client", connection, &bridge);
76
77 cout << "Negotiating... " << flush;
78 // conn.poll() does all the negotiation
79 while (conn.getState() == Negotiate<std::iostream>::IN_PROGRESS) {
80 conn.poll();
81 }
82 cout << "done" << endl;
83
84 // Check whether negotiation was successful
85 if (conn.getState() == Negotiate<std::iostream>::FAILED) {
86 cerr << "Failed to negotiate" << endl;
87 return 2;
88 }
89 // Negotiation was successful
90
91 // Get the codec that negotiation established
92 Codec<std::iostream> * codec = conn.getCodec();
93
94 // This should always be sent at the beginning of a session
95 codec->streamBegin();
96
97 // Say hello to the server
98 helloWorld(*codec);
99 connection << flush;
100
101 cout << "Sleeping for 2 seconds... " << flush;
102 // Sleep a little
103 sleep(2);
104 cout << "done." << endl;
105
106 // iosockinet::operator bool() returns false if the connection was broken
107 if (!connection) cout << "Server exited." << endl; else {
108 // It was not broken by the server, so we'll close ourselves
109 cout << "Closing connection... " << flush;
110 // This should always be sent at the end of a session
111 codec->streamEnd();
112 connection << flush;
113 // Close the socket
114 close(cli_fd);
115 cout << "done." << endl;
116 }
117
118 return 0;
119}
Definition: sockbuf.h:4
Definition: Bridge.h:20