Boost - server client example not working.

closed account (2NywAqkS)
Hi,
I'm been switching over from winsock to boost::asio and have been following the official examples. When I run this over a LAN connection there are no issues, however when I try and run it over the internet nothing happens. I'm almost certain that I'm port forwarding correctly so there has to be something wrong with the code or my router. It's not even like the packets occasionally get lost because I've created a client that sends like 2000 packets a second and still nothing. I'd love to be more concise but I think it'd be easier if I just pasted in all the client server code.

Client:
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
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

enum { max_length = 1024 };

int main()
{
    boost::asio::io_service io_service;

    udp::socket s(io_service, udp::endpoint(udp::v4(), 0));

    udp::resolver resolver(io_service);
    udp::resolver::query query(udp::v4(), "my ip", "5555");
    udp::resolver::iterator iterator = resolver.resolve(query);

	while(1 < 2)
	{
		s.send_to(boost::asio::buffer("Hello", 5), *iterator);
	}

  return 0;
}


Server:
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
#define _CRT_SECURE_NO_WARNINGS

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      socket_(io_service, udp::endpoint(udp::v4(), port))
  {
    socket_.async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&server::handle_receive_from, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

  void handle_receive_from(const boost::system::error_code& error, size_t bytes_recvd)
  {
    if (!error && bytes_recvd > 0)
    {
		std::cout << "Roger";
      socket_.async_send_to(
          boost::asio::buffer(data_, bytes_recvd), sender_endpoint_,
          boost::bind(&server::handle_send_to, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
    else
    {
      socket_.async_receive_from(
          boost::asio::buffer(data_, max_length), sender_endpoint_,
          boost::bind(&server::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
  }

  void handle_send_to(const boost::system::error_code& /*error*/,
      size_t /*bytes_sent*/)
  {
    socket_.async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&server::handle_receive_from, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

private:
  boost::asio::io_service& io_service_;
  udp::socket socket_;
  udp::endpoint sender_endpoint_;
  enum { max_length = 1024 };
  char data_[max_length];
};

int main()
{
    boost::asio::io_service io_service;
    server s(io_service, 5555);

    io_service.run();

  return 0;
}


I'm using boost 1.54

Any help is much appreciated!
Thanks,
Rowan.
works for me (although I had to set up port forwarding too)
closed account (2NywAqkS)
The problem was for some reason is was blocked by windows firewall.
Topic archived. No new replies allowed.