TCP over internet

I’m having trouble using TCP over the internet. I can create connections on the localhost or over LAN, but I get ‘Unable to open TCP connection’ errors when the two machines are not local. Is this a port forwarding issue? The server has its port forwarded, and I don’t want to have to make every client forward its port. (UDP transmission works fine for me between non-local machines).

This is the code I’m using.

from direct.showbase.ShowBase import ShowBase
from panda3d.core import WindowProperties
from pandac.PandaModules import QueuedConnectionManager, QueuedConnectionReader, ConnectionWriter, NetAddress, NetDatagram, QueuedConnectionListener, PointerToConnection
from direct.distributed.PyDatagram import PyDatagram

PORT = 5555

class Client():
    
    def __init__(self):
        self.cManager = QueuedConnectionManager()
        self.tcpWriter = ConnectionWriter(self.cManager,0)
        self.tcpReader = QueuedConnectionReader(self.cManager, 0)
        taskMgr.add(self.tskReaderPolling,"Poll the connection reader",-40)
        
        # This fails
        self.conn = self.cManager.openTCPClientConnection('68.59.144.180', PORT, 1000)
        if self.conn:
            self.tcpReader.addConnection(self.conn)
            
            self.SendPacket()
            
    def SendPacket(self):
        dg = PyDatagram()
        dg.addUint8(5)
        self.tcpWriter.send(dg, self.conn)
            
    def tskReaderPolling(self, task):
        if self.tcpReader.dataAvailable():
            datagram = NetDatagram()
            if self.tcpReader.getData(datagram):
                print 'client got data'
        return task.cont
        
class Server():
    
    def __init__(self):
        self.cManager = QueuedConnectionManager()
        self.tcpSocket = self.cManager.openTCPServerRendezvous(PORT, 1000)
        self.tcpWriter = ConnectionWriter(self.cManager,0)
        self.tcpListener = QueuedConnectionListener(self.cManager, 0)
        self.tcpListener.addConnection(self.tcpSocket)
        self.tcpReader = QueuedConnectionReader(self.cManager, 0)
        
        taskMgr.add(self.tskListenerPolling,"Poll the connection listener",-39)
        taskMgr.add(self.tskReaderPolling,"Poll the connection reader",-40)
        
    def SendPacket(self):
        dg = PyDatagram()
        dg.addUint8(5)
        self.tcpWriter.send(dg, self.conn)
        
    def tskReaderPolling(self, task):
        if self.tcpReader.dataAvailable():
            datagram = NetDatagram()
            if self.tcpReader.getData(datagram):
                print 'server got data'
                self.SendPacket()
        return task.cont
    
    def tskListenerPolling(self, task):
        if self.tcpListener.newConnectionAvailable():
            rendezvous = PointerToConnection()
            netAddress = NetAddress()
            newConnection = PointerToConnection()
        
            if self.tcpListener.getNewConnection(rendezvous, netAddress, newConnection):
                newConnection = newConnection.p()
                self.tcpReader.addConnection(newConnection)
                self.conn = newConnection
        return task.cont



  
class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        wp = WindowProperties()
        wp.setSize(850, 480)
        wp.setTitle("GAME")
        base.win.requestProperties(wp)
        
        c = Client()
        #s = Server()

app = MyApp() 
app.run()

I run one as the client and another as the server. It works if the client connects to localhost (i.e. server and client on same machine) or if a machine on my router connects using the router IP (e.g. 192.168.1.1), but it doesn’t work if I try to connect from another machine using my global IP

The error is:
Unable to open TCP connection to server 68.59.144.180 on port 5555

not a network expert, but that sounds like your router is not forwarding port 5555. by default, most routers reject incoming packages on port from outside the LAN.

if it works with localhost, your code most likely is correct.

That was exactly it, even though I had already thought about it. I was forwarding the port using the option ‘TCP/UDP’, which failed, but when I switched it to TCP only, it worked.