Communicating between two different computers with TCP/IP - java

I've been studying how to use sockets to make two java programs communicate.
Now, on each example I've gone through, they've always placed the "server side program" on the same computer on which the "client side program" was, and of course the IP set was either 127.0.0.1 , or simply "localhost".
Now, my question is:
how do I get to communicate the two programs if one is on a computer, and one on another?
I've tried to set the IP as the one my computer has when connected to internet (the one I found simply by googling "what's my IP"), but that way it doesn't seems to work.
Can anyone explain me better how I can make it work?

The computers will need to be on the same network or their IP address will need to be accessible to the internet. If you are in windows, you can find the local IP address by opening a cmd window (press windows key + R then type cmd) and running ipconfig. On linux (maybe it works on Mac as well) you can run ifconfig in a terminal. If your computers are on the same local area network you should be able to use the IP addresses indicated by those commands.
If they are not on the same local area network, a router in between the computers may have a firewall policy which is blocking the connection.

Related

Java get REAL loopback address programmatically

I am developing a Java application which will listen a local port to communicate with another program. Normally it will listen to, say 127.0.0.1:8808.
One of my client reports that they are using Citrix environment, it is like MS remote desktop environment, multiple users can log in the same machine in the same time.
As you may know, such terminal server users share one loopback address, that means, only the first user run my application can successfully bind to 127.0.0.1:8808, the others will receive resource conflict error.
My client try to fix this by enable "virtual loopback", this setting can automatically assign different loopback address to different users, every user will get 127.0.0.2-127.0.0.255 as the loopback address instead of all 127.0.0.1, that sound like a perfect solution for this situation.
A test shows virtual loopback works well for other programs like chrome and IE, TCPView tool shows any connection to localhost are redirect to 127.0.0.2, except my program.
I am thinking the problem may be because I am using the fixed address 127.0.0.1 in the code, but I don't have an environment to confirm. So my question here is: Is there any Java method I can use to get the REAL loopback address in such situation, so I can bind to the the right address?
Thanks in advance!

How to list all computers in network?

For a small java game I made, I would like to be able to play it with two computers in the same (home) network. I think I will use RMI and am now trying with computer on ipaddress 192.168.2.3.
I know I can search for a registry on this ipaddress on my other computer at 192.168.2.6, but I would like to show a list of all ipaddresses in the network my computer is connected with. Prefferably only if they actually host a game.
Now I tried some questions here on stackoverflow:
How to enumerate IP addresses of all enabled NIC cards from Java?
How can I find all locally bound IP addresses in Java?
How to get a list of IP connected in same network (subnet) using Java
Why does InetAddress.isReachable return false, when I can ping the IP address?
,but I don't think I need all my computer network interfaces and InetAddress.isReachable() always seem to result to false (even though I can ping via cmd and I have firewall turned off) and calling a commandline
"ping -n 1 192.168.2.i" for all i, where 0<=i<=255,
always exits normal, so it results always in true.
What is the best way to get a list of ipaddresses of computers in the same network as the computer the JVM runs on?
With the linked answers, you should be able to filter the available interfaces down to a few possible options (i.e. interfaces that are up, no loopback, have an IPv4 address, etc.).
To discover game hosts, you can do something like the following.
Let the game hosts listen for UDP broadcasts on a specific port.
Let the clients send out a UDP broadcast to the broadcast address of each of the remaining interfaces from above. The broadcast address can be determined by getBroadcast() in class InterfaceAddress.
The host replies, to let the client know it is waiting. When using UDP, the hosts IP is in the received DatagramPacket. When using TCP, the hosts IP can be determined from the Socket.
Then the client can use the address of the host to establish a direct connection and/or set up RMI.
Edit: I found this blog post, which includes code that does more or less what I described.

LAN Game in Java

I have spent some time learning about socket programming in Java and have managed to make a couple of simple apps that utilize sockets. (instant messenger, tic-tac-toe, basic things) For my programs I used a client-server relationship with ServerSocket and Socket classes. So far I have been testing all my games on the same machine, meaning the client and the server both run on the same machine and the socket ip I am using is 127.0.0.1. Now I want to make a LAN game using the same logic. One computer will be the server and another will be the client.
The thing I wanted to ask, and pardon me if this is a stupid question, I am not really educated about networks and whatnot, but under what conditions can I establish a socket connection between two machines. What I mean is, I run my socket server on one computer and I want the socket on another computer to connect using the first computer's ip. Say, for example my ip is "192.1.1.4" I want to be able to connect to that computer. Is it possible to establish a connection like this between just any two computers in the world? I know "lan" stands for "local area network" but I am quite ignorant on it beyond that. Sorry it it is a dumb question and I can clarify it if someone needs me to.
Basically, what criteria must be met on two machines for me to be able to establish a socket connection between them using a Java program?
You can make a TCP/IP connection between:
Two machines in the same LAN (private IP)
Two machines with public IP (internet)
A machine in a LAN and a machine with public IP provided that the connection is openned from the LAN to the public IP
You can't open a direct TCP/IP connection to a machine inside a LAN from outside the LAN, unless the gateway is configured to redirect the connections to a specific port to that machine.
On an internal network you do just what you said, client connects to server using server's ip address or hostname on the given port.
over the internet can be tricky because of firewalls and NAT. For example, your computer's ip address on the home network is probably somewhere along the lines of "192.168.0.xxx" - but if you go to: http://www.whatismyipaddress.com you'll see that your internet facing ip address is completely different. What you'll see is basically your router's IP address on the internet (WAN).
So basically, the server will have to setup port forwarding on their router for your game's port to his computer. Then he will have to provide the clients with his internet facing ip address for connection.
The main criterion for establishing a connection - ignoring a multitude of possible factors such as firewall configuration, etc. - is that the two machines are simply on the same network. You may be aware that an IP address starting with 192.168.. always refers to a computer on a local network, which is the situation you are asking about, so if you have two computers connected on a local network (e.g. via a router), and you know the IP address of each machine, then it really is as simple as that - you connect in the same manner you have been using up until now. In fact, the same applies on the internet - even if you have two machines set up on different sides of the world and you know their IP addresses (again, ignoring potentially more significant firewall issues), the process is exactly the same. This is precisely the reason that the internet has proved so scalable, as the process of locating a machine with a particular IP address is handled by the lower layers of the network stack.
tl;dr: It's the same as what you are doing already, just with the appropriate IP addresses.
you can use a public IP address and you don't have to change anything about your own address, this is handled by your router that connects to the internet. What you do have to consider is your firewall settings. The ports you are using in your socket should be added to your 'allowed' list. Normally "established" traffic is allowed by default. This means that you need to allow your incoming ports # serverside to go to your server.

Pinging computers on a network

I am a researcher at University and I'm trying my hand at a bit of amateur java2 programming.
I've been working through the KnockKnockServer/Client Java2 tutorial to send strings from one program to another using Java Sockets.
http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html
Full source can be downloaded there and it compiles nicely and is a great example of using IP's to get two programs to talk to each other in Java.
This works fine when I use both the server and the client running on the same machine and the machine name as "localhost"
kkSocket = new Socket("localhost", 4444);
And it also works at home when I use it on my home network with a InetAddress:
InetAddress myaddress = InetAddress.getByName("129.11.138.64");
kkSocket = new Socket(myaddress, 4444);
However when I try to do the same on the university machines it doesn't work because they are behind some kind of proxy despite the fact that the two machines are next to each other and plugged into adjacent ethernet ports.
Question:
How to I work out how to get the two machines to speak to each other, when I don't have full control of the network they are on? I can't even work out how to get them to ping each other. I have visited "www.whatismyip.com" to get the external ip's of each machine but that doesn't work, and I've looked at the next work adapters ip and the gateway they are on but still I can't get them to talk to each other.
Any ideas.
Thanks!
It sounds to me like your network is blocking traffic on port 4444. Try a different port, or confirm that port 4444 is allowed.
There are several different parts to this problem:
1) Can the two machines connect to each other?
You can test this from the command line with the "ping" command which is available in both Windows and *nix. Ping tests for the existence and reliability of a connection to the named machine.
2) Could the route be filtered?
Just because you can ping a machine, doesn't mean there is no firewall or proxy between them. You can find the route taken between the two machines with the "tracert" command. The more steps in the route, the more things that might stop your signal
3) Is Java firewalled?
Just because it is installed, does not mean it has open network access. Try fetching a few URLs or similar to see if Java has network connectivity.
4) Is the port blocked?
A port could be blocked in two ways: some other application is using it, or a firewall is limiting listening ports. If the former, Java will not be able to attach a Socket to the port. If the latter, try a different port. There is nothing to stop you running 10000 clients simultaneously and seeing if you can connect to any of them.
5) How do you do this in Java?
Ping can be implemented by InetAddress.isReachable().
6) What should I say to the angry Network Admin guy who has just turned up?
"I'm not port and network scanning! Honest!" Hunting for usable ports is a basic hacking operation. Some workplaces have automatic tools to check for it and will view it as inappropriate.
Even i cannot ping two machines at my work place.
you need to change the network preferences or try the following:
(instructions for IE)
Go to tools-> internet options -> lan settings -> advanced.
Add exceptions to the field: "Do not use proxy server for addresses beginning with:"
enter the networks ip starting numbers, for eg if your network has ip adds starting with 10.--.--.-- write 10.* in the field.
You need to make sure that each machine knows about the other's private IP address on the LAN (see the ipconfig or ifconfig command). It changes (nearly) each time the devices are connected on the LAN, and sometimes even while they are connected on the LAN.
www.whatismyip.com won't help you, because it will only give you the public IP address of the router/NAT which is closest to that server. There is no guarantee it is also the closest one to your devices. Even if it were, this information would be useless, because you (most often) can't predict how the NAT will translate your private IP address into public IP address.
Even if you could 'guess' that information, there is a possibility that the router/NAT might not let you use this translation behind it (i.e., on the LAN).
To summarize it, make sure you use the proper private IP addresses assigned to your devices and make sure that no router or firewall is blocking traffic over the considered port.

How to find the MAC address of a 'shutdown' system on local lan (is it possible?)

How to find out the mac address of a system which is:
Shutdown
Power plugged in
Connected to Lan
Nic card ON.
Update
I am asking how to find out the mac address of some other system which is already shutdown.... and which is on lan..
I want to find out its mac from my system.... and to be clear... I want to tell that.. arp table can be populated by using ping .. i.e. if 172.16.3.12 is not in arp table then it can be added by just ping 172.16.3.12 but a system can be pinged only if its on ... a shutdown system can't be pinged...
Once the system is shutdown your best bet to finding the MAC address is looking for stickers somewhere on the case!
Seriously, I guess you are looking for some way to "wake on lan" a computer on a local lan? I'm not certain that the machine would respond (be supposed to respond) to anything unless you know the mac already and can the correct magic packet.
From the wikipedia article I linked:
How it works
Wake-on-LAN is platform-independent, so any
application on any platform that sends
magic packets can wake up computers
running on any platform. It is not
restricted to LAN (Local area network)
traffic.
The computer to be woken is shut down
(sleeping, hibernating, or soft off;
i.e., ACPI state G1 or G2), with power
reserved for the network card, but not
disconnected from its power source.
The network card listens for a
specific packet containing its MAC
address, called the magic packet,
broadcast on the broadcast address for
that particular subnet (or an entire
LAN, though this requires special
hardware or configuration).
arp -a ip-address if you know the IP address that the PC typically receives from DHCP, or if it has a static IP.
The PC on which you run the arp command may yet have this information in cache. I just did this from home by logging into a remote desktop session on one of my servers. The server I logged into still had my PCs infomation in the ARP cache even though my PC was not responding to ping.
Initially I logged into my DHCP server to see if my PC still had a lease because that will list your MAC address, and it will show your PC's name. Unfortunately for me, my PC has a very short lease and so it was not in the list.
Fortunately, I happen to know the IP address that my PC typically obtains from DHCP, so I was able to use the above arp command.
Using this information I was able to send a magic packet to my PC and wake it up. This I did using a free utility I found after a brief Google search.
Yes, I once tried to implement a data-center-wide power management system based on Wake On Lan technology. I had a number of solutions, but never got to implement them because the data center people decided that the reliability costs involved in shutting down and powering back up were higher than the energy savings.
A couple of random observations:
My experience was that the routers forgot the mac addresses after the machines had been shut down for more than 20 minutes. After that, the mac addresses were lost until the machines next powered up. Obviously, this will vary depending on the actual router used, but 20 minutes is probably pretty typical.
This means that in practice, the only way to get the mac address is to obtain it BEFORE you shut the system down. I'm not sure what your application is, but I think you can probably find a way to do this one way or another.
Once the router has forgotten the mac address, there's no way to deliver the WOL packet to the machine to be woken from outside the network. From inside the network, you can still do it with a broadcast packet. Either way, you still need to know the mac address, since it's part of the WOL packet.
Now theoretically, you could modify the firmware in the NIC chip to allow it to respond to ARP packets even when the machine itself is powered off. This may even be common practice -- I don't know. If so, what you want to do is do-able, but would be highly dependent on the exact network interface you have.
try to use getmac command
example: getmac /s [IP address]
but i cannot know the mac address of computer turned off by this command

Categories

Resources