mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-01-19 12:07:13 +01:00
4a6bfc086c
Switched to POCO library for unified platform/library interface. Deprecated the external module API. It was creating more problems than solving. Removed most built-in libraries in favor of system libraries for easier maintenance. Cleaned and secured code with help from static analyzers.
103 lines
2.2 KiB
C++
103 lines
2.2 KiB
C++
//
|
|
// tcpserver.cpp
|
|
//
|
|
// This sample demonstrates a TCP server.
|
|
//
|
|
// Copyright (c) 2005-2018, Applied Informatics Software Engineering GmbH.
|
|
// and Contributors.
|
|
//
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
//
|
|
|
|
|
|
#include "Poco/Net/TCPServer.h"
|
|
#include "Poco/Net/TCPServerConnection.h"
|
|
#include "Poco/Net/TCPServerConnectionFactory.h"
|
|
#include "Poco/Net/StreamSocket.h"
|
|
#include "Poco/NumberParser.h"
|
|
#include "Poco/Logger.h"
|
|
#include "Poco/Process.h"
|
|
#include "Poco/NamedEvent.h"
|
|
#include <iostream>
|
|
|
|
|
|
using Poco::Net::TCPServer;
|
|
using Poco::Net::TCPServerConnectionFilter;
|
|
using Poco::Net::TCPServerConnection;
|
|
using Poco::Net::TCPServerConnectionFactory;
|
|
using Poco::Net::TCPServerConnectionFactoryImpl;
|
|
using Poco::Net::StreamSocket;
|
|
using Poco::UInt16;
|
|
using Poco::NumberParser;
|
|
using Poco::Logger;
|
|
using Poco::Event;
|
|
using Poco::NamedEvent;
|
|
using Poco::Process;
|
|
using Poco::ProcessImpl;
|
|
using Poco::Exception;
|
|
|
|
|
|
namespace
|
|
{
|
|
class ClientConnection: public TCPServerConnection
|
|
{
|
|
public:
|
|
ClientConnection(const StreamSocket& s): TCPServerConnection(s)
|
|
{
|
|
}
|
|
|
|
void run()
|
|
{
|
|
StreamSocket& ss = socket();
|
|
try
|
|
{
|
|
char buffer[256];
|
|
int n = ss.receiveBytes(buffer, sizeof(buffer));
|
|
while (n > 0)
|
|
{
|
|
std::cout << "Received " << n << " bytes:" << std::endl;
|
|
std::string msg;
|
|
Logger::formatDump(msg, buffer, n);
|
|
std::cout << msg << std::endl;
|
|
n = ss.receiveBytes(buffer, sizeof(buffer));
|
|
}
|
|
}
|
|
catch (Exception& exc)
|
|
{
|
|
std::cerr << "ClientConnection: " << exc.displayText() << std::endl;
|
|
}
|
|
}
|
|
};
|
|
|
|
typedef TCPServerConnectionFactoryImpl<ClientConnection> TCPFactory;
|
|
#if defined(POCO_OS_FAMILY_WINDOWS)
|
|
NamedEvent terminator(ProcessImpl::terminationEventName(Process::id()));
|
|
#else
|
|
Event terminator;
|
|
#endif
|
|
}
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
try
|
|
{
|
|
Poco::UInt16 port = NumberParser::parse((argc > 1) ? argv[1] : "2001");
|
|
|
|
TCPServer srv(new TCPFactory(), port);
|
|
srv.start();
|
|
|
|
std::cout << "TCP server listening on port " << port << '.'
|
|
<< std::endl << "Press Ctrl-C to quit." << std::endl;
|
|
|
|
terminator.wait();
|
|
}
|
|
catch (Exception& exc)
|
|
{
|
|
std::cerr << exc.displayText() << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|