[Home]BoostSocket/ClientExample

BOOST WIKI | BoostSocket | RecentChanges | Preferences | Page List | Links List

Difference (from prior major revision) (no other diffs)

Added: 111a112,113

[buy fioricet online] [buy fioricet] [[buy fioricet online]]

Socket Client Example

  ip4::address addr;
  addr.hostname("localhost");
  addr.port(3234);

  ip4::tcp_protocol protocol;

  data_socket socket(connect(protocol,addr));

  socket.setsockopt(boost::socket::socket_option_linger(1000));

  boost::socket::basic_socket_stream<char> s(socket);
  for (int k=0;k<10;++k)
  {
    s << "hello\n";
    s << "hello ";
    s.flush();
  }

  socket.close();

Some questions

Did I miss the summary of what this example does? [I think it sends the text "hello\n hello " to port 3234 of the same computer 10 times, but it is always good to double-check I have read an example as intended] -- Correct :-)

Is there a reason socket_base::initialise/finalise are not wrapped in a RAII object? -- it is now!

Don't you think that everyone (as they do now) is going to write a wrapper for this library that acts like the following? --DJS camio@yahoo.com

 int main()
 {
   try
   {
     TCPConnection con( "localhost",3234 );
     con << "Hello how are you?" << std::endl;
     std::string response;
     std::getline( con, response );
     cout << response << endl;
   }  //con closes on disconnect
   catch ( ... )
   {
     cout << "There was some kind of error" << std::endl;
   }
 }

Sorry don't understand what you are asking - Hugo

I was merely pointing out that the simplicity of the code I wrote outweights the flexibility of the code you wrote in 99% of useage. --DJS

Here comes a similar remark: Why not give a COMPLETE and simple example? This will help me and other interested in a boost socket library to get a kickstart. I mean something like:

 #include "whichfiles?"

 int main()
 {
     boost::socket::ip4::address addr("localhost",3234);
     ...
     ...
     boost::socket::basic_socket_stream<char> sout(socket);
     sout << "Hello World!" << endl;
     std::string reply;
     sin >> reply;
     std::cout << reply;
 }

=== The following compiles with the current version in the sandbox

 #include "boost/socket/ip4.hpp"
 #include "boost/socket/socketstream.hpp"
 #include "boost/socket/socket_exception.hpp"
 #include "boost/socket/connector_socket.hpp"

 using namespace boost::socket;

 void main()
 {
  try
  {
    ip4::address addr("127.0.0.1", 3234);

    typedef connector<> connector_t;
    typedef connector_t::data_connection_t data_connection_t;

    connector_t connector;
    data_connection_t socket;
    connector.connect(socket, ip4::tcp_protocol(), addr);
    boost::socket::basic_socket_stream<char> s(socket);
    s << "Hello!" << std::endl;
    std::string response;
    std::getline(s, response);
    std::cout << response;
    socket.close();
  }
  catch (const socket_exception& s)
  {
    std::cerr << s.message() << std::endl;
  }
 }

But not the version on sourceforge/giallo. The file socketstream.hpp doesn't exist, nor is basic_socket_stream declared or referenced anywhere in the files. What happened? - Dan (dan@eloff.info)

I also have problems with compiling this example - I'm using boost-sandbox/boost::sockets (27.II.2005), Janek

[buy lipitor online] [buy lipitor] [[buy lipitor online]]

[buy fioricet online] [buy fioricet] [[buy fioricet online]]


BOOST WIKI | BoostSocket | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited August 24, 2008 1:00 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers