// // // // // Lab. Calcolo II - Esempio di codice // // // // // // // // Example code: socket connect and send demonstration (uses C // socket lib and shelper calls) // (20060915 francesco.prelz@mi.infn.it) #include <iostream> #include <string> #include "netstream.h" int main (int argc, char *argv[]) { int dest_port=1317; const char *dest_host = "127.0.0.1"; std::string msg = "Hello from "; msg += argv[0]; std::cout << "Connecting to " << dest_host << ":" << dest_port << "..." << std::endl; onetstream s(dest_host, dest_port); if (s.good()) { s << msg; if (s.good()) { std::cout << "Sent message: \"" << msg << "\"" << std::endl; } else { std::cerr << "Error sending message." << std::endl; // It would certainly be nice to know why... } } else { std::cerr << "Error connecting." << std::endl; // It would certainly be nice to know why... } s.disconnect(); return 1; }