Is it possible to access lan when using vpn? - java

I'm building an app, and I need to access some data in lan with a given address(Say 192.168.11.222), I use HttpUrlConnection to do my work, and this is just fine when I did not launch a vpn connection using other app(Say shadowsocks android), but when I using vpn established by other app, I just get an exception
java.io.IOException: unexpected end of stream on com.android.okhttp.Address#9727cf6d.
I tried using openConnection(Proxy.NO_PROXY), and that just does'nt help, anyone know how to figure it out?
Since I can't close vpn in some cases.
Thanks in advance.

As far as I know if you use a VPN Connection you will get a WLAN instead of a LAN, so the device connected to a VPN will have access to the machines of your LAN and the network of the VPN. The problem here is when the network of LAN and the VPN network are in the same IP address range. If some machine in your LAN has the same IP address than some machine in your VPN network it will cause conflicts on routing the packages.
So is it possible to access lan when using VPN? Yes, but you have to take into account the previous explanation and be sure that the machines won't have the same IP so you don't have problems to access resources

Related

Java. How to get a list of exactly my servers in the local network?

Using tcp sockets, I wrote a server, launched it on a computer. Wrote a client for android. Being in the same local network, I successfully connect from my smartphone knowing the local ip address of the computer and the server port.
The problem is that I want to run this server on multiple computers on the local network, and their local addresses can change.
Need to somehow scan and select an available server in the application.
How is it implemented? What is the special protocol for scanning? Or just try to connect by sorting through all possible addresses and ports?
Is UDP broadcast a bad solution?
Sorry for google-translator!
UPD: I am trying to make a remote PC application. This will not work on the global internet. only local network. Thanks for the answer
I solve it by use DatagramSocket to UDP broadcast to 192.168.1.255.

Socket TIMEOUT when phone is on 3G, and works when on Wifi- Why?

I'm programming a client-server app, my client being an android phone, my server being my laptop.
So my issue is that this one line of code:
Socket connectionToServerSocket = new Socket(hostName, portNumber);
works perfectly fine when my cellular phone connects to the Internet with my home wifi connection, and simply times out when connected through 3G (cellular provider) eg it blocks for a while then throws a timeout exception.
The funky thing is,that I can see (using OS Monitor) that some apps are connected through very common ports, for example port #80, but switching portNumber (as well as the port that the server is listening to) to 80 doesn't help (eg it still times out), and I've tried many different ports-same result.
DNS works fine (eg it translates the logical String which I gave hostName to the correct IP) but it doesn't send the server anything...
I'm lost,what could be the reason? How can I check and resolve it?
I've run into this issue as well doing a similar application.
Your laptop and phone can connect to each other while on the same network because they share a IP address lookup through your router.
When the device is connected to the WIFI, it's request get passed through the router to check for IP addresses, it will find your laptops IP and save a request to a DNS because it can find the laptops IP already. The laptop works the same way, it finds the Ip address of the client through the router as well.
However, when your phone is on 3G, it has no way of knowing exactly where your laptop's IP address is. That's why it times out: it goes from your router to your nearest DNS (where it tries to resolve the correct IP Address), if it cannot find a domain or IP that matches it will fail.
Some steps to fix this . . .
Depending on your router you can set up port forwarding for your laptop's IP. This means incoming requests to your router can be piped to your laptop's server implementation.
Then go to any site like this http://touch.whatsmyip.org/ on your laptop to get your laptops IP. Save this to add to your clients Socket set up.
For debugging until your laptop server is visible for DNS lookup, go into your client code and add this.
Socket debugSocket = new Socket("the.laptop.ip", 80);
Some warnings:
Depending on your Router, your ip may change during restarts
With port-forwarding any browser with your ip, ex) 178.12.434.01 can log onto your laptops personal server
Future Changes:
Once a dedicated server is up and running, registered with a domain you can change the above ip parameter to "your.domain.com", and behind the scenes the actual IP address to your server will be found via DNS lookup.
the reason for that is that the server in your laptop is closed to external network by default, what you need to do is something called port forwarding
*take note: port forwarding put your server in a cyber security risk, make sure you make the right adjustments to keep your server safe.
case 1: It is working when your laptop and your phone is connected to wifi right ?
Try this once
case 2 :
connect your phone to 3G.
enable hotspot on phone.
connect your laptop to your phone's hotspot.
check the IP of your laptop if it is changed replace that in Socket object. Socket connectionToServerSocket = new Socket(newIP, portNumber);
Run your project.
Just try this once you will get what i am trying to say.
You are getting the timeout exception because your server that is
having the service is not reachable from the external network.
Hope this will help :)

Java Network Connection - No Further Information Error

Alright, I have a java server setup using port 6567 and IP address 0.0.0.0 as to accept any connection. When I attempt to connect over my local network (192.168.1.15) I am able to connect just fine using the server. However when I switch to a non-local IP address (my routers public IP) I am unable to connect to it.
I have the router port forwarded and the proper rules in place on my firewall/etc. Is there any limitations on Java connecting in this fashion? I'm able to connect externally but not internally. Any thoughts on what might be causing this problem?
I'm starting to think it might be a router-specific problem, being that it could be rejecting the connection but I am unable to test that currently.
Turns out it was just the router itself that rejects internal connections using an external IP address. My personal fix was to just add a bit of testing code that automatically changes the IP if on a local machine to 127.0.0.1 rather then the external IP.
Worked flawlessly both on my own PC and having people connect externally once I set that up.
Hmm I'm not sure about it but maybe that will help.
Most probable, Your ServerSocket gets bound to a local IP address (e.g. 0.0.0.0) and ServerSocket binds to the port address there; and wouldn't respond to any requests coming from an IP address. Try new ServerSocket(4444, 50, InetAddress.getByAddress(new byte[] { YOU IP ADDRESS }).
or check again firewall
edit: Tell me how did you tried to connect from other IP than local?

Get IP of Android for P2P connection

Would it be possible to get the IP of Android Phone for P2P connection, Actually i want to connect two android Phones via Sockets ?
I believe you'll need a central server which helps with peer discovery. That's not all even getting IP might not be enough you'll have to work around issues like Net traversal ..this may help Android: NAT Traversal?

In Java, how can I test for network connectivity without using DNS?

We have a Java application running on a portable device and the JVM we're using (CReMe) appears to have a bug where it caches negative DNS lookups, even when we tell it not to. Thus, when we try to make a connection to our server and the DNS lookup fails because the network connection isn't established yet, the application is stuck because it will never perform the lookup again, even when connectivity is restored.
We've tried testing by opening a Socket to a hard-coded IP address, but obviously there's no guarantee that a given IP address won't change in the future. Can anyone suggest another way to verify that we have network connectivity?
You could have a list of IPs and try to open a connection. They could be IPs of machines on your LAN or they could be Google or Facebook's IP.
If you can't connect to any IP on the list, you can assume the network is down.
When the network is up, you can update the list with a DNS lookup.
You could try custom DNS client like dnsjava.
Maybe you can try to use the DNS servers of Google? Doubt they will ever change, and pretty sure they are usually up :)
8.8.8.8
8.8.4.4

Categories