Java - Connecting two machines on same network - java

I want to simply connect two machines on the same network via a TCP Server/Client socket connection.
Right now, I need the IPv4 address from the machine hosting the server in order to connect the client.
localhost works fine when I'm running the server and the client on the same computer, but that's not very useful.
Is there any way around having to manually punch in the IPv4 of the host computer?
Thanks, this will probably clear up a lot of confusion.

Use a broadcast to send all clients a message. (this could contain the servers IP but think about that! it could be a security issue)
There are good examples on sending broadcast messages with java
Broadcasting to Multiple Recipients

If you know the name of the hosts, then you can also pass these names in a config file and use getaddrinfo() to get the IP address of the host: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html . Since hostnames do not chnage frequently, this should provide good improvement as compared to manually punching the IP address.

Related

Public to Local IP Communication

I have created project which has two components as Desktop client and web.
web server is communicating to the my another program which i run as client on other systems.
After connecting those clients communication is happening in network as all IP's within network are reachable.
However, when i deployed web app on public IP now i am not able to connect the clients as the local IP's of those clients are not reachable by server.
How can i achieve this communication between local IP to public and vice versa?
There are multiple ways to achieve this.
Anyways, if you want the service to be reachable publically then you´ll probalby want to forward the Port to the machine running the service.
Also, make sure the Firewall allows connections to this port.
Since you´re talking about Web-Apps it´s probably HTTP, Port 80 TCP, or HTTPS, the encrypted version of HTTP running on port 443 TCP.
To explain it, your ISP gives you one public IP address.
Since you probably have multiple devices using internet, they all appear in the internet as the one IP address your provider gave you.
Whenever you send something out your router will remember where you tried to connect and if a response comes in your router knows which device to send the response to.
Now, since you want someone to connect to you, there was no request so your router does not know where to put the packet and simply blocks it.
In most routers you can configure something usually called NAT or Port Forwarding. You simply specify that communication on Port 80 or 443 should be routed to the internal IP. It has one of the following formats:
192.168.0.1 - 192.168.255.254
172.16.XXX.XXX - 172.31.255.254
10.0.0.0 – 10.255.255.254

Socket communication on Android localhost

I'm developing multiple applications which need to communicate with each other via socket. First I need them to be able to communicate on localhost. So when I'm running both of them at the same time on my phone they can communicate (I know that there are easier ways to do this on the same phone but in the future they will run on separate phones).
My code for socket communication is very similar to this: link
Difference is that my apps are running this as foreground services.
I've set the ip for the server on the client to 127.0.0.1 but they just won't connect (not in the emulator and not on real phone). What am I missing?
UPDATE:
I've found an easy way to get the device own IP address, so instead of localhost I use this (with www.google.com domain): answer to "java InetAddress.getLocalHost(); returns 127.0.0.1 … how to get REAL IP?"
Maybe it's not too nice but it works.
You'll need to set the local IP address of each one, so they can communicate within your LAN. So use an address like 192.168.1.X. Both if you're running your devices as virtual or physical, you may easily know the local IP address they have accessing your router's web interface and seeing their bound IPs.

Java TCP/IP Socket internet connection problems

Im using TCP/IP sockets in java to try and create a client-server application. The program works fine when run locally and also over the local area network, but when I use the internet IP address the clients connection is refused.
I used this website to get my IP address and have added a firewall entry to unblock the port im using (port 4445).
I am almost certain the problem lies in some sort of security measure that is blocking the port. Does it matter that I'm running the client and server on the same PC but using the IP address from the previously mentioned website?
If I could get a list of ways to test the port is in fact open, or a list of things to try in order to get my program running, that would be great!
That website may very likely give you the IP address of the gateway through which your PC is connecting to the internet, and if the gateway is out of your control (which is most of the cases as far as I know) there's nothing you can do to use that IP address to test your program. Here's some advice:
Try http://aws.amazon.com, once registered you have one-year free access to a micro-server (which can be accessed publicly through DNS/Elastic IP.)
If your PC have a public IP address, you don't need that website to find out what it is. Just check your network adapter control panel.
Where is the server has been located? If your server is located in some commercial hosting, there is possibility that the ports you use are blocked. Also if you use modem with router or just router in your local network you should check nat table.

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.

How to send TCP queries for unknown IP of embedded system?

I need to make Android application which can send TCP queries to an embedded RS-232 system (connected to a router).
Unfortunately, no IP address was given because each embedded system has a different IP address.
To learn the IP Address, an android device (tablet/smartphone) must connect to the local wireless network and acquire the address of the embedded system. The local network also has a public IP address which enables the embedded system to communicate with the outside world.
My task is to send queries to the embedded system and get replies from it. The protocol is RS-232 for both input and output.
Please, give me an idea of where to start. Thank you.
Scan the network for the open port if there are no zero config options.
If you know the range of mac addresses the devices use you should be able to filter them out using ARP.
In case you can control both ends you can use multicast and save the discovery step.
Alternatively you can use dns-sd, but again the embedded system should be aware of it.
At the end, if none of the above options are possible you should scan the network as mentioned in other answer, but if DHCP is used you will never know if the address you obtained is still valid.
If you are talking about on the same network you could use a UDP broadcast which is picked up by the device which contains the remote ip, which then sends a response to the android handset with it's own tcp ip address.

Categories

Resources