views:

1282

answers:

3

Learning boost, and compiled their daytime server client example. Since I cant use port 13 that is in the example I only changed the port numbers in the server and client example. Server runs fine, but the client doesnt connect it seems, and no error is given.

Input data for the client is "127.0.0.1".

Server:

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

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}

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

    tcp::endpoint endpoint(tcp::v4(), 8087);
    tcp::acceptor acceptor(io_service, endpoint);

    for (;;)
    {
      tcp::iostream stream;
      acceptor.accept(*stream.rdbuf());
      stream << "test" << make_daytime_string();
    }
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

And the client:

#include <iostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: daytime_client <host>" << std::endl;
      return 1;
    }

    tcp::iostream s(argv[1], 8087);
    std::string line;
    std::getline(s, line);
    std::cout << line << std::endl;
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
+1  A: 

Hmm, all works on 1_36 boost version and msvc 2005 compiller.
Check your firewall settings.

bb
+1  A: 

A few things would help to debug this for you:

  1. What platform are you running
  2. What compiler are your using, including version
  3. What version of boost are you using

Also, one thing to check is whether the server is binding to 127.0.0.1 or the external interface. Try using the IP address of your external interface instead of 127.0.0.1. Check this in windows using ipconfig and in linux using ifconfig.

MGoDave
Yes, it was binding external interface...thanks
Milan
A: 

The port option takes a string, which may be the name of the service, as "daytime", and then it will look up the corresponding port, or explicitly the port, but it must be a string:

tcp::iostream s(argv[1], "8087");

Arthur Ribacki