| 1 |
#include <ClanLib/application.h> |
| 2 |
#include <ClanLib/core.h> |
| 3 |
#include <ClanLib/network.h> |
| 4 |
|
| 5 |
class CL_NetworkTest : public CL_ClanApplication |
| 6 |
{ |
| 7 |
|
| 8 |
CL_Socket *client; |
| 9 |
CL_IPAddress *ip; |
| 10 |
bool quit; |
| 11 |
|
| 12 |
int main(int argv, char**) |
| 13 |
{ |
| 14 |
quit = false; |
| 15 |
|
| 16 |
CL_SetupCore::init(); |
| 17 |
CL_SetupNetwork::init(); |
| 18 |
client = new CL_Socket(CL_Socket::tcp); |
| 19 |
ip = new CL_IPAddress("www.clanlib.org", "80"); |
| 20 |
std::string request_msg = "GET index.html HTTP/1.0\n\n"; |
| 21 |
|
| 22 |
client->connect(*ip); |
| 23 |
client->send(request_msg); |
| 24 |
|
| 25 |
CL_Slot read_client = client->sig_read_triggered().connect(this, &CL_NetworkTest::client_read); |
| 26 |
CL_Slot remote_disc = client->sig_disconnected().connect(this, &CL_NetworkTest::remote_disconnect); |
| 27 |
|
| 28 |
while(!quit) |
| 29 |
{ |
| 30 |
CL_System::keep_alive(); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
delete client; |
| 35 |
delete ip; |
| 36 |
|
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
void client_read() |
| 41 |
{ |
| 42 |
char buffer[64*1024+1]; |
| 43 |
int received = client->recv(buffer, 64*1024); |
| 44 |
|
| 45 |
if(received == 0) |
| 46 |
{ |
| 47 |
std::cout << "No data in the buffer yet client_read() was called" << std::endl; |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
buffer[received] = 0; |
| 52 |
|
| 53 |
std::cout << buffer << std::endl; |
| 54 |
} |
| 55 |
|
| 56 |
void remote_disconnect() |
| 57 |
{ |
| 58 |
std::cout << "The remote side has closed the socket" << std::endl; |
| 59 |
quit = true; |
| 60 |
} |
| 61 |
|
| 62 |
} app; |