3

I am using c++ boost asio for making a server client application.

I followed the guide lines from here.

And I am still wondering why I get the following result:

./server    #ok

./client    # error

bind: Address already in use


server.cpp:

#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>

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

struct UDP_Message
{
    double number;
};

int main()
{
    try
    {
        boost::asio::io_service io_service;
        udp::socket socket(io_service, udp::endpoint(udp::v4(), config::udp_port));
        UDP_Message message;
        message.number=0;
        for (;;)
        {
            udp::endpoint remote_endpoint;
            message.number=message.number+0.001;
            boost::system::error_code ignored_error;
            socket.send_to(boost::asio::buffer(&message,sizeof(message)),
            remote_endpoint, 0, ignored_error);
        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return 0;
}

client.cpp:

#include <ctime>
#include <iostream>
#include <stdio.h>
#include <string>
#include <boost/array.hpp>
#include <boost/asio.hpp>

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

namespace config
{
    const unsigned short udp_port=1414;
}

struct UDP_Message
{
    double number;
};

int main()
{
    try
    {
        boost::asio::io_service io_service;
        boost::asio::socket_base::reuse_address option(true);
        udp::socket socket(io_service, udp::v4());
        socket.set_option(option);
        socket.bind(udp::endpoint(udp::v4(), config::udp_port));
        UDP_Message message;
        for (;;)
        {
            boost::array<char, 1> recv_buf;
            udp::endpoint remote_endpoint;
            boost::system::error_code error;
            socket.receive_from(boost::asio::buffer(recv_buf),
            remote_endpoint, 0, error);
            if (error && error != boost::asio::error::message_size)
            throw boost::system::system_error(error);
            std::memcpy(&message,recv_buf.data(),sizeof(message));
            std::cout<<message.number<<std::endl;
        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    return 0;
}
Community
  • 1
  • 1
ar2015
  • 5,558
  • 8
  • 53
  • 110
  • I trully believe than anybody who uses boost::asio should 'learn the ropes' of socket programming by using naked BSD socket routines. – SergeyA Jan 12 '16 at 15:04
  • @TNW got both from [here](http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tutdaytime5/src.html) and tried to separate them. – ar2015 Jan 12 '16 at 15:08
  • The `recv_buf` array looks like it's a bit on the small side. – molbdnilo Jan 12 '16 at 15:44

1 Answers1

0

You are trying to bind both your client and server to the same port, udp_port=1414. This you can not do.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • but they must send and listen to the same port – ar2015 Jan 12 '16 at 15:06
  • If you messed server and client code (as other comment suggests), my answer is not relevant. Please post proper code and I will edit my answer accordingly. – SergeyA Jan 12 '16 at 15:07
  • i got the codes from here: http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tutdaytime5/src.html – ar2015 Jan 12 '16 at 15:10
  • how to separate server from client? – ar2015 Jan 12 '16 at 15:15
  • thanks. could you please explain explicitly. how i should test send and receive for udp packets by two separate applications? – ar2015 Jan 12 '16 at 21:23
  • @ar2015, read something on UDP communication. There is a lot! – SergeyA Jan 12 '16 at 22:09
  • 10
    Thanks, but it is not an answer. – ar2015 Jan 13 '16 at 00:04
  • @ar2015 Yes, the client must send to the port the server listens on. And the server must send to the port the client listens on. The server listens on a well-known port. The client can send from any port, and the server should send replies to the port the client sent from. – David Schwartz Jan 21 '16 at 13:07