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 06:53:18 -0400
User-agent: Mozilla Thunderbird 0.7.3 (X11/20040803)

Hmm...thinking about this....

the better way would be to collect and cache the peer at the time the accept call itself is being made.

I am not sure why getSender in TCPStream was done that way offhand, either. It may be a left-over from a time when there was no getSender method in the Socket base class and was never updated to complete the implimentation.

I will do some testing of solutions for both of these issues, probably later today. It's easy enough to setup a test solution around the demo tcp.cpp, and once a working solution is found with appropriate library changes, it will be easy to see how to use it from said demo. More than likely I will make getSender method work from TCPStream, and then look at the accept issues seperately.

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]