00001 #include <iostream> 00002 #include <exception> 00003 #include <string> 00004 00005 #include <socklibpp/sockstream.h> 00006 00007 using namespace std; 00008 using namespace socklibpp; 00009 00010 int main() 00011 { 00012 addr_in local(addr_in::addr_any, 0); 00013 local.port("echo"); 00014 00015 sock server; 00016 server.create(sock::pf_inet, sock::sock_stream); 00017 00018 if (!server.bind(local)) { 00019 cerr << "Could not bind: " << server.error << endl; 00020 return 1; 00021 } 00022 if (!server.listen()) { 00023 cerr << "Could not listen: " << server.error << endl; 00024 return 1; 00025 } 00026 00027 sock client; 00028 string line; 00029 while (true) { 00030 client = server.accept(); 00031 00032 sockstream con(client); 00033 con.exceptions(ios_base::eofbit); 00034 00035 try { 00036 while (getline(con, line)) 00037 con << line << crlf; 00038 } 00039 catch (sockstream::failure) { 00040 cerr << "Lost connection" << endl; 00041 } 00042 } 00043 }