I'm trying to setup a Udp connection after a Tcp connection has been made. All of the connection states and control goes through Tcp, but I want the client and server to be able to talk through Udp as well. This is my problem.
The server will have many clients connected to it on separate threads. I have it %100 working fine with just Tcp binded, but I want to use Udp. Because of Udp Hole Punching I'm having a hard time listening for a specific endpoint on Udp because the endpoint can change when communicating with a NAT device.
So, to put the question simply: How do I easily get a "UdpClient" connection going from a TcpClient connection to reference?
Thank you in advance!
There will be no tunnel for UDP, just because you have a successful TCP connection.
You have to establish the UDP connection the same way you got the TCP connection. E.g. if the TCP connection was established from intern to outside you have to do the same with UDP, otherwise the NAT device will not establish a connection for the UDP connection and thus packets from the peer will not be sent to you.
Related
is it possible to have one socket router that would pass incoming sockets to socket servers?
I need this because I want to have multiple servers for handling sockets but only one port for clients to connect to. so if one of the servers goes down, router would send the socket to other healthy socket servers.
Is this even possible and how? or any other solution for my problem?
Yes this is definitely possible, and is used very widespread.
Basically you need to have one TCP server running on that one port you want clients to connect to. Then you can listen for connections. When a connection is established you can reroute packets to other servers (you choose).
Listen on a port with your main server
Accept connections as they come in.
When a connection is opened, read the data in another thread. Take that data and send it to the other server you want to.
Basically, it is a proxy.
I'm trying to make p2p connection using UDP hole punching method, but I always get ICMP packet with Destination unreachable status.
At first, there is created an UDP connection to server:
send = new IPEndPoint(IPAddress.Any, 0);
server = new UdpClient(send);
server.AllowNatTraversal(true);
without any direct connection (no ip, no port, just endpoint on random port).
I send some packets to server with direct server.send([..],[..],serverip,serverport) on server's ip and port and obtain information (remote IP, port used to connect to server) about another peer
Now I'm trying to make hole punch with sending simple UDP packets on peer's IP:PORT using server connection (but again with direct server.send([..], [..], peerip, peerport)). I know that the first packet is always dropped, so I'm sending it 50 in 100ms intervals, while remote peer does same thing.
If I understand UDP hole punching method, sending packet from first peer (A) opens NAT record in A's NAT and it is dropped by B's NAT because of no record in NAT. So when B send packet to A, there is record in A's NAT created with first packet (for B) and packet should be received by B. The NAT record in B's NAT is created. A should send another packet to B successfully.
P.S.: http://nattest.net.in.tum.de/test.php test was successful
http://nattest.net.in.tum.de/individualResult.php?hash=a5f229d156d4f5409a305c37729d9510
http://nattest.net.in.tum.de/individualResult.php?hash=3fd60e888721908a9480cd12836b97af - using VPN on second VM
P.P.S.: I'm using virtualized Windows in Virtual Box.
You should get a network capture trace from both endpoints and study the results. Pay close attention to port numbers on all sides, as the port number may be getting re-mapped as well as the IP address.
It is difficult to infer what are you asking. If you are getting an ICMP error, it's possible that the NAT or endpoint wasn't ready to receive your UDP packet. Repeating the hole punching test a few more times might clear this issue up.
I suspect the port number you think the remote peer is listening on is getting mapped differently from what you think it is. Such will be the case if you are on a symmetric NAT, which is very possible with VPN on VM.
We're using Fleck for our Websockets.
When we test our network program locally it all works fine. The process of a client connecting is:
Establish TCP connection
Send/receive infrequent TCP messages
Start sending/receiving UDP stream to server
The problem is on the live server which is far away geographically, clients can receive and send TCP messages, and they can send UDP messages, but they do not receive UDP messages.
We've investigated it as far as we can, and have found that the RemoteEndPoint property of the TCP connection on the server has the wrong IP address. It appears to be an IP address from our ISP. We use this IP address to attempt to send UDP messages back to the client. As it has the wrong IP, the client never receives these UDP messages.
The actual source IP address of the client must be known somewhere, as the TCP messages make it back OK. I've gone through the Fleck source and have printed out the RemoteEndPoint of the underlying System.Net.Socket but it keeps giving the wrong IP address.
Does anyone know what is going wrong and how we can expose the actual IP addresses of the clients?
The most likely reason is that your client does not have a public IP address, but a private address behind Network Address Translation (a very common setup). A common private addresses are of the form 10.X.X.X or 192.168.X.X.
NAT device replaces private address in a packet from your client with its IP address, which is public. For the outside world it seems that the traffic originates from the NAT device. The main advantage of NAT is that it allows many clients to use a single public IP address (IP addresses are sparse). But NAT introduces a problem: an incoming packet needs to be routed to a correct client, but it does not have a client IP address. Some other strategy needs to be used to recognize to which client a packet should be routed and such routing is not always possible.
For example, the most well known limitation of NAT is that you can't simply start a listening TCP server behind a NAT and then connect to it from the outside world. The reason is that NAT device has no idea that your server listens on a given port and thus, it has no way to known that TCP SYN packets from the outside world need to be passed to your client. A workaround for this is to explicitly configure the NAT device to route SYN packets directed to a given port to a specific client (port forwarding).
When a client behind a NAT initiates a TCP connection, a NAT device remembers state of the connection (client address, client port, remote address, remote port). Because of this, when a response arrives, the device knows to which client the response should be passed. When the connection is closed, the device discards state information. This is why your client can communicate over TCP.
But UDP is more difficult to handle because it is connectionless and stateless. When the device sees a UDP packet it does now known if a reply is expected and when, so not all NAT devices handle UDP traffic. Although there is a technique that allows to do it.
To summarize: the problem you are seeing is not C# specific. Setting IP address in packets from your server to the client IP address won't help, because it is most likely a private address that is not routable. To solve the problem you need to use a NAT device that can pass UDP traffic in both directions. But if you are developing a generic client to be used in many setups, you may want to reconsider if UDP is the best option. TCP connection forwarding is a basic feature that all NAT devices support, but some devices may not support UDP.
i am wondering how tcp servers work because there is no multicasting. I am interested in using tcp for my game since it won't require to much packets to be sent like some other games. It still needs to have a client / server architecture though.
Since there is no multicasting, is there just a loop to send everything directly from server to client for every client? Is this what minecraft does (cuz i read it uses tcp)
it was my understanding that only 1 socket can be bound to a port. With udp, the server socket can accept connections from IPAdress.Any, so it can receive information from all clients. Since TCP is connection only, how would this work? Can multiple TCP connections be made on the same socket?
Only one listening connection can exist per port on the server. However, many clients can connect to that one listening port. A "Connection" under the hood is the combination of ServerIP + ServerPort + ClientIP + ClientPort, also the client port does not need to be the same every time (only the server side port needs to stay static), the OS chooses a random high number port and give that to the client side for the connection. That is why you can have many outgoing connections on a client but only one listening connection on the server.
Look at this page for a example on how to set up multiple connections to one port.
I am attempting to create a server and client that utilizes both TCP and UDP. The server works very well in a LAN setting but the UDP messages are not being received when transmitted over a WAN. I believe it is because the UDP socket used to send the data is not remaining in the NAT tables long enough to return any information. Is there a way to either make the UDP port stay open in the router (without port forwarding) or use the same port for UDP as the already connected TCP connection? Thanks in advance.
If you're not getting any traffic it is probably simply blocked by the firewall. In this case it is not about forwarding, it is about opening the port.
Most (if not all) NAT/Firewall devices will allow UDP traffic in both directions once a hole is punched through the NAT. That is, if my laptop here, sitting behind a NAT/firewall, sends a UDP packet out to the Internet my NAT/firewall will allow return UDP traffic to the originating port number through. I work a lot with UDP and my experience is that this is the rule and very few exceptions.
Keep in mind though UDP packets are not guaranteed to be delivered.
Is your client behind a NAT? Do any packets the client send get to the server? Is the problem in the server to client direction?
If you use the same port number for UDP and TCP this will not change the situation. You can't piggyback on a TCP connection because it is a different protocol.
Network Address Translation (NAT) Behavioral Requirements for Unicast UDP
http://en.wikipedia.org/wiki/UDP_hole_punching