5

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 ?

Templar
  • 1,694
  • 1
  • 14
  • 32
  • Please, clarify, who sends what to whom. Esp. *On A's side there is a Safari, making requests to A* is unclear to me. – Amin Negm-Awad Jul 06 '16 at 11:07
  • @AminNegm-Awad It meant, that to port A a Safari makes requests. – Templar Jul 14 '16 at 08:58
  • Anyway, I gave up on this. It is way too complicated to implement something like this. I made only a HTTP proxy and modified some headers to know exactly which client sends which request so I can send them back the correct response. – Templar Jul 14 '16 at 08:59

0 Answers0