CocoaAsyncSocket TCP Proxy / Tunnel

I am using CocoaAsyncSocket, trying to create TCP-tunnel / mux / demux, forwarding all connections through a pair of ports (A ↔ B). Everything written to port A should β€œexit” from port B and vice versa. I can only use ports A and B, I can not open other ports.

I create 2 AsyncSockets listening on A and B and have 2 arrays of connected clients, and in

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag if (sock.localPort == B.localPort) { for (AsyncSocket * cl in clientsOfA) { [cl writeData:data withTimeout:-1 tag:0]; [cl readDataWithTimeout:-1 tag:0]; } [sock readDataWithTimeout:-1 tag:0]; } - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag if (sock.localPort == B.localPort) { for (AsyncSocket * cl in clientsOfA) { [cl writeData:data withTimeout:-1 tag:0]; [cl readDataWithTimeout:-1 tag:0]; } [sock readDataWithTimeout:-1 tag:0]; } everything that I read on port A, I will send to all clients of port B and vice versa. On the side there is Safari, referring to A.

My problem: When the data is returned from port B, and on side A I have 5 clients (thanks to Safari ...), I won’t know which one originally requested the current data packet, so I send data to all of them and .. ... well ... everything is ruined.

Can this mux / demux be achieved this way at all? Or how to do it right? How can I distinguish initiating clients?

+5
source share

All Articles