From: Brian Candler Date: 2009-11-20T19:17:28+09:00 Subject: Re: UDP Proxy Randy General wrote: > I was able to sort out how to send from the same port I'm listening on > and condensed the code down to the following: > > http://pastie.org/707376.txt This code looks very strange to me. It handles the first received packet in one way then the second received packet in a different way, then alternates between these two ways. If the protocol requires handling in this way then if you ever lost one packet then it would get out of sync. I suspect what you're trying to do is handle a --> proxy --> b and a <-- proxy <-- b in which case you need to be able to distinguish these two cases when you receive the packet, not by the sequencing. It also seems strange to me your requirement that if the proxy is listening on port X, then it must also send with a source port of X. That's not normally a requirement for a proxy, but then you haven't really said much about the protocol itself. Does the destination really care about the source port which the request comes from? Normally a client picks a random source port >=1024, so the only thing which matters is the destination port: r1 X r2 X a ------> proxy -------> b where r1 and r2 are randomly-chosen ports for the lifetime of the socket. Anyway, in your code, life would be much easier if you bound two different sockets (either on different ports, or on different IPs). X Y a ------> proxy -------> b because you could select() on the two sockets, and easily distinguish outbound from return packets. You also said you need to support multiple concurrent clients. So you need, for each return packet, to know which client to send it to. The easiest way of doing this would be to bind a new socket for each client, on a different (dynamic) port. > But it's still unable to handle proxying a connection to a FPS server, > while the C implementation can. I suggest you look at tcpdump output of the two programs, to see how the sent packets differ in each case. -- Posted via http://www.ruby-forum.com/.