#include "Dasyne.hpp"
#include <iostream>
using namespace std;
using namespace libdasyne;DEngine netEngine;DClient *client = new DClient(&netEngine);const uint16_t PORT_NUMBER = 5127;static bool runProgram = true;void connectHandler(std::string netMessage,
NetworkCode socketStatus)
{
DBuffer greeting;
if (socketStatus == DA_SUCCESS)
{
cout << "Connected to " << client->getRemoteAddress() << endl;
greeting.appendString("Hello, Server!");
client->send(greeting);
} else
{
cout << netMessage << endl;
runProgram = false;
}}void sendHandler(void)
{
cout << "Sent all data" << endl;
}void receiveHandler(DBuffer &theData)
{
cout << "Received " << theData.toString() << endl;
cout << "Disconnecting..." << endl;
runProgram = false;
}void errorHandler(string errMsg,
bool socketOK)
{
cout << errMsg << endl;
if (!socketOK)
runProgram = false;
}
int main(int argc,
char *argv[])
{
int eventsRun = 0; if (argc != 2)
{
cout << "Usage: simpleclient <host name>" << endl;
cout << endl;
runProgram = false;
} else
{
client->setReceiveHandler(&receiveHandler);
client->setSendHandler(&sendHandler);
client->setErrorHandler(&errorHandler);
client->startConnect(argv[1], PORT_NUMBER, DA_IPV4, &connectHandler);
} while (runProgram)
{ eventsRun = netEngine.update();
if (eventsRun < 0)
{
cout << netEngine.getErrMsg() << endl;
runProgram = false;
} else if (eventsRun == 0)
DEngine::sleep(1);
} delete client;
return 0;
}
Back to Contents