I'm using CocoaAsyncSocket trying to create a TCP-tunnel/mux/demux, forwarding all connections through a port pair (A <-> B). Everything written to port A should "come out" of port B and vice versa. I can use only ports A and B, can't open other ports.
I'm creating 2 listening AsyncSockets
on A and B and have 2 arrays of the 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];
}
everything I read on port A I will send to all clients of port B and vice versa.
On A's side there is a Safari, making requests to A.
My problem is : When data comes back from B port, and on A's side I have 5 clients ( thank you Safari ...), I won't know which one initially requested the current data packet, so I'm sending data to all of them and...well... everything is messed up.
Can this mux/demux be achieved this way at all ? Or what is the correct way to achieve this ? How could I differentiate the initiating clients ?