Debian Codegraph @

Tests/SocketTest/server.cpp

raw
1 /*
2 $Id: server.cpp,v 1.4 2004/02/10 17:01:45 sphair Exp $
3
4 Program to test how well sockets work in ClanLib.
5
6 Author: Mark Robinson Feb 2003
7 */
8 #include <ClanLib/core.h>
9 #include <ClanLib/application.h>
10 #include <ClanLib/network.h>
11
12 class ClientConnection : public CL_Runnable
13 {
14 CL_Socket conn;
15 CL_Thread *th;
16
17 public:
18 ClientConnection(CL_Socket sock)
19 {
20 conn = sock;
21 conn.sig_disconnected().connect(this, &ClientConnection::sock_close);
22 th = new CL_Thread(this);
23 th->start();
24 }
25
26 virtual ~ClientConnection()
27 {
28 // I guess we can stop our thread now
29 th->terminate();
30 }
31
32 void sock_close()
33 {
34 std::cout << "Client has closed the socket" << std::endl;
35 delete this;
36 //th->terminate();
37 }
38
39 void run()
40 {
41 char *big_buf = new char[1024+1];
42 std::cout << "Write on Socket" << std::endl;
43 while(1)
44 {
45 conn.recv(big_buf, 1024);
46 std::cout << big_buf << std::endl;
47 memset(big_buf, 0, 1024+1);
48 CL_System::keep_alive();
49 }
50 delete[] big_buf;
51 CL_System::keep_alive();
52 }
53
54 };
55
56 class SocketTest : public CL_ClanApplication
57 {
58
59 CL_IPAddress *server_ip;
60 CL_Socket *server_socket;
61 CL_Socket connection;
62 bool has_connected;
63
64 void print_help()
65 {
66 std::cout << "client [-6|-4]" << std::endl;
67 std::cout << "-6 Connect with IPv6" << std::endl;
68 std::cout << "-4 Connect with IPv4(default)" << std::endl;
69 }
70
71 int main(int argv, char** argc)
72 {
73
74 CL_SetupCore::init();
75 CL_SetupNetwork::init();
76
77 has_connected = false;
78
79 if(argv > 1)
80 {
81 if(strncmp(argc[1], "-6", 2) == 0)
82 {
83 server_ip = new CL_IPAddress("9000");
84 server_socket = new CL_Socket(CL_Socket::tcp, CL_Socket::ipv6);
85 }
86 else if(strncmp(argc[1], "-4", 2) == 0)
87 {
88 server_ip = new CL_IPAddress("9000");
89 server_socket = new CL_Socket(CL_Socket::tcp, CL_Socket::ipv4);
90 }
91 else
92 {
93 print_help();
94 return 0;
95 }
96 }
97 else
98 {
99 server_ip = new CL_IPAddress("9000");
100 server_socket = new CL_Socket(CL_Socket::tcp, CL_Socket::ipv4);
101 }
102
103 server_socket->sig_read_triggered().connect(this, &SocketTest::sock_read);
104 server_socket->sig_write_triggered().connect(this, &SocketTest::sock_write);
105 server_socket->sig_exception_triggered().connect(this, &SocketTest::something);
106 server_socket->sig_disconnected().connect(this, &SocketTest::something);
107
108 try
109 {
110 server_socket->bind(*server_ip);
111 }
112 catch(CL_Error e)
113 {
114 std:: cout << e.message << std::endl;
115 return 0;
116 }
117 server_socket->listen(1);
118
119 while(1)
120 {
121 new ClientConnection(server_socket->accept());
122 CL_System::keep_alive();
123 }
124
125
126 delete server_socket;
127 delete server_ip;
128 return 0;
129
130 }
131
132 void sock_read()
133 {
134 std::cout << "Read on Socket" << std::endl;
135 }
136
137 void sock_write()
138 {
139 std::cout << "Write on Socket" << std::endl;
140 }
141
142 void something()
143 {
144 std::cout << "Something Happened" << std::endl;
145 }
146
147 void doStuff()
148 {
149 CL_System::keep_alive();
150 }
151
152
153 } app;