bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Imposible to determine source IP in TCPStream & TCPSocket?


From: David Sugar
Subject: Re: Imposible to determine source IP in TCPStream & TCPSocket?
Date: Thu, 26 Aug 2004 07:34:22 -0400
User-agent: Mozilla Thunderbird 0.7.3 (X11/20040803)

From the TCPStream did you try the getPeer methods?

Andrey Kulikov wrote:
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?





reply via email to

[Prev in Thread] Current Thread [Next in Thread]