Simple Network chat in Java - java

just had a question to a simple chat program i wrote in java.
The program uses TCP to transmit simple message strings back and forth from Server/Client. And it works on all the computers on my Network. But when i sent it to a friend of mine to see if it worked from his computer it did not. So my question is: Would i need to add some specific code to allow messages from other comps from outside my network to be recieved? or is it some kind of Firewall that is preventing this?

Probably a firewall or a router.
Firewall: port is not opened.
Router: message are not forwarded to the machine which hosts your application.
Or your application is using the internal IP and not the IP from the internet (which has to be forwarded to your internal ip)

If you don't specify anything special in the code for computers that's not on the internal network, then it won't handle them differently. It won't handle the connections differently by itself. I'd check your(or his) router and open any ports that you use.

Related

UDP over Internet. How does it work?

As I am programming a network chat (java, but should not make a difference for the question), and wanted to use UDP, I ran into the problem of it not working over the internet. After a little research I found out that you have to have port forwarding for the specific port activated. So now it comes to my question:
Does UDP work over the Internet in a not configurable way?
For example, if I would program a whole Network Game would it make sense to use UDP? Or would I require the Player to activate Portforwarding and open the Port etc?
When would it make sense to use UDP then? And why?
I'm actually not understanding the whole point of UDP then.
For my programming point of view I would like to have a way to use it intuitive.
Like creating the DatagramSocket and the DatagramPacket, configure the Packet with the Data and the Destination and send it away over the internet.
As for my Users I don't want them to have to configure any specific things like opening the exact port they want to use etc. I just want them to use the program (server and client) and it should work.
The problem you've run into is not one of UDP vs TCP (although using the unreliable, unordered UDP as the basis of a chat application seems like an odd choice to me).
The problem is that of NAT traversal. In a nutshell, home routers perform a network function called NAT - Network Address Translation. They do it in order to use a single public IP address for all machines inside the NAT (which are given private addresses - usually 10.0.0.0 or 192.168.0.0). The router then switches the source IP address in all packets sent from inside the LAN from the private address to the public one. It uses port numbers to "remember" which machine sent what to what address, in order to perform the backwards translation when the response arrives.
The problem arises when someone wants to initiate a connection to a machine behind a NAT. Without seeing an outgoing connection first, the NAT doesn't know to which internal computer and port it should forward the packet. This is what happens to you.
There are various fixes for this issue, with the simplest one being manual port forwarding (as you've discovered), but it's a well known problem faced by any peer-to-peer application. If you need to contact a machine behind NAT (i.e. contact most home users) and you want your application to work out-of-the box (without your users fiddling with their routers) you need to research NAT traversal techniques, implement them in your application, and hope that the user's home routers support them. It's a huge pain in the neck.
EDITED: with Joachim Pileborg's correct suggestions!
UDP is often a better choice for action-based games, where it's vitally important to have updates to the client or server with the latest data about a player, player input, or the game world.
TCP begins with a 3-way handshake to establish a connection (which takes time). If your game communication protocol is via TCP, all packets in a message have to arrive before the message becomes available. Even a small amount of Internet congestion could cause your game to lag.
TCP is good for communications that MUST arrive in full.
With UDP, the client or server can send the latest player/game state in individual packets that do not depend on arriving in order. If a packet is late, or arrives out of order... it should be ignored.
UDP is good for communications that need to be fast, but losing individual packets is OK.
Both should be available in your Java platform.
Here's some further reading:
http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/

can an isp block incoming connections?

First of all, i am a newbie in network programming. Few days ago, i wanted to test a simple java server(simple java echo server). I first tried running it in a university computer and then tried connecting through my laptop which is in a different network without any success. I assumed the network(private university network) didn't allow the connection. Next, i ran the server in my laptop expecting it to be public. I ran the server. I ran it in port 5000. Since the connection was through wifi i also did port forwarding so that my router would know where to forward the incoming requests. I even disabled the firewall just to test. Then there is a tool in the internet which i obtained from some other user in stackoverflow http://www.yougetsignal.com/tools/open-ports/ which can check if a port is open in a machine. I put my ipaddress and port number to see if it is reachable. It continued saying the port is closed. Although i don't have a good idea about networks, i called the ISP and asked if the issue was with the network being a private network and if it is possible to host server in my laptop, she concurred saying it was private network and what i am trying to do is not possible. I wanted to try it in some other computer. I met one good guy in stackoverflow who allowed me to run the server in his computer in taiwan. He disabled his firewall or added an exception, then ran the server. I then ran my client and it could successfully connect to his server. I was ecstatic.
So, my question and curiosity is how does isp blocking incoming connection work? Did the ISP really block the incoming connection to my server or could the problem be with some other things? Is it ever possible to host server in my laptop which is accessible to the world. I am still not content with the answer i got from the ISP which was it was private network so it was blocking..isn't internet basically the collection of private networks connected to each other so isn't everyone in one way or other part of a private network? Please help me with these dilemmas. Appreciate your effort and help a lot! :)
Your house probably has one global external IP assigned to you by your ISP. They will normally block commonly used ports incoming, such as 80 (HTTP), 22 (SSH), 23 (Telnet), 25 (SMTP), and 443 (HTTPS). The majority of the remaining ports should be open for you to use.
As you have one global IP, and multiple devices on your network, you need to tell your router which computer the incoming traffic on a given port needs to go to. This is called Port Forwarding. As you don't control the routers upstream from you at your university, you can't change anything while there. At home you can.
Port Forwarding is explained in depth here.
I suppose it depends on the ISP. I have been able to open ports before with mine, using port forwarding from my router.
As a technician who works for an ISP, I can also say that the tech support generally doesn't have a clue whenbit comes to port blocking or networking in general, they mostly follow a script and ask you if you have turned it off and on again. Maybe check that you are forwarding the port correctly?

Java program without forward port in router

I made application with java using socket. My computer is the server and my phone(android ) is the client. Only what i trying to do is to forward String from phone to client PC.
Everything works fine when i configure my router and open the port i using. i don't want every time when costumer will install my application will need to open port in the router.
I thought about using remote server that will run my server code. but i didn't find server that can do that . i don't want to make my pc a server for all costumers.
How does all the chats companies do it without open port?
You must look into UPnP. This is what /most/ if not /all/ torrent clients use to allow foreign connections, without forwarding ports. How chat clients do it is a different scenario. They use hacks such as firewall hole-punching using UDP (with an external server) http://www.h-online.com/security/features/How-Skype-Co-get-round-firewalls-747197.html see this link for details on UDP hole punching.
Also see this article http://www.codeproject.com/Articles/13285/Using-UPnP-for-Programmatic-Port-Forwardings-and-N for usage of UPnP. But this is in C++, but I think you will understand.
EDIT: http://4thline.org/projects/cling/ I found this. I think it can help you.
You had to open port probably because of your routers firewall :). If you already opened let us say port 9090 then every client app (android phone) will be able to connect to it :). Of course if somebody wants to install server on their own pc they would problably forward some ports and disable some firewalls. If you want server with no special requriements lookup VPS'es. :)

How to stop routers blocking my applications network traffic

I have a application which listens for commands over IP.
The program works fine locally but when I try to send the application commands with a remote address it won't connect.
Is there anyway to get around the router blocking the inbound network traffic?
I'm using JAVA
Thanks.
If this is a pair of hosts you control, you can open the incoming port. You might also
succeed using a tunneling program such as Hamachi to effectively set up a VPN linking
the hosts.
If you're talking about a pair of unrelated computers (for example trying to set up
a connection for a game) there's no general solution that doesn't involve installing
and trusting additional software. The usual solution is to use a public server and
relay the traffic between the end points.

Java Sockets time-out over the Internet

I created a small chat program, that works flawlessly when client & server are run on the same computer (and probably network, too).
However, as soon as I try to connect to another computer over the internet, the socket connection simply times out.
Is this because of firewalls / routers, etc?
And how can I connect a ServerSocket & Socket over the internet?
However, as soon as I try to connect to another computer over the internet, the socket connection simply times out. Is this because of firewalls / routers, etc?
Yes, most likely. You're running into the NAT problem: essentially, the same externally visible IP address maps to many internally visible endpoints, and external endpoint doesn't know which internal endpoint to give your socket request to.
The easiest way around this is to have both your clients connect to a third party which both of them can see, and then have the third party mediate the communication. This is how most instant-messaging protocols work, for example.
If you have no way to control a third-party entity like that, an alternative to directly connect two clients is to have both clients open up an agreed-upon port, and then map communications on that port to their own internal endpoint. This provides the missing link that the externally visible endpoint (e.g. your home router) needs to deliver the communication to its intended destination.
If your server is behind a NAT router box (and most home computers are, especially if you use WiFi), then it won't be reachable from the outside unless you set up your router to port forward to that server.
What's the IP of your server computer? If it's 192.168.x.x or 10.x.x.x, then it's a non-routable address and can't be reached from outside.
Assuming with running on the same computer you mean that you tell the client the server is at 127.0.0.1 / localhost, it shouldn't be a problem in your code but because of firewalls or routers. If your server is behind a router performing masquerading (i.e., the server doesn't have a public but private IP address like 192.168.x.y for instance), you have to configure the router to pass a connection from the internet to the computer running the server.
Another reason why it doesn't work might be the way you bind your server to the interface. If you specify 127.0.0.1 there, the server will only listen for requests coming from the same system. If you don't specify an address, it will listen on all interfaces.
Edit Your comment indicates that you indeed have the NAT problem like others said. Configuring your router accordingly is probably the easiest solution.
First, test to see if it really works on a LAN; it sounds like you're just assuming it does.
If it works on your local network, perhaps it's failing because the server lacks a public IP, and is running behind a NAT'ing router.

Categories