1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
using namespace std;
#define OPEN_UDP_PORT 0
#define SEND_DATA_TO_SERVER 1
#define RECEIVE_ANSWER_FROM_SERVER 2
#define CLOSE_UDP_PORT 3
#define RECEIVE_DATA 20
#define ERROR 10
using boost::asio::ip::udp;
class UDPClient
{
public:
UDPClient(
boost::asio::io_service& io_service,
const std::string& host,
const std::string& port
) : io_service_(io_service), socket_(io_service, udp::endpoint(udp::v4(), 0)) {
udp::resolver resolver(io_service_);
udp::resolver::query query(udp::v4(), host, port);
udp::resolver::iterator iter = resolver.resolve(query);
boost::asio::socket_base::broadcast option(true); // Socket option to permit broadcast messages
socket_.set_option(option);
endpoint_ = *iter;
}
~UDPClient()
{
socket_.close();
}
void send(const std::string& msg) {
socket_.send_to(boost::asio::buffer(msg, msg.size()), endpoint_);
}
bool isOpen()
{
if(socket_.is_open())
{
return true;
}
else
{
return false;
}
}
void connect_handler(const boost::system::error_code &error )
{
if(!error)
{
cout << "Everything went well" << endl;
}
if(error)
{
cout << "Something went wront" << endl;
}
else
{
cout << "nothing returned" << endl;
}
}
void write_handler(const boost::system::error_code& error, std::size_t byte_transferred)
{
if(!error)
{
cout << "Everything went well" << endl;
}
if(error)
{
cout << "Something went wront" << endl;
}
else
{
cout << "nothing returned" << endl;
}
}
void sendToServer(string scannerAddress, int port)
{
boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::address::from_string(scannerAddress),port);
socket_.async_connect(endpoint,boost::bind(&UDPClient::connect_handler,this,boost::asio::placeholders::error)); //async connect to server
int data[3] = {-1,-1,17230};
socket_.async_send(boost::asio::buffer(data),0, boost::bind(&UDPClient::write_handler,this, boost::asio::placeholders::error,sizeof(data))); //async send to server
}
private:
boost::asio::io_service& io_service_;
udp::socket socket_;
udp::endpoint endpoint_;
};
int main()
{
int state = OPEN_UDP_PORT;
string scannerAddress = "10.48.37.183";
boost::asio::io_service io_service;
UDPClient client(io_service, "localhost", "5337" ); // Client created;
while(true)
{
switch (state)
{
case OPEN_UDP_PORT:
{
cout << "hello world" << endl;
if(client.isOpen())
{
cout << "UDP connection open!" << endl;
state = SEND_DATA_TO_SERVER;
}
else
{
cout << "UDP connection is not open!" << endl;
state = ERROR_HANDLING;
}
break;
}
case SEND_DATA_TO_SERVER:
{
//cout << "Send data to server" << endl;
client.sendToServer(scannerAddress,9008);
break;
}
case RECEIVE_ANSWER_FROM_SERVER:
{
cout << "hello world" << endl;
break;
}
case RECEIVE_DATA:
{
cout << "hello world" << endl;
break;
}
case CLOSE_UDP_PORT:
{
cout << "hello world" << endl;
break;
}
case ERROR_HANDLING:
{
cout << "hello world" << endl;
break;
}
}
}
return 0;
}
|