我有实现 Poco SocketReactor 的代码,如 Echo 示例中所述。
class TSPConnectionHandler
{
public:
TSPConnectionHandler(const StreamSocket& socket, SocketReactor& reactor) : _socket(socket),
_reactor(reactor)
{
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ReadableNotification>(*this, &TSPConnectionHandler::onSocketReadable));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, WritableNotification>(*this, &TSPConnectionHandler::onSocketWritable));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ShutdownNotification>(*this, &TSPConnectionHandler::onSocketShutdown));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ErrorNotification>(*this, &TSPConnectionHandler::onSocketError));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, TimeoutNotification>(*this, &TSPConnectionHandler::onSocketTimeout));
}
virtual ~TSPConnectionHandler()
{
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ReadableNotification>(*this, &TSPConnectionHandler::onSocketReadable));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, WritableNotification>(*this, &TSPConnectionHandler::onSocketWritable));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ShutdownNotification>(*this, &TSPConnectionHandler::onSocketShutdown));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ErrorNotification>(*this, &TSPConnectionHandler::onSocketError));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, TimeoutNotification>(*this, &TSPConnectionHandler::onSocketTimeout));
}
void onSocketReadable(const Poco::AutoPtr<ReadableNotification>& pNf)
{
cout << "READable !!" << endl;
try
{
vector<char> m_buffer;
m_buffer.resize(1024, '\0');
LONG32 m_buflen = _socket.receiveBytes(&m_buffer[0], 1024);
if (m_buflen == 0)
{
cout << "Connection reset by peer normally" << endl;
delete this;
}
_socket.sendBytes(&m_buffer[0], m_buflen);
}
catch(Poco::Net::NetException& e)
{
cout << "socket read exception: " << e.displayText() << endl;
delete this;
}
}
void onSocketWritable(const Poco::AutoPtr<WritableNotification>& pNf)
{
cout << "WRITEable !!" << endl;
}
void onSocketShutdown(const Poco::AutoPtr<ShutdownNotification>& pNf)
{
cout << "SHUTDOWN!!!!!!!!!!!!" << endl;
delete(this);
}
void onSocketError(const Poco::AutoPtr<ErrorNotification>& pNf)
{
cout << "Error!!" << endl;
}
void onSocketTimeout(const Poco::AutoPtr<TimeoutNotification>& pNf)
{
cout << "Timeout!!" << endl;
}
private:
StreamSocket _socket;
SocketReactor& _reactor;
};
它使用以下代码在程序的其他地方正常启动:
Poco::Net::ServerSocket tcpsock("9495");
Poco::Net::SocketReactor reactor;
Poco::Net::SocketAcceptor<TSPConnectionHandler> acceptor(tcpsock, reactor);
Poco::Thread thread;
thread.start(reactor);
waitForTerminationRequest();
reactor.stop();
thread.join();
return Application::EXIT_OK;
我也有python客户端来测试它:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 9495))
data = raw_input ( "Something:" )
client_socket.send(data)
data = client_socket.recv(1024)
print "RECIEVED:" , data
client_socket.close()
我得到了我不明白如何解决的问题列表:
- python字符串“client_socket.connect(('localhost', 9495))”完成后,服务器开始发送垃圾邮件“onSocketWritable”,如何阻止它?我知道如果套接字变得可写,我必须得到通知,但是为什么它会在套接字的整个生命周期中继续这样做呢?如果那是正常情况,WritableNotification 是为了什么而设计的?
- 如果我在调试模式下启动 python 并在“client_socket.close()”之前关闭它,服务器会得到异常并将被删除抛出“socket read exception:”。但不会发送errornotification 和shutdownnotification。为什么?反正我从来没见过他们派人。
- 如果我让“client_socket.close()”完成,在服务器端我将收到 ReadableNotification 并将抛出“正常由对等方重置连接”。但仍然不会有 ShutdownNotification。为什么?