Network Client Identification

I am using networking code based on the samples in this thread:

discourse.panda3d.org/viewtopic.php?t=4881

I have multiple clients connecting now, and I need to identify which client is sending the data. I also have instances (such as private messages) where data needs to be sent to a specific target.

How can I identify which client is sending data? As a first attempt, I have each client, upon connecting, request a clientID number. When the request comes in, I need to make sure that the response goes out to only the client that is requesting. How can I do this.

I tried inserting the print datagram.getConnection() line as shown towards the bottom of the thread, but nothing gets printed. I tried it in a couple of different places with no luck.

My networking may be a bit rusty, but if the client connects at port 9099, doesn’t the server negotiate a new port with the client? At that point, is there any way to reference the port in question? Or get a handle to that communication stream?

Thanks!

Just as an update, I went digging in the NetDatagram.h file, which I assume is where panda is getting this class from. I am still unable to determine how to extract any information on which client I am getting my data from.

Any further help would be appreciated.

In Server.py:

    def getData(self):
        data = []
        while self.cReader.dataAvailable():
            datagram = NetDatagram()  # catch the incoming data in this instance
            # Check the return value; if we were threaded, someone else could have
            # snagged this data before we did
            if self.cReader.getData(datagram):
                print datagram.getAddress()
                data.append(self.processData(datagram))
        return data

After self.cReader.getData, you can get the client ip address by datagram.getAddress. Does it help ?

I did something very similar. I defined self.recentConnection = ’ ’ in server.py, then, in server.py i did this:


def getData(self):
        data = []
        while self.cReader.dataAvailable():
            datagram = NetDatagram()  # catch the incoming data in this instance
            # Check the return value; if we were threaded, someone else could have
            # snagged this data before we did
            if self.cReader.getData(datagram):
                data.append(self.processData(datagram))

            self.recentConnection = str(datagram.getAddress())

        return data

in my main routine, then, i use:


        self.server = sfServer.Server(9099, compress=True)

and later:


        for data in self.server.getData():

            data = dict(data)

            print self.server.recentConnection

which gives me the IP address.

Now, the next step is to find out how to turn the IP address that I have into the connection that sendData is going to want to have. That is what I will work on next!

Thanks for the help so far…

Note that your process may not be able to process the data fast enough. If in the mean time some client data is sent over, your self.recentConnection will be override by the next message.

I suggest you pack the address with the data to avoid the problem:
data.append((address, self.processData(datagram)))