// // // // // Lab. Calcolo II - Esempio di codice // // // // // // // // Example code: socket listen and receive demonstration (uses C // socket lib and shelper calls) // (20060914 francesco.prelz@mi.infn.it) #include <iostream> #include <string> #include "netstream.h" int main (int argc, char *argv[]) { int port=1317; std::cout << "Listening for connections..." << std::endl; inetstream s("localhost", port); if (s.good()) { std::string msg; while (s.good()) // The message will be terminated by the client disappearing... { std::string snippet; if (s.good()) { s >> snippet; if (! msg.empty() && ! snippet.empty() ) msg += " "; msg += snippet; } } if (!msg.empty()) { std::cout << "Received message: \"" << msg << "\"" << std::endl; } else { std::cerr << "Error receiving message or empty message." << std::endl; // It would certainly be nice to know why... } } s.disconnect(); return 1; }