B4J Question Socket problem

wl

Well-Known Member
Licensed User
Hi,

I'm trying to write a local reverse proxy. What it does is:

- accept traffic on a local port
- send the data back and forth between the local port and a remote port

eg: A synology NAS Cloudstation client only works on port 6690 but I would like to be running my local B4J application that accepts data on my local port 6690 (so the cloudstation can connect to localhost on port 6690) and communicate with the external synology server on port 6691

Below is the code. It just does not seem to work (communication halts very soon). The code currently only works for a single connection but it seems the protocol is only using a single socket (as soon as I can resolve my issue I would make it multi-connection proof of course)

Thanks for any suggestions !!

B4X:
Sub AppStart (Args() As String)
    client.Initialize("client") 'Socket
    client.Connect("my.server.com", 6691, 0)

    server.Initialize(6690, "server") 'ServerSocket
    server.Listen

    StartMessageLoop
End Sub

Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
        streams.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "streams") 'AsyncStreams
        tw = NewSocket.OutputStream
    End If
End Sub

Sub client_Connected (Successful As Boolean)
    If Successful Then
        streams2.Initialize(client.InputStream, client.OutputStream, "streams2")
        tw2 = client.OutputStream
    End If
End Sub

Sub streams_NewData (Buffer() As Byte)
    tw2.WriteBytes(Buffer, 0, Buffer.Length)
    tw2.Flush
End Sub

Sub streams2_NewData (Buffer() As Byte)
    tw.WriteBytes(Buffer, 0, Buffer.Length)
    tw.Flush
End Sub
 

wl

Well-Known Member
Licensed User
Thanks Erel.

I initially used this but then I noticed I can't flush the streams so I thought this could be case and used this method.
 
Top