Does NAT block UDP packets coming from a server connected using TCP? - java

I'm trying to develop a game with using java server and game maker studio client. I've written some basic connection code and it works fine in my local network. But when I host the server and try to connect my client gets the ID assigned by server which sent through TCP but it does not receive any commands sent by UDP protocol. How server can't establish connection over UDP while it can over TCP. I heard I need UDP hole punching. But once I'v written the server with game makers studio networking functions and UDP was able to establish connection with same configurations on the server. So they are using hole punching in their built-in functions ? If I need to implement hole punching instead should I use TCP only or I should learn hole punching for any cost ?

There are quite a few questions here so I'll try to break them apart piece by piece. First of all...
Does NAT block UDP packets coming from a server connected using TCP?
Yes. Just because the NAT has established a TCP connection between two machines does not mean it will automatically forward UDP packets as well.
How server can't establish connection over UDP while it can over TCP?
TCP is a connection oriented protocol while UDP is a connectionless protocol, so there is technically no such thing as a "UDP connection". When two computers communicate via UDP they simply exchange UDP packets back and forth and the "connection" is implied (or managed at a higher level), rather than spelled out by the UDP protocol itself.
I heard I need UDP hole punching... If I need to implement hole punching instead should I use TCP only or I should learn hole punching for any cost ?
UDP hole punching is one possibility but it is complicated and not 100% guaranteed that all NATs will be compatible. Instead, you could build your game using just TCP, then once it is working and deployed, experiment with more advanced communication channels like UDP hole punching or WebRTC Data Channels.

Related

Convert UDP server to TCP

I made a UDP server and now I want the server to be TCP, I don't like losing packets.
Will it be hard to reverse it to TCP or just a few things?
What things should I change to make it TCP?
Can I still use all my packet classes?
I know I have to change the DatagramSocket but don't know to what.
You should read about TCP sockets in Java, as Parker suggested.
If you want advanced functionalities for your server, such as session management, you can use a socket framework like Apache MINA or Netty.

How do i send text data from one computer to another wirelessly using java?

i have a good experience of socket programming in java using lan cable but I'm unable to find anything regarding the wireless transfer using java
plz help me out
Start here.
TCP or UDP can be used. TCP establishes a secure connection between the computers and ensures the delivery of the packages. UDP sends the package and it could be received or not. TCP is the most commonly used of the two, but UDP is mainly used for multiplayer video games.
Sending data over lan wirelessly should still work using sockets.

Problems testing my Java UDP peer to peer program

I've tested the program locally using a program called "Packet Tester"
I downloaded UDP Receiver/Sender for my mobile. Using the java program on my computer I was able to send packets to the mobile server and it was working great. I just typed in the mobiles IP address and the port number it was listening for.
When I tried running my UDP peer to peer on my Laptop and Computer they could not chat at all.
Does anyone know if it is to do with the fact the same router is being used or what not. It should not be since my mobile is receiving messages from the laptop and computer. ]
The Java app can retrieve UDP packets locally, but not from the laptop or mobile. The mobile can retrieve udp packets from the laptop and mobile. The tests show confusing results which have lead me to a wall.
I would appreciate what your thoughts on this are.
Thank you.
The problem might be because of Network Address Translation done by the router.
I believe you are sending packets to correct IP address but the port is not correct. The port to which you might be sending the packets is the local port to the machine.
You need to send the packets to the port assigned by the router.
When the packet goes from a computer A to computer B through the router, the router maps the local port of computer to some random port.
So, if computer B needs to send a packet to computer A, then the computer B needs to send it to the IP:port assigned by the router. The router will then forward it to the local IP:port of computer A.
I suggest first understand how this mapping is created when UDP packets travel through a router within or external to the network. Read about Network Address Translation, UDP holepunching.
These source may help:
Network Address Translation
UDP Hole Punching
RFC 4787 NAT Behavioral Requirements UDP
RFC 5128 P2P across NAT

UDP Client Server dual communication

I am learning about UDP client server where the communication is one way from Server to Client. Server will send UDP packets to Client.
I want to to make it dual communication where Server and Client can send UDP packets to each other.
Do I need to make another Datagram socket for Client Server to make it dual communication or can I use my existing Datagram socket to do the job. If I dont need another Datagram Socket , I would appreciate if you can show me how it is done
I would appreciate if you can point me to relevant resources on dual commuication for Client Server
Yes, you can use the existing DatagramSocket.
Take a look at example in this link:
http://phoenix.goucher.edu/~kelliher/s2011/cs325/feb25.html
Its upon you how to use the send and receive methods of a socket depending on your needs.

How would I locate the correct IP address of another computer on my LAN which is not publicly accessible?

I am writing a distributed Java app, but the networking side of things is stumping me. For some reason it's not working correctly. I think it's because the IP address I get through ipconfig /all is not accessible from outside the LAN. I appreciate any tips or advice.
Overview
You need what is commonly-known as "NAT Traversal", or ICE. There are two primary protocols used on the internet today TCP and UDP. TCP sockets carry a significant amount of session state information in them; consequently it is a significantly more difficult protocol to use for P2P than UDP.
UDP Tunneling
The following list is a simplified outline of the more general STUN Protocol (RFC 5389) that you could use to implement a P2P service based on UDP NAT Traversal...
Deploy a UDP server with a public address and start listening for UDP packets from your clients. Clients will embed their private IP address inside the UDP packets sent to your server; it would be a good idea to implement some form of authentication to ensure you are getting connections from a valid client (instead of some random packet scanner).
The server reads how their private IP address has been translated into a public IP address from the UDP Datagrams.
If you want to make connections between specific users, also embed this information inside packets sent from the clients to your server; your server will implement a username directory to associate client UDP socket information with usernames (that peers will try to connect to).
Your UDP server should send the corresponding information back to the other relevant peer(s).
Now, peers can communicate directly by sending UDP datagrams to these translated addresses; these packets will go through client NAT devices in the path as long as the UDP ports in question are allowed and the delay introduced by this protocol does not trigger state timeouts in the NAT devices.
After you have established UDP connectivity, you could form an UDP SSL VPN between the two clients using something like OpenVPN; this would give you a trivial channel to initiate a TCP connection between the clients. However, there are non-trivial security and trust issues to consider in this connectivity model; it is unlikely to be useful between random users on the internet.
TCP
If TCP connectivity is required, I suggest looking at this internet draft, MMUSIC-ICE-TCP: TCP Candidates with Interactive Connectivity Establishment (ICE)
some ip address are not routable: http://en.wikipedia.org/wiki/IP_address - if you have one of those, you wont be able to access it from outside the lan directly. you can access the other addresses on your lan from one that is on that lan.
if you are outside the lan, you can not start a tcp connection to a specific machine on the lan, but that machine can start one with you: http://en.wikipedia.org/wiki/Network_address_translator

Categories