B4J Question Socket problem

wl

Well-Known Member
Licensed User
Longtime 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
Longtime 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.
 
Upvote 0

jerry07

Member
Licensed User
Longtime 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,

I know this is very old post but I have 2 questions that are about this post so this is why I thought it was good place to ask instead starting new topic.

1. Did you update this code with working solution? Reading conversation between you and Erel it seems that you follow his advice and this code may have been updated?

2. Can you please describe little more your case scenerio that you were trying to solve and did you actually solve it? Was the attempt to have 2 applications CloudStation and B4J run on same external port 6690 and have communication redirected to internal port 6691 for CloudStation and internal port 6690 for B4J app?

Thank you.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I thought it was good place to ask instead starting new topic
It is never the correct place if the thread is old.

You should always create a new thread in the questionsforum for any Issue you have.
 
Upvote 0
Top