Good day!
CommonCPP 1.2.4
OS: Windows XP
I want to write small program, which wait connection on specific port,
and on accepting connectin write to console the IP-address and port,
from what the connection come.
For listen socket i'm using ost::TCPSocket class.
And my questions is how can i determine the source IP-address of the
connection without produce a new classes?
This is a example:
==========================================================
#include <iostream>
#include <cc++/socket.h>
using namespace std;
using namespace ost;
void acceptConnections(TCPSocket *a_listenSocket){
while(true)
{
try{
while(a_listenSocket->isPendingConnection(50))
{
tpport_t pp;
TCPStream *stream = new
TCPStream(*a_listenSocket);
// Socket *s = (Socket *)stream;
// InetHostAddress addr = s->getSender(&pp);
InetHostAddress addr =
a_listenSocket->getRequest(&pp);
cout << "Connection from " << inet_ntoa(addr.getAddress()) <<
":" << pp << endl;
// Some data exhange...
delete stream;
}
}catch (Socket *ex) {
cout<<"Exception while accept connections:" <<
ex->getErrorString() <<endl;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
TCPSocket *listenSocket = 0;
int port = 4455;
try
{
listenSocket = new TCPSocket("0.0.0.0", port);
acceptConnections(listenSocket);
delete listenSocket;
}
catch(Socket *socket)
{
cout << "Exception in program:" << socket->getErrorString() <<
endl;
}
return 0;
}
==========================================================
This code cause WSAError 10057 (Not connected) ("A request to send or receive
data was
disallowed because the socket is not connected and (when sending on a
datagram socket using a sendto call) no address was supplied.")
in TCPSocket::getRequest method.
Get source IP-address from TCPStream also imposible, because getSender
method is a stub, and private. (What the reason for make this useful
method private?)
And if i try to cast TCPSocket to Socket, which has mehod getSender,
the returned result is a garbage. (seems returns uninitialized value).
Summary:
How can i determine connection's source IP-adress using TCPSocket?
Why TCPStream::getSender is stub and private?