This question already exists:
How make every remote ip address connect to the Java RMI's server?
Closed 7 years ago.
I am developing a client/server application using Sockets and here is the basic code for the client:
Socket sock = new Socket("serversip",9999);
PrintStream pr = new PrintStream(sock.getOutputStream());
And here's for the server:
ServerSocket ser = new ServerSocket(9999);
Socket sock = ser.accept();
BufferedReader ed = new BufferedReader(new InputStreamReader(sock.getInputStream()));
Whenever, the client and the server applications are on the same network, this works absolutely flawlessly(i use Inet.getLocalhost() and supply the servers's ip to the client manually)
But when they are on different networks(i.e. Internet) and I use:
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
String ipp = in.readLine();
to obtain the server's public ip address and provide it manually to the client it just won't work.
It always says:
java.net.ConnectException: Connection timed out: connect
I have used Java RMI before and it too gave the same result i.e. not working on the internet, just working on private network.
So, how am I supposed to establish a connection between remote java programs on the internet, is it even possible?? It makes me wonder then how all these chat applications,etc we use work.
Please help me!! I would be grateful to anyone who reads my problem and at least provides a response. Thank you for reading!
checkip returns the server's public IP, but it is likely that the server is behind a NAT. In that case the server is part of a Local Area Network and it has a local IP address (such as 192.168.1.2). You will have to map the port 9999 on the NAT device (e.g. the router provided by the ISP in a residential connection) to the same port on the server at 192.168.1.2. Some ISP manage the NAT itself and don't allow them to be configured by the users.
Another point to consider are firewalls. There is a (software) firewall running on the server itself (such as the Windows Firewall, or iptables in Linux), as well as there may be a firewall on the client side that is blocking the outgoing connections to port 9999. In the case of residential connections, the ISP may also be blocking traffic on that port. In corporate networks, outgoing connections to arbitrary ports are sometimes blocked too.
Related
I am trying to write a client-server app that connects a PC as a client to my PC as a server.
When I enter 127.0.0.1 as server IP in client side, in my PC, it works properly so it's not a coding problem. Also when I enter my IP (got it from nslookup command in kali) and connect to internet, client connects to server properly.
But when I open my client app in other PC and server app in my PC, the client side a "Connection Time out" Exception will be thrown.
I have tried turning off the firewall in the client side (Windows 10) but not from the server side.
Here are my codes:
Server:
ServerSocket server = new ServerSocket(SERVER_PORT);
Socket client = server.accept();
//Some codes
Client:
Socket server = new Socket(SERVER_IP, SERVER_PORT);
For better answer we need more informations from you. But from this what you post, this is best answer i can make. You didn't explain what is your goal, do you want to make connection inside local network or you would like to make connection to 'outside world', over internet.
If you like to make two PCs communicate inside local network, than you must make sure they are on same network, or more accurate, that they are connected to same router. You have to set your server IP address to IP address on your local network, that would probably be something like 192.168.xxx.xxx
If you want to communicate over internet, i suggest test in local network first, as described before. If it works on local network, then you have to deal with firewalls, router setting,etc to make it work on the internet too.
Take look at this too
Use Socket-communication over different networks
I have a problem with the socket in java: I have a server on the desktop PC that I've a ServerSocket object Instantiated such:
ServerSocket socket = new ServerSocket(port);
where "port" is a number of port.
This serverSocket accept a client. This client is another application java that runs on another desktop PC that connected to the same net of the server PC. In this application java, I Instantiated a Socket object which connects with local IP of server PC (example: 192.168.1.11) and port of serverSocket. When I run the server and the client, the client does not connect to the server. If I run the server and the client on the same desktop PC, the client manages to connect to the server.
I was thinking of a problem with the firewall configuration but I did not manage to solve it. Help me please.
This is code of server:
//I will accept one client
ServerSocket server = new ServerSocket(19999);
Socket client = server.accept();
new Thread(new ManagerConnectionThread(client)).start(); //this thread manage the requests
ManagerConnectionThread is a class that manage the communication between server and client:
//This code is on the server
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
This is code of client:
public static void main(String[] args){
Socket client = new Socket(InetAddress.getByName("192.168.1.11"), 19999); //192.168.1.11 is IP of "server" pc
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
}
do you have a router involved? if yes you will need to configure some port-forwarding and try to access via the external address, meaning that you will try to connect through the router that will point to your machine.
Also, you might need to do some tweaking with the firewall, at some point I was doing something similar for school, but as a server, I was using a Linux based OS so the procedure might be different, but I definitely needed to allow in/out for the port that I was using.
you might find some answers in this post which is similar
Java Chat with TCP/IP over LAN
Edit: I found my old homework, and I have definitely used the external address (especially because I wanted to access it from school, while the server was home) so I would connect to my external address of the laptop and then in the router's settings I would forward the port that I was using to the server, in the server allowing incoming traffic for the specific port.
I am trying to send data over TCP/IP from a Java EE application to an Android app. In Android I have the following in a AsyncTask:
try {
ServerSocket serverSocket = new ServerSocket(48760);
while (true) {
Socket socket = serverSocket.accept();
// Do something interesting with sent data
}
} catch (IOException e) {
e.printStackTrace();
}
I have verified that the app executes until it blocks at the accept() call, as expected.
on the server side (Java EE) I use:
// address contains the IP of the android device as a string.
InetAddress addr = InetAddress.getByName(address);
Socket conn = new Socket(addr, 48760);
Here I get "java.net.ConnectException: Connection timed out: connect" when the application reaches "new Socket()". Any ideas as to why this is?
I have in my manifest. And the ip in "address" seems to be the correct one (the ip provided by my service provider).
Is there a better way of sending messages? I was planing on using Serializable objects (from Android to JavaEE I use HTTP, through a Servlet) but I am open for suggestions.
Okay there is a problem with your implement !
Why you need to connect from server to client (who cares but client is server now !) !?
You must fix this problem !
And lets show you what is your problem !
If your server (JAVA EE Server) is on public internet so it means your server has a public IP address . This public IP address is accessible from all devices that connected to internet ! You can control all ports of server and so on ... , so every one can connect to your web application in public Internet.
But in client side somethings are difference !
When you connect to internet in client side this will happen :
1 - Your device connect to router (it can be a modem in your home or mobile service)
2 - Your router has an IP address in IPS network and ISP network translate it (using NAT) to a possible public IP
3 - now your device(s) has the same IP as that IP assigned to your router in ISP
Your problem is in section 2 ! because most of ISP(s) share a public IP between clients so it means when your assigned IP is ADDR_1 there is another clients that has the same IP ADDR_1 and you cant control all ports on it . Maybe when you listening on port 8080 in your local network the ISP assign another port for your public IP (Regardless of NAT will block your listening ports in most of ISP(s))
So every clients doesn't have unique IP and too hard to find them in public internet (it will be harder when new NAT(s) [Symmetric-NAT] exists on ISP)
There is several ways to handle this problem , but there isn't any way to work 100% !
I am currently working on a simple multiplayer game where serveral clients need to connect to a server.
My server consits of a single serverSocket. This serverSocket accepts incoming connections and hands them over to connection object that starts a separate thread.
ServerSocket seso = new ServerSocket(12345);
while(true){
Socket toClient = seso.accept();
new Connection(toClient); //creates a thread that opens streams etc
}
Clients open a new Socket and connect to this server.
Socket toServer = new Socket();
toServer.setReuseAddress(true);
toServer.bind(new InetSocketAddress(65432)); //always using the same port
toServer.connect(new InetSocketAddress(serverIP,12345));
Now if i close the connection to the server using toServer.close(); and try to connect again to the server, i get an "address already in use: connect" exception.
Using TCPView i can see that the state of the client procress changes to TIME_WAIT. But shouldn't i be able to use this port again because of setReuseAddress(true)? Am i using it wrong or is it an server problem?
I do always call .close() on toClient and toServer. Nevertheless i always have to wait until the socket is completely closed (after TIME_WAIT) before this client can connect again to the server.
When i close the entire application, the socket is immediately closed (not in state TIME_WAIT) and this client can connect to my server. (And ofc there is a connection reset exception in my server)
How can I do that without always closing the application ?
Thanks for your help.
To expand on my comment, a client / server protocol requires the server to listen on a port known to or discoverable by the client -- that can be considered the definition of "server" -- but it does not ordinarily require clients to connect from a specific port. If you do not bind the client socket to a particular port, then the underlying system will choose an available (source) port automatically and transparently.
If the server depends for some reason on clients connecting from a particular port, then you should re-evaluate that aspect of your design. If it does not, then you are making your own trouble by having clients connect that way. This should be all you need to do:
Socket toServer = new Socket();
toServer.connect(new InetSocketAddress(serverIP, 12345));
I wrote a simple socket program on android, the server side sets up a server socket waiting for incoming connections, the client side just connect to the server by establish a connection with the server's ip address. I'm using Sprint's 3G Nexus S as the server, and ATT'S 4G Samsung galaxy S3 as client. When my client tries to connect, it throws out the exception"No route to destination". But then I switched to my personal WIFI network, it worked perfectly. Can someone help me with this issue? or is there any special requirement in android to use cellular network to set up socket connection? thank you!
My client side is:
socket = new Socket(serveripaddress, 8008);
out = new PrintWriter(socket.getOutputStream());
My server side is:
ss = new ServerSocket(8008);
update: I just used 2 Sprint cellphones to test my code and it works fine!! This is just a special case in which i have to use phone as server, it is not recommended in general
Carrier firewalls typically don't allow incoming connections to mobile devices. Doing so would leave the mobile devices open to security risks and abuse of their data limits. It can also depend on the APN used for the connection.