Boost :: Socket finally compiled.

xiaoxiao2021-03-06  94

Searching Maillist on the Internet, some people say that Boost's socket is almost almost useless (looking forward to a long time). Torting a morning, I finally compiled.

1. Download the Socket library and other files (including the IO library, preferably full down) from the boost's Sandbox CVS (http://cvs.s.sources/boost-sandbox/boost-sandbox/).

2. Put the Socket library COPY to BOOST_1_30_0 / boost / directory (Copy to Boost_1_31_0 but compile, there are many problems, give up), see which libraries or files lacked when compiling, then from Boost-Sandbox / copy.

3. Compile the socket library, http://www.boost.org/more/getting_started.html, 9CBS has a Chinese guide, search. If you lack certain files, you will be copied from Sandbox. It is VC7, afraid that VC6 cannot pass.

4. Write a Client Test and a Server test program. There should be no big problem, and when LINK should add WS2_32.LIB and libboost_socket.lib.

Client test programs, VC7 console programs.

#include "stdafx.h"

#include "boost / socket / ip4.hpp" #include "boost / socket / socketStream.hpp" #include "boost / socket / socket_exception.hpp" #include "boost / socket / connection_socket.hpp"

Using Namespace Boost :: Socket;

INT_Tmain (int Argc, _tchar * argv []) {Try {ip4 :: address addr ("127.0.0.1", 3234);

TypedEf Connector <> Connector_t; typedef connection_t :: data_connection_t data_connection_t;

Connector_t connector; data_connection_t socket; connector.connect (socket, ip4 :: tcp_protocol (), addr; Basic_Socket_Stream

S (socket);

S << "Hello! Hello! Hello! Hello! Hello!" << std :: endl;

Std :: string response;

Std :: getLine (s, response);

Std :: cout << Response;

Socket.close ();

}

Catch (const socket_exception&)

{

Std :: CERR << S.Message () << std :: endl;

}

Return 0;}

/ ************************************************** *********************************************************** *********************

Server-side test programs, VC7 console programs (from Sandbox's examples Copy, it seems to have some small problems, change a little bit).

*********************************************************** *********************************************************** ********************** / # incrude "stdafx.h" #include "ssteam.h"

#include "Boost / Socket / IP4.HPP" #include "Boost / Socket / SocketStream.hpp" #include "Boost / Socket / Acceptor_Socket.hpp" #include "boost / socket / socket_set.hpp" #include "boost / socket /address_info.hpp"#include "boost / lexical_cast.hpp"

#include "boost / shared_ptr.hpp" #include "boost / bind.hpp"

#include "map"

#include "algorithm"

#include "vector"

// # include "Boost / Test / Unit_Test.HPP"

Using Namespace Boost :: Socket;

TypedEf Boost :: Shared_Ptr

<>> Socketptr;

Typedef std :: map

CLIENTS;

Struct socket_equal: public std :: binary_function

Socket_base <> :: socket_t,

Bool>

{

Bool Operator () (Const Clients :: Value_type & P,

Socket_base <> :: socket_t s) const

{

Return P.first-> Socket () == s;

}

}

Void server_test () {Try {std :: cout << "starting" <

Clients clients;

IP4 :: Address Addr; Addr.Port (3234); Addr.ip ("127.0.0.1");

IP4 :: TCP_PROTOCOL Protocol;

Option :: non_blocking non_block (false);

TypedEf Acceptor_Socket <> acceptor_socket_t; typedef acceptor_socket_t :: data_connection_t data_connection_t;

Acceptor_socket_t listening_socket; listening_socket.open (Protocol, Addr, 6);

Boost :: Socket :: Socket_set Master_Set; Master_St.Insert (Listening_socket.sock ());

While (True) {boost :: socket :: socket_set active_set; active_set = master_set

Std :: cout << "Selecting" <

IF (select (active_set.width (), active_set.fdset (), 0, 0, 0) == - 1) {throw "unExpected select result";} const boost :: socket :: socket_set :: item tnd = Active_set ();

For (Boost :: Socket :: Socket_Set :: Iterator i = Active_Set.Begin (); i! = i_nd; i) {if (* i == listenging_socket.socket ()) {//! This is Our Acceptor socket while (true) {ip4 :: address client; SocketPtr accepted_socket (new data_connection_t); int ret = listening_socket.accept (* accepted_socket, client); if (ret == Success) {accepted_socket-> ioctl (non_block); master_set. INSERT ()); std :: cout << "accepted client" << client.ip () << ":" << client.port () << std :: endl; std :: string Port; // = boost :: lexical_cast

(Client.Port ());

Std :: stringstream ss;

SS <

Port = ss.str ();

address_info_list addr_info (client.ip () c_str (), port.c_str (), address_info_list :: canonname.); address_info_list :: iterator iend = addr_info.end (); for (address_info_list :: iterator i = addr_info.begin () ; I! = IEND; i) std :: cout << I-> hostname () << std :: end1

Std :: cout << Accepted_socket-> socket () <

Clients.insert (std :: make_pair (attcept_socket, client));

Else Break;}} else {// tres} else {// Thase Are Our data Socket std :: cout << "Receiving data" <

Std :: string Str;

Clients :: itrator client = std :: find_if (clients.begin (), clients.end (), boost :: bind

(socket_equal (), _ 1, * i));

IP4 :: Address & Client_addr = Client-> Second; std :: cout << client_addr.to_string (); boost :: socket :: basic_socket_streams (* client-> first);

While (! ss.eof () &&! ss.fail ()) {ss >> str; ss << "Server: i Have Recieved Your Hello!" <

IF (! ss.fail ())

Std :: cout <

<

}

IF (! client-> first-> is_open ())

Std :: Cout << "Socket Closed" <

IF (ss.eof ()) {// reading from socket surced.rase (* i); clients.rase (client);}}}

}}} Cat :: cerr << Endl; throw;}}

INT _TMAIN (int Argc, _tchar * argv []) {server_test (); return 0;}

<> <> <> <

<>

<> <>

转载请注明原文地址:https://www.9cbs.com/read-123603.html

New Post(0)