Connection refused when creating a new LAN socket - java

I've been trying to establish a very basic connection between two computers in my LAN. I've viewed several code snippets and went from there, but things seem to fail at the very start: creating a socket.
I've tried it with both connecting to the computer I'm executing the code from (using the hostnames "Jeroen-DESKTOP" and "Localhost" and the localhost IP "127.0.0.1"), and connecting to my laptop (using "Jeroen-LAPTOP"). I've tried the ports 6666 and 7598and opened all TCP connections to it in my firewall settings.
The code I'm using is very basic:
socket = new Socket("Jeroen-LAPTOP", 6666);
System.out.println(socket.getPort());
With the error being:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at domain.Client.<init>(Client.java:13)

Without knowing exactly what you are doing -
Assuming you are opening a socket on Machine A and trying to connect to it using Machine B, then the typical scenario in Java would be.
Machine A:
ServerSocket server = new ServerSocket(4444);
Socket connection = server.Accept();
Machine B:
Socket sock = new Socket(MACHINE-A-IP, 4444);
This would require exception handling etc to allow it to work. You can see examples here;
http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
Regarding your comment - yes you can run them both locally.

Related

How Do I Connect To A Remote MySQL Database in Java? [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 6 years ago.
I'm developing a Java application that (among other things) queries a remote database. I'm having trouble getting a connection to the database.
My code is:
private static String MYSQL_URL = "jdbc:mysql://db644874220.db.1and1.com";
private static String DB_NAME = "db644874220";
private static Connection CONNECTION;
static {
String dbUrl = MYSQL_URL + "/" + DB_NAME;
try {
Class.forName("com.mysql.jdbc.Driver");
CONNECTION = DriverManager.getConnection(dbUrl, MYSQL_USERNAME, MYSQL_PASSWORD);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
The error I get is:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.joshuaevanslowell.IO.<clinit>(IO.java:41)
at com.joshuaevanslowell.Speculator.<init>(Speculator.java:46)
at com.joshuaevanslowell.Speculator.main(Speculator.java:32)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 17 more
I've seen some other questions dealing with this, but they generally assume that the database is on localhost, which mine isn't. I'm using 1&1 web hosting, and I can work with the database through my browser through phpMyAdmin. The information it gives about the server is
Server: db644874220.db.1and1.com via TCP/IP
Server type: MySQL
Server version: 5.5.50-0+deb7u2-log - (Debian)
Protocol version: 10
User: dbo644874220#10.72.2.9
Server charset: UTF-8 Unicode (utf8)
I feel like I'm doing something horribly wrong, and I just can't make head or tail of this.
If you have telnet client client installed assuming 3306 is the mysql port, please try
telnet ipaddress 3306.
I fear you may not have direct connectivity to mysql server. To enable this you may need to edit the configuration as answered in similar questions.
Also, please do close the DB connection or rather use a connection pool. Having too many open connections(above the configured limit) may cause similar issues.
my guess is that the db644874220.db.1and1.com host is not known from the location where you run your Java code
try to do ping db644874220.db.1and1.com from the machine where you run the Java code or telnet db644874220.db.1and1.com 3306. If neither return a result it means that you don't have a way to access that machine via its name.
Either use the IP instead if you know it or add a line in your hosts file
[IP address] db644874220.db.1and1.com

Trying to test connection on localhost ports using Socket and get ConnectException

I'm writing a simple test Java program, using Socket to test which port is available for TCP service on my local machine. Here is my code:
for (int i = 0; i < 1024; i++) {
try {
//test availability
InetAddress addr = InetAddress.getLocalHost();
Socket s = new Socket(addr, i);
System.out.println("There is a server on port " + i + "of localhost.");
} catch (IOException e){
e.printStackTrace();
break;
}
}
and when running I got:
java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at main.PortScanner.main(PortScanner.java:19)
I'm new to Socket. And do I need to start a Socket server to response for my quest?
Or what should I really do to scan my ports?
Thanks for your answer.
You are trying to establish a client connection to port 0 which is not possible. If you would bind the serverside socket to port 0, the system would auto-assign a free port above 1024.
Thus a server socket would never listen to port 0.
Unfortunately I could not find a documentation about this. It might be that other operating systems behave differently.
Your second question: If nothing is listening on a port then you will get a connect exception.
Do you run this program as root or Administrator ? If not, only these users can open a socket with a port between 0 and 1024.

Java exception while attempting socket connection

I am trying to make a socket connection to an ip:port and it all works correctly on one of my computers. The client first connects to the server with a socket connection to log in; after that it expects incoming connections from the server and they are successful.
But, when I tried it on another computer, the client made the first connection to log in successfuly, and then waited for incoming connections. On the server side where the connections would be initiated I got this message:
java.net.NoRouteToHostException
and the incoming connection failed.
Why would that happen? Could it be a firewall issue?
s = new Socket(id, 4446);
BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
thats the 46-th line
java.net.NoRouteToHostException: No route to host: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sample_server.doComms.run(doComms.java:46)
at java.lang.Thread.run(Unknown Source)
As stated in the API:
Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the remote host cannot be reached because of an intervening firewall, or if an intermediate router is down.
You most likely have a firewall or something blocking the connection. Try disabling one if you have one real quick, and see how that works. If it doesn't work, it's something else (the full stacktrace may help as comments on your question say).
'No route to host' means that your host doesn't even know how to contact the target. It's a network connectivity issue. Not a firewall issue.

Java sockets error: java.net.ConnectException: Connection refused: connect [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java sockets - java.net.ConnectException: Connection refused: connect
I've created a simple chat program which communicates using sockets. Everything works fine when I'm running it on localhost. However, the problems occur when I try to link the client and server programs using my IP.
http://www.canyouseeme.org/ can connect to my server on port 9999 so I know that the server is fine and the port is open. However, my client can't connect.
The error log...
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.connect(Client.java:129)
at Client.main(Client.java:47)
Does anybody have any idea what might be causing this? Thanks in advance.
Links to the full source code:
http://pastebin.com/2XftHtn9
Are you trying to connect to your own server using its public IP address from inside your LAN? For most SoHo routers, port forwarding only works WAN-to-LAN, not LAN-to-LAN. What you're looking for is called "hairpin NAT", and many SoHo routers just don't do it. To reach your server from inside your LAN, use its inside IP address, not its public IP address.

java.rmi.ConnectException: Connection refused to host

After a hard work, I have set up Java RMI tunnelling using apache as http
server. Everything is fine and Works like a charm at my office LAN.But when I installed at client's place, I am getting some exception.The RMI System works only on his server.
When I tried from other clients' pcs I get the following.
Can you guys help me solve this?
java.rmi.ConnectException: Connection refused to host: 172.xx.x.xxx;
nested exception is: java.net.ConnectException: Connection timed
out: connect at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown
Source) at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown
Source) at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown
Source) at sun.rmi.server.UnicastRef.invoke(Unknown Source) at
java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown
Source) at
java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy1.getUserID(Unknown Source) at
rmi.source.ServerImpl$JobScheduler.run(ServerImpl.java:265) at
java.util.TimerThread.mainLoop(Unknown Source) at
java.util.TimerThread.run(Unknown Source) Caused by:
java.net.ConnectException: Connection timed out: connect at
java.net.PlainSocketImpl.socketConnect(Native Method) at
java.net.PlainSocketImpl.doConnect(Unknown Source) at
java.net.PlainSocketImpl.connectToAddress(Unknown Source) at
java.net.PlainSocketImpl.connect(Unknown Source) at
java.net.SocksSocketImpl.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
java.net.Socket.connect(Unknown Source) at
java.net.Socket.<init>(Unknown Source) at
java.net.Socket.<init>(Unknown Source) at
sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown
Source) at
sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown
Source) ... 10 more
I am running files from roseindia rmi
there is mistake rmicregistry, one have to use rmiregistry:
I changed ip to localhost, how to run on special port number I don't know this moment
one has to use rmiregistry
not rmicregistry
so:
move to folder where does your class files take seat.
type : rmiregistry
then open 2 new command prompts move to same folder where classes seat and use one to run server and another to client.
RMI servers have the nasty habit to pass new port numbers to clients to handle the client communication to a particular Remote object. If this port number is blocked by the firewall, you would get an exception like that.
Make sure you register all Remote objects with the same port number. RMI will then multiplex the client calls for you.
after my research, the solution is:
System.setProperty("java.rmi.server.hostname", IP);
before you register the service on server side. following is an example, hope it might help you!
public static void main(String[] args) throws MalformedURLException, RemoteException, AlreadyBoundException {
RmiServer server=new RmiServer();
System.setProperty("java.rmi.server.hostname", 指定IP);
LocateRegistry.createRegistry(8808);
Naming.rebind("//10.10.116.74:8808/SAMPLE-SERVER", server);
}

Categories

Resources