B4J Question TCP Socket over internet

PatrikCavina

Active Member
Licensed User
Hi to everyone,
I'm using jNetwork library to connect my client app to my server app.
In a LAN connection between client e server works but over internet the connection isn't established.
Using this site: http://checkip.dyndns.com/, i saw what was my ip over internet.
-This is client part
B4X:
Sub connect_MouseClicked (EventData As MouseEvent)
    ReceiverSckt.Initialize("ReceiverSocket")
    If ReceiverSckt.Connected Or ipAddress.Text = "" Or port.Text = ""  Then Return
    Try
        ip = ipAddress.Text
        prt = port.Text
        ReceiverSckt.Connect(ip,prt,0)
        Wait For ReceiverSocket_Connected(successful As Boolean)
        If successful Then
            Stream.InitializePrefix(ReceiverSckt.InputStream,True,ReceiverSckt.OutputStream,"Stream")
        Else
            fx.Msgbox(MainForm,"Connection refused","Connection")
        End If
    Catch
        fx.Msgbox(MainForm,"Connection refused","Connection")
    End Try
End Sub

Sub disconnect_MouseClicked (EventData As MouseEvent)
    If Not(ReceiverSckt.Connected) Then Return
    Stream.Close
    ReceiverSckt.Close
End Sub

This server part:
B4X:
Sub AppStart (Args() As String)
    Try
        poolConnection.Initialize
        SenderSckt.Initialize(port,"SenderSocket")
        SenderSckt.Listen
    Catch
        Log(LastException)
    End Try
    Log("Server IP: "&SenderSckt.GetMyIP)
    Log("Ready to send")
    Log("Listening...")
    StartMessageLoop
End Sub

Private Sub SenderSocket_NewConnection(Successful As Boolean,NewSocket As Socket)
    If Not(Successful) Then
        Log("Unsuccessful connection from: "&NewSocket.RemoteAddress)
        Return
    End If
    Log("New connection from: "&NewSocket.RemoteAddress)
    poolConnection.Put(newStream,NewSocket.RemoteAddress)
    SenderSckt.Listen
End Sub

When i try to connect over internet i recive this exception:
Waiting for debugger to connect...
Program started.
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at anywheresoftware.b4a.objects.SocketWrapper$1.run(SocketWrapper.java:140)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

I use port 6881.
I opened that port in firewall also.
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
is it your server self hosted? if that is so there are a number of reasons that could throw this error:

some of which i have encountered:

1.- Your ISP is allocating more than one customer with the same public ip.

2.- you have to configure your modem (open port there too) with redirecting your public ip to specifically your computer.

3. If you already did that, you have to disable dhcp for your server because the modem will not redirect it correctly.
 
Upvote 0
Top