Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
util/liblinebasedcomms/tcp_client.cpp
00001 // copyright 2009 t. schneider tes@mit.edu
00002 // 
00003 // this file is part of comms, a library for handling various communications.
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This software is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #include "goby/util/string.h"
00019 
00020 #include "tcp_client.h"
00021 
00022 goby::util::TCPClient::TCPClient(const std::string& server,
00023                                  unsigned port,
00024                                  const std::string& delimiter /*= "\r\n"*/)
00025     : LineBasedClient<boost::asio::ip::tcp::socket>(delimiter),
00026       socket_(io_service_),
00027       server_(server),
00028       port_(as<std::string>(port))
00029 { }
00030 
00031 
00032 bool goby::util::TCPClient::start_specific()
00033 {
00034     boost::asio::ip::tcp::resolver resolver(io_service_);
00035     boost::asio::ip::tcp::resolver::query query(server_, port_);
00036     boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
00037     boost::asio::ip::tcp::resolver::iterator end;
00038 
00039     boost::system::error_code error = boost::asio::error::host_not_found;
00040     while (error && endpoint_iterator != end)
00041     {
00042         socket_.close();
00043         socket_.connect(*endpoint_iterator++, error);
00044     }
00045     
00046     return error ? false : true;
00047 }
00048 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends