Socket connection request refused [closed] - java

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am trying to have a Socket connection between my laptop and my Android.
I am making laptop as a server. To establish the connection, I am using the IP of laptop as shown by ipconfig (running Windows 7) (this address is not the same as shown by websites like 'whatismyip').
Things work fine when I connect both my laptop and Android to the same wireless router and I use the address given by ipconfig.
However, when I use the EDGE connection of my Android and access the same IP address, the Socket connection request is refused. Also, the IP given by whatismyip doesn't work either with WLAN or EDGE.
I have heard that there are two IP addresses, internal and external. And I suppose that the address given by ipconfig is the internal address. Also, there is some procedure called as port forwarding.
Can someone please help me to access the ServerSocket via the EDGE connection? Which IP should I use for that, and if I need to do port forwarding, how can it be done?
Thanks.

On your laptop, when you run the ipconfig command, the ip address it shows on whatever interface you are connecting to your router with is a private address (not a routable address). The IP address shown by whatismyip is the IP address assigned to your modem. When your phone is connected to your router over Wi-Fi, you are within the same subnet as your laptop, and are able to create a connection. When you are on the EDGE network, you are on a different subnet, and since your laptop's IP is not routable, you can't connect to it. On most routers, you can configure all ingress traffic for a destination port to be sent to your laptop. The configuration for this varies for each router, but the idea is the same. The IP address you want to forward this traffic to is the private IP address of your laptop (the one shown by ipconfig), and the port is whichever port the traffic you are sending is destined for.

Related

Which ip address should I use to connect 2 computers in my home using datagram sockets in java?

I am trying to connect 2 computer in my home using DatagramSockets (or even Sockets) in java. What exactly should I do? Which IP Address should I use to connect them?
If your machine is using DHCP then it's not upto you to decide which ip address you will use. Your machine will be assigned some dynamic ip address. To see that use ifconfig on Linux box and ipconfig on win machine. Once you have their IP address you can use these to connect your machine. Chances are high that your machine has dynamic ip's.
Assuming you're a windows user:
to get your LAN IP address open a command prompt and type ipconfig.
A bunch of stuff will show up, you are looking for the line that says
IPv4 address.....: 192.168.#.#
It should be noted however, that this is a so "dynamic" ip address, that can be changed whenever you disconnect and reconnect from your router.
I recommend that you either make your ip static (look this up on google, there are lots of tutorials) or use your computers hostname instead. To obtain your hostname you simply type hostname in the commandprompt.
in your code you can get your ip address by doing this in your client code:
String ip = Inet4Address.getByName("<your servers hostname>").getHostAddress();
I hope this helps, although questions like these belong on Super User, as they really don't have much to do with coding.

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 :)

Getting Connectiontimedout error when creating a remote SocketServer in Java

In the socket programming i am able to connect to the server socket when it is on the same pc i.e 127.0.0.1 but when my friend at a remote location runs the server program and i try to connect to it it shows the Connectiontimedout Error.
I'm giving the ip address and port number right.
Do i need to add something extra?
In order to access server remotely, your friend should bind the server to an IP address which is accessible from your machine. This will not be the case if your friend's ISP or wifi router has allocated a private IP address to him.
In such case both of you can join a Virtual Private Network to be on the same network.
Another option is port forwarding. If both of you can access a common machine then your friend can forward a port from the common machine to the application server's port to his machine. Now you can access your friends application server by accessing the socket at forwarded port on common machine.
If both of you are already on the same network then it might be possible that the server is listening on 127.0.0.1 interface only.
There are possibly other middle-boxes that do NAT (Network Address Translation) in the path between you and your friend. These normally prevent the initiation of TCP or other connections over the Internet.
Try doing the same with both of you on the same LAN (Local Area Network) or with a Hamachi VPN to simulate a LAN over the Internet.
Another possibility is configuring your router/NAT at your location to forward the port for your application to the IP address of your machine. In this case make sure to give your friend your public IP (you can get that with http://checkip.dyndns.org/).

How to access private ip address [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I have my laptop that is connected to Internet through my Android mobile which is having its hotspot application running. Now my laptop must have been provided with private ip and my mobile is using a public ip address. I am running a web server on this laptop. Now how can I access this web server from my friends laptop which is not a part of the network ?
I mean which ip address should I provide private ip of the laptop or the public ip address of my mobile?
First your need to find out the following two information:
Your laptop's private IP on which your web server listens on. Use ipconfig on windows or ifconfig on linux or mac.
Your public IP (If you don't know how to find this, you could navigate to www.whatismyip.com and it will show you your public IP)
Then setup a port forwarding rule in your router that forwards a port to your laptop's web server's port. It's not a good idea to forward any port to your laptop, so pick one port. This can be the same port as the port your webserver is configured to listed on.
Lets say you have your web server running on 8080 on your laptop, you would setup a port forwarding rule that maps source port 8080 to your laptop's private ip on port 8080.
You then use the public ip along with the port from outside your internal network. E.g. http://xx.xx.xx.xx:8080
If you want to Access the Website outside the Network, you have to use the public ip.
With the private ip you just can Access ist within Your Network.
I recommend you to get a webserver, because your public ip changes with every reconnect, but at least onces in 24h. And most phone providers do not allow server hosting.

Java Socket Programming

Java Socket Program did not work for WAN
I have written a TCP IP socket program which works fine in my LAN.
One of my friend is at bangalore He ran the server and I ran the
client with the host name of my friend's IP. In this case my
socket program did not work.
You said that your program is attempting to connect to host 192.168.1.107 port 46216.
192 prefix specifies it is a class C address and is private. Making your program connect to that will force it to remain on the local network searching for that node. You will need to find the IP address of your router (you can use http://whatismyip.org/ to find this out). Then go into your router settings and forward port 46216 to 192.168.1.107 (your node), or even better, your MAC address which is not subject to change (in case your router is running DHCP).
on a side note, it isn't good to hardcode IP addresses. Simply use a textfield to avoid having to redistribute the client when your IP is changed, as it is likely you have a dynamic IP from your ISP.
Your friend running the server is most likely behind either a firewall or NAT. Make sure you are using the external IP address and if necessary port forwarding the packets to the correct IP.
The IP address you gave seems to be a local address, rather than a public internet address. If you are looking for 192.x.x.x, you will not make it out to "the internet", but will be confined behind your router.
WhatIsMyIP is a good way of getting a public IP address, and THAT is the address you should use in your connection. Also, be sure to forward any ports that will be used by your program, because otherwise your router will likely filter the traffic and still create an issue.
You could use an implementation of STUNT or other NAT Traversal protocol.
The ip of computer on thih u deployed your server program is not in your reach.
192.x.x.x ip means (class C) it is for local subnet.
You need to have change your ip address of your net-adapter so that your router could route it through internet.

Categories