// LICENSE: (Please see the file COPYING for details) // // NUS - Nemesis Utilities System: A C++ application development framework // Copyright (C) 2006, 2008 Otavio Rodolfo Piske // // This file is part of NUS // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation version 2.1 // of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #ifndef CLIENT_APP_H #define CLIENT_APP_H #include "ndefs.h" #include "nobject.h" #include "nopt.h" #include "ntcpsocket.h" #include "nstring.h" #include "noutput.h" class ClientApp: public NObject { public: ClientApp(void); bool setHost(const NOpt<ClientApp> &opt); bool setPort(const NOpt<ClientApp> &opt); void run(void); private: NString m_host; nuint16 m_port; NTcpSocket m_sock; }; #endif // CLIENT_APP_H
// LICENSE: (Please see the file COPYING for details) // // NUS - Nemesis Utilities System: A C++ application development framework // Copyright (C) 2006, 2008 Otavio Rodolfo Piske // // This file is part of NUS // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation version 2.1 // of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #include "serverapp.h" ServerApp::ServerApp(void) : m_port(0), m_host(), m_sock() { } bool ServerApp::setPort(const NOpt<ServerApp> &opt) { NString optVal; optVal = opt.getValues().at(0); m_port = atoi(optVal.toChar()); m_sock.setPort(m_port); return true; } bool ServerApp::setHost(const NOpt<ServerApp> &opt) { m_host = opt.getValues().at(0); m_sock.setHost(m_host); return true; } void ServerApp::handleConnection(NTcpSocket *client) { NDataStream data; nint32 bytes = 0; // Once a host connects, display a message client->writeLine("Hi there!"); // Receives data from the host until receives a shutdown // command or until client disconnect do { data = ""; NMessage::print() << "Waiting for read ... "; // Reads data from the client bytes = client->readLine(&data); if (bytes < 0) { break; } else { if (bytes == 0) { continue; } } NWarning::print() << "Read (" << bytes << " bytes): " << data.toString(); if (data.toString() == "shutdown") { // If I use write I should add a \r\n at the end client->write("Good bye!\r\n"); client->disconnectFromHost(); exit(0); } } while (bytes >= 0); // Just in case, disconnect from the host client->disconnectFromHost(); } void ServerApp::run(void) { // Opens the socket m_sock.open(); // Make it reuse the address if possible m_sock.setSocketOptions(NAbstractSocket::REUSE_ADDRESS); // Listens m_sock.execListen(); NMessage::print() << "Server is up an running"; // Starts receiving connections while (true) { NTcpSocket *client = NULL; NMessage::print() << "Waiting for connection"; client = m_sock.waitForConnected(); handleConnection(client); NWarning::print() << "Client disconnected"; // Release memory delete client; } }
// LICENSE: (Please see the file COPYING for details) // // NUS - Nemesis Utilities System: A C++ application development framework // Copyright (C) 2006, 2008 Otavio Rodolfo Piske // // This file is part of NUS // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation version 2.1 // of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #ifndef SERVER_APP_H #define SERVER_APP_H #include "ndefs.h" #include "nobject.h" #include "nabstractsocket.h" #include "ntcpsocket.h" #include "nstring.h" #include "noutput.h" #include "nopt.h" class ServerApp: public NObject { public: ServerApp(void); bool setPort(const NOpt<ServerApp> &opt); bool setHost(const NOpt<ServerApp> &opt); void run(void); private: nuint16 m_port; NString m_host; NTcpSocket m_sock; void handleConnection(NTcpSocket *client); }; #endif // SERVER_APP_H
// LICENSE: (Please see the file COPYING for details) // // NUS - Nemesis Utilities System: A C++ application development framework // Copyright (C) 2006, 2008 Otavio Rodolfo Piske // // This file is part of NUS // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation version 2.1 // of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #include "clientapp.h" ClientApp::ClientApp(void) : m_host(), m_port(0), m_sock() { } bool ClientApp::setHost(const NOpt<ClientApp> &opt) { m_host = opt.getValues().at(0); return true; } bool ClientApp::setPort(const NOpt<ClientApp> &opt) { NString optVal; optVal = opt.getValues().at(0); m_port = atoi(optVal.toChar()); return true; } void ClientApp::run(void) { NDataStream data; int bytes; // Sets the host to connect to m_sock.setHost(m_host); // Sets the port to connect to m_sock.setPort(m_port); try { // Open the socket m_sock.open(); // Connect to the host m_sock.connectToHost(); bytes = m_sock.readLine(&data); if (bytes < 0) { NMessage::print() << "Server disconnected"; exit(1); } else { if (bytes == 0) { NMessage::print() << "Nothing to read"; } else { NMessage::print() << "Read " << bytes << ":"; NMessage::print() << data.toString(); } } // Write something to the socket. Note that you don't need to append // a \r\n at the end of the data. It will be automagically appended to // you. If you don't want that behaviour, use write() instead. m_sock.writeLine("Hello network!"); m_sock.disconnectFromHost(); } catch (NNetworkException &e) { m_sock.disconnectFromHost(); throw; } }