I'm using sockets for a client-server application (i.e. the client sends data and the server received the data). My code is below. When I use the client in an Android 5.1 application on a tablet and the server in a Java application on a Windows 7 PC it works very well (i.e. the tablet can send data to the PC). But when I try to use the server code on the Android 5.1 application on the tablet and the client code on the PC (i.e. sending data from the PC to the tablet) the connection cannot be established and I'm getting the error:
java.net.ConnectException: Connection timed out: connect
The tablet and the PC are in the same network.
What is wrong?
Server:
ServerSocket ss = new ServerSocket(13005);
Socket socket = ss.accept();
ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
Client:
socket = new Socket();
socket.connect(new InetSocketAddress("10.2.130.125", 13005));
oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
Edit:
I have now executed netstat -nap. The output is as follows (I have altered the foreign addresses slightly). 13005 seems not to be used.
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 417 0 10.2.130.125:36844 xxx.xxx.23.74:443 CLOSE_WAIT
tcp 417 0 10.2.130.125:35996 xxx.xxx.23.110:443 CLOSE_WAIT
tcp6 1 0 ::ffff:10.2.130.125:44994 ::ffff:xxx.xx.205.170:443 CLOSE_WA
IT
tcp6 0 0 ::ffff:10.2.130.125:43995 ::ffff:xx.xx.119.188:5228 ESTABL
ISHED
tcp6 1 0 ::ffff:10.2.130.125:42353 ::ffff:xx.xx.23.110:80 CLOSE_WAI
T
tcp6 1 0 ::ffff:10.2.130.125:35722 ::ffff:xx.xx.205.170:443 CLOSE_WA
IT
tcp6 1 0 ::ffff:10.2.130.125:48101 ::ffff:xx.xx.205.110:443 CLOSE_WA
IT
I have also executed nmap -sS -Pn -p- 10.2.130.125. The output is:
Starting Nmap 7.60 ( https://nmap.org ) at 2018-01-18 16:10 W. Europe Standard T
ime
Nmap scan report for public-docking-cx-0635.ethz.ch (10.2.130.125)
Host is up (0.059s latency).
Not shown: 65534 filtered ports
PORT STATE SERVICE
22/tcp closed ssh
Nmap done: 1 IP address (1 host up) scanned in 159.90 seconds
Here is the stacktrace from the client. It is just a timeout exception. From the server I don't get any exception.
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(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 inf.ethz.ch.streaming.server.main.Main$1.run(Main.java:164)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Maybe the port 13005 is being used, probably the last connection was not closed.
Connect your device via USB. Then go to the folder where you have adb and run the following:
adb shell
netstat -nap
This will show you all the active ports in the android device. You will be able to check if port 13005 is being used.
If you have lot of connections, then is better to apply grep to step 2.
netstat -na |grep 13005
Then try to use another port, for example 5001. If the port is not restricted and is free, you should be able to connect.
Are you sure that the port is available from outside?
You have to open the port in your router configuration.
Related
I realized an java application that connects to MySQL.
when i make my ip adress of my pc in the navigator of another pc in same local network ,I access to phpMyadmin and Wamp.
When i make 127.0.0.1 on the same pc (Wamp install) I connect to 127.0.0.1 from my java application and it works .
but the problem when I put my ip address in my java application either from my pc or from another pc in same local network , I can not manage to connect .
this is my file of configuration in the java application:
db.user=root
db.password=
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://192.168.1.59:3306/testfournisseur
Is there a manipulation to do?
This is the errors appear when i connect to the database:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 1 ms ago.
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:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:666)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1069)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
... 72 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2431)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:590)
... 74 more
I'm unfamiliar with MySQL on Windows but the most common reason I see for being unable to connect is that the bind-address in my.cnf is set to 127.0.0.1, if you change this to 0.0.0.0 or another accessible address then it should work fine.
I also noticed you specify port 80 in your properties example, MySQL runs on 3306 by default so changing the port to that (or omitting it) may work.
Edit: Windows' built-in firewall may be blocking the connection so it's definitely worth checking out.
I'm trying to make a simple pong game with multiplayer support, but when I try to connect to the server application with my external ip it fails.
What I've Tried
localhost, 127.0.0.1, and 192.168.0.10 all work.
When I check to see if my port is open with an external tool it always returns true right away if I have the server running.
Turning off firewall or adding exceptions hasn't helped.
pinging my external IP returns instant response.
Code for sockets creation in Java and exception
Server Socket Creation
serverSocket = new ServerSocket(7777);
for(;;){
socket = serverSocket.accept();
}
Client Socket Creation
socket = new Socket(IP, 7777);
Exception thrown by client
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 java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Client.<init>(Client.java:93)
at Client.main(Client.java:153)
From what you've told us, the setup looks like this:
You have port forwarded port 7777 from external IP to internal IP B
Your server listens on internal IP B and 127.0.0.1
Your client successfully connects to internal IP B:7777 and 127.0.0.1:7777
Your client does not connect if you point it to external IP:7777
This setup just does not work with most home routers/NAT gateways, they will not port forward a TCP connection destined for the external IP that comes from the internal network itself - it'll only port forward connections that actually comes from the outside(the internet).
Have you tried to accept the connection client yet?
By accept method from serverSocket
boolean isStopped = false;
while(!isStopped){
Socket clientSocket = serverSocket.accept();
Connection time out means your client doesn't get response of its request, possible problems are:
a) The IP/domain or port is incorrect
b) The IP/domain or port (i.e service) is down
c) The IP/domain is taking longer than your default timeout to respond
d) You have a firewall that is blocking requests or responses on whatever port you are using
e) You have a firewall that is blocking requests to that particular host
f) Your internet access is down
Note that firewalls and port or IP blocking may be in place by your ISP
while trying to create a new connection to rabbitmq running on a different server, I got the following error:
java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:406)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; reason: java.net.SocketException: Connection reset
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedByte(Unknown Source)
at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95)
at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:131)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:533)
Steps taken :
rabbitmq is running on the server.
server is specified
default port is specified
lsof -i tcp:5672
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
beam.smp 3084 rabbitmq 15u IPv6 18611 0t0 TCP *:amqp (LISTEN)
rabbitmqctl list_connections
Listing connections ...
guest client_server 55765 running
...done.
netstat -tapnl | grep 5672
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 3084/beam.smp
tcp 0 0 0.0.0.0:55672 0.0.0.0:* LISTEN 3084/beam.smp
tcp 0 0 :::5672 :::* LISTEN 3084/beam.smp
One of the possible reasons is that user you are connecting with to RabbitMQ has no rights to access to virtual hosts.
You can check this using Management Plugin (Admin tab).
Do not specify the default port as you have mentioned in your steps.
If you have not created virtual host on the actual server, where you are trying to connect, Do create a virtual host and give it admin permision.
Set the virtual host on the factory before creating the new connection, like factory.setVirtualHost("VIRTUAL_HOST_NAME_ON_SERVER");
Make sure username on the server on which you are trying to connect is Admin and have access to the Virtual Host you just created.
Specify your username and password along with virtual host, while getting the connection.
Start your application in Debug Mode, and check if it now passes, factory.newConection();
This should make your things work.
Got the same exception, and it worked for me.
If it still does not work paste your code snippet.
Check the host and port value
In application.properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
See RabbitMQ site is running on port 15672 whereas in code using amqp protocol.
You can check if the SSL/TLS support is enabled. Then use the instruction useSslProtocol :
ConnectionFactory factory = new ConnectionFactory();
factory.useSslProtocol();
I got Unable to connect to AMQP server error when create a new connection. It was confirmed that it was a problem with the firewall. I solved it by command systemctl stop firewalld.
I was trying to develop a p2p file transfer application on java and as for the beginning I decided to run some transfer tests using localhost, as for the server, between local drives by some codes I found on the internet marked as working. The problem is for every port number I tried so far(+20) I got a "connection refused" error. I've installed microsoft loopback adapter as precaution, yet couldn't find any way to solve it. Any help would be appreciated.
just in case, I'm writing some code part related to socket in the client class.
// localhost for testing
Socket sock = new Socket("127.0.0.1",15123);
System.out.println("Connecting...");
and here is the error message
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
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 Client.main(Client.java:12)
line 12 is Socket sock = servsock.accept();
Most often , I saw that this exception comes when there is no service available to listen.Try to use another(free) port and make it sure that your server is running.You can find free port by writing netstat -a -o -n in cmd(on windows)
I know that many people ask this question but I try many path solution but any one was correct.
So, I have problem with connect to my postgresql server(on CentOS 6.4 minimal version - I have there only postgresql server, ftp and ssh service) from remote client(in LAN). I have ssh connection so I think that problem can by with permission tcp/ip.
Java throw exception:
org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:19)
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 java.net.Socket.connect(Unknown Source)
at org.postgresql.core.PGStream.<init>(PGStream.java:60)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:101)
... 11 more
ifconfig:
eth0 Link encap:Ethernet HWaddr 08:00:27:F0:3A:F4
inet addr:192.168.1.18 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fef0:3af4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2677 errors:0 dropped:0 overruns:0 frame:0
TX packets:1437 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:251266 (245.3 KiB) TX bytes:214910 (209.8 KiB)
/var/lib/pgsql/9.2/data/pg_hba.conf - only main part of file
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
host all all 192.168.1.18 255.255.255.0 password
#hostssl all all 192.168.1.18 255.255.255.0 trust
#hostnossl all all 192.168.1.18 255.255.255.0 trust
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres 127.0.0.1/32 ident
#host replication postgres ::1/128 ident
host all all 192.168.1.18 255.255.255.255 password
host all all ::1 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff password
var/lib/pgsql/9.2/data/poistgresql.conf - only main part
# - Connection Settings -
listen_addresses = '*'
#'localhost,192.168.1.18'
# what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
# Note: Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directory = '' # (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour = off # advertise server via Bonjour
# (change requires restart)
and last debug info is comment netstat -ta
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:postgres *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
tcp 0 52 PC2.home:ssh Michal-laptop.home:26146 ESTABLISHED
tcp 0 0 PC2.home:ssh Michal-laptop.home:25917 ESTABLISHED
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:postgres *:* LISTEN
tcp 0 0 localhost:smtp *:* LISTEN
Can you help my? what I should do to allow connection?
Thank you for your time.
As you cannot telnet to your server port 5432, you are going to have to open port 5432 in the firewall.
Think it's something like System -> Administration -> Firewall or /usr/bin/system-config-firewall through the console.
In var/lib/pgsql/9.2/data/poistgresql.conf
Uncomment below line:
#port = 5432
You can change listening port if you want. Restart the service.