Unable to connect to gnatsd server

259 views
Skip to first unread message

AK

unread,
Mar 28, 2019, 10:02:07 AM3/28/19
to nats
Hi, 

I've been trying to setup Android NATS client using Java Android Client https://nats.io/download/kedzie/nats-android/.

It giving me an error of java.io.IOException: Unable to connect to gnatsd server. when I execute it.

Here's the code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
Connection nc = Nats.connect("nats://10.0.1.1:4222");
Log.d("MyApp", nc.getServers().toString());
Log.d("MyApp", nc.getStatus().toString());

Subscription sub = nc.subscribe("updates");

Message msg = sub.nextMessage(Duration.ZERO);

String str = new String(msg.getData(), StandardCharsets.UTF_8);
Log.d("MyApp",str);

nc.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
}

The error indicate it's coming from this line:
Connection nc = Nats.connect("nats://10.0.1.1:4222");

However, if I use this program in Kotlin it works perfectly.

val nc = Nats.connect("nats://10.0.1.1:4222")

val sub = nc.subscribe("updates")
val msg = sub.nextMessage(Duration.ZERO)

val response = String(msg.getData(), StandardCharsets.UTF_8)

Could anyone help me ?

Thanks.

-ak-





co...@synadia.com

unread,
Mar 28, 2019, 11:37:54 AM3/28/19
to nats
Hi, 

This looks like your application cannot find the NATS server running on 10.0.0.1.  

1. Can you ping 10.0.0.1?
2. Could you provide the output from your nats server?  If you run it with the -DV flags, you'll get debug output to see if you are connecting.
3. You can test with telnet.
    $ telnet 10.0.1.1 4222

You'll get the NATS connect string back, e.g. "INFO {"server_id":"21ePTjUtrmmNB7NgXXu8u3","version":"1.4.0"..."

Regards,
Colin

AK

unread,
Mar 28, 2019, 7:28:02 PM3/28/19
to nats
Hi Colin, 

Please see my answer below.

Regards,

-ak-

On Thursday, March 28, 2019 at 10:37:54 PM UTC+7, co...@synadia.com wrote:
Hi, 

This looks like your application cannot find the NATS server running on 10.0.0.1.  

1. Can you ping 10.0.0.1?

Yes, I can ping the server.
α ping 10.0.1.197
PING 10.0.1.197 (10.0.1.197): 56 data bytes
64 bytes from 10.0.1.197: icmp_seq=0 ttl=64 time=0.055 ms
64 bytes from 10.0.1.197: icmp_seq=1 ttl=64 time=0.089 ms
64 bytes from 10.0.1.197: icmp_seq=2 ttl=64 time=0.100 ms
 
2. Could you provide the output from your nats server?  If you run it with the -DV flags, you'll get debug output to see if you are connecting.

α gnatsd -DV
[63735] 2019/03/29 06:13:38.486049 [INF] Starting nats-server version 2.0.0-RC5
[63735] 2019/03/29 06:13:38.487173 [DBG] Go build version go1.11.4
[63735] 2019/03/29 06:13:38.487179 [INF] Git commit [not set]
[63735] 2019/03/29 06:13:38.487407 [INF] Listening for client connections on 0.0.0.0:4222
[63735] 2019/03/29 06:13:38.487417 [INF] Server id is NDMK34NH52MLBR3GMFOXNHAP5MQ2RISLRBHJ37LQZDVT4GSYTLHSSBCD
[63735] 2019/03/29 06:13:38.487421 [INF] Server is ready
[63735] 2019/03/29 06:13:38.487436 [DBG] Get non local IPs for "0.0.0.0"
[63735] 2019/03/29 06:13:38.487660 [DBG] ip=10.0.1.197
[63735] 2019/03/29 06:13:38.543498 [DBG] 10.0.1.195:52101 - cid:1 - Client connection created
[63735] 2019/03/29 06:13:38.547176 [TRC] 10.0.1.195:52101 - cid:1 - <<- [CONNECT {"lang":"java","version":"2.4.2","protocol":1,"verbose":false,"pedantic":false,"tls_required":false,"echo":true}]
[63735] 2019/03/29 06:13:38.547389 [TRC] 10.0.1.195:52101 - cid:1 - <<- [PING]
[63735] 2019/03/29 06:13:38.547403 [TRC] 10.0.1.195:52101 - cid:1 - ->> [PONG]
[63735] 2019/03/29 06:13:38.557684 [TRC] 10.0.1.195:52101 - cid:1 - <<- [SUB updates 1]
[63735] 2019/03/29 06:13:38.557753 [TRC] 10.0.1.195:52101 - cid:1 - <<- [PING]
[63735] 2019/03/29 06:13:38.557765 [TRC] 10.0.1.195:52101 - cid:1 - ->> [PONG]
[63735] 2019/03/29 06:13:40.362423 [DBG] 10.0.1.197:49435 - cid:2 - Client connection created
[63735] 2019/03/29 06:13:40.362441 [DBG] 10.0.1.197:49434 - cid:3 - Client connection created
[63735] 2019/03/29 06:13:40.362654 [DBG] 10.0.1.197:49436 - cid:4 - Client connection created
[63735] 2019/03/29 06:13:40.365779 [DBG] 10.0.1.197:49437 - cid:5 - Client connection created
[63735] 2019/03/29 06:13:40.390765 [TRC] 10.0.1.197:49436 - cid:4 - <<- [CONNECT {"lang":"java","version":"2.4.1","protocol":1,"verbose":false,"pedantic":false,"tls_required":false,"echo":true}]
[63735] 2019/03/29 06:13:40.390856 [TRC] 10.0.1.197:49436 - cid:4 - <<- [PING]
[63735] 2019/03/29 06:13:40.390869 [TRC] 10.0.1.197:49436 - cid:4 - ->> [PONG]
[63735] 2019/03/29 06:13:40.397163 [TRC] 10.0.1.197:49434 - cid:3 - <<- [CONNECT {"lang":"java","version":"2.4.1","protocol":1,"verbose":false,"pedantic":false,"tls_required":false,"echo":true}]
[63735] 2019/03/29 06:13:40.397217 [TRC] 10.0.1.197:49434 - cid:3 - <<- [PING]
[63735] 2019/03/29 06:13:40.397228 [TRC] 10.0.1.197:49434 - cid:3 - ->> [PONG]
[63735] 2019/03/29 06:13:40.400706 [TRC] 10.0.1.197:49436 - cid:4 - <<- [SUB updates 1]
[63735] 2019/03/29 06:13:40.400760 [TRC] 10.0.1.197:49436 - cid:4 - <<- [PING]
[63735] 2019/03/29 06:13:40.400798 [TRC] 10.0.1.197:49436 - cid:4 - ->> [PONG]
[63735] 2019/03/29 06:13:40.402145 [TRC] 10.0.1.197:49434 - cid:3 - <<- [SUB updates 1]
[63735] 2019/03/29 06:13:40.402192 [TRC] 10.0.1.197:49434 - cid:3 - <<- [PING]
[63735] 2019/03/29 06:13:40.402206 [TRC] 10.0.1.197:49434 - cid:3 - ->> [PONG]
[63735] 2019/03/29 06:13:40.403740 [TRC] 10.0.1.197:49435 - cid:2 - <<- [CONNECT {"lang":"java","version":"2.4.1","protocol":1,"verbose":false,"pedantic":false,"tls_required":false,"echo":true}]
[63735] 2019/03/29 06:13:40.403801 [TRC] 10.0.1.197:49435 - cid:2 - <<- [PING]
[63735] 2019/03/29 06:13:40.403814 [TRC] 10.0.1.197:49435 - cid:2 - ->> [PONG]
[63735] 2019/03/29 06:13:40.407106 [TRC] 10.0.1.197:49435 - cid:2 - <<- [SUB updates 1]
[63735] 2019/03/29 06:13:40.407141 [TRC] 10.0.1.197:49435 - cid:2 - <<- [PING]
[63735] 2019/03/29 06:13:40.407149 [TRC] 10.0.1.197:49435 - cid:2 - ->> [PONG]
[63735] 2019/03/29 06:13:40.412971 [TRC] 10.0.1.197:49437 - cid:5 - <<- [CONNECT {"lang":"java","version":"2.4.1","protocol":1,"verbose":false,"pedantic":false,"tls_required":false,"echo":true}]
[63735] 2019/03/29 06:13:40.413056 [TRC] 10.0.1.197:49437 - cid:5 - <<- [PING]
[63735] 2019/03/29 06:13:40.413071 [TRC] 10.0.1.197:49437 - cid:5 - ->> [PONG]
[63735] 2019/03/29 06:13:40.416136 [TRC] 10.0.1.197:49437 - cid:5 - <<- [SUB updates 1]
[63735] 2019/03/29 06:13:40.416964 [TRC] 10.0.1.197:49437 - cid:5 - <<- [PING]
[63735] 2019/03/29 06:13:40.416993 [TRC] 10.0.1.197:49437 - cid:5 - ->> [PONG]
[63735] 2019/03/29 06:13:41.442624 [TRC] 10.0.1.197:49436 - cid:4 - <<- [PING]
[63735] 2019/03/29 06:13:41.442649 [TRC] 10.0.1.197:49436 - cid:4 - ->> [PONG]
[63735] 2019/03/29 06:13:43.826024 [TRC] 10.0.1.195:52101 - cid:1 - <<- [PING]
[63735] 2019/03/29 06:13:43.826057 [TRC] 10.0.1.195:52101 - cid:1 - ->> [PONG]
[63735] 2019/03/29 06:13:48.705667 [DBG] 10.0.1.197:49436 - cid:4 - Client connection closed
[63735] 2019/03/29 06:13:48.707272 [DBG] 10.0.1.197:49434 - cid:3 - Client connection closed
[63735] 2019/03/29 06:13:48.707630 [DBG] 10.0.1.197:49437 - cid:5 - Client connection closed
[63735] 2019/03/29 06:13:48.707635 [DBG] 10.0.1.197:49435 - cid:2 - Client connection closed
^C[63735] 2019/03/29 06:14:49.415202 [DBG] Trapped "interrupt" signal
[63735] 2019/03/29 06:14:49.415223 [INF] Server Exiting..

 
3. You can test with telnet.

α telnet 10.0.1.197 4222
Trying 10.0.1.197...
Connected to 10.0.1.197.
Escape character is '^]'.
INFO {"server_id":"NB23DNOKH6HLKLLKBNCUUFCRT6ABE5U27M4KMX2WWGTTCNLL7ZEMSZVD","version":"2.0.0-RC5","proto":1,"go":"go1.11.4","host":"0.0.0.0","port":4222,"max_payload":1048576,"client_id":4}
^]
telnet> q
        Connection closed. 

co...@synadia.com

unread,
Mar 29, 2019, 11:30:25 AM3/29/19
to nats
In this case your server IP is 10.0.1.197...
   
        [63735] 2019/03/29 06:13:38.487436 [DBG] Get non local IPs for "0.0.0.0"
        [63735] 2019/03/29 06:13:38.487660 [DBG]  ip=10.0.1.197

...but your application is attempting to connect to 10.0.0.1 according to the code...  Does your application connect if you change the connect url to use 10.0.1.197?

e.g. 
  Connection nc = Nats.connect("nats://10.0.1.197:4222");

Aryo Karbhawono

unread,
Mar 29, 2019, 12:13:57 PM3/29/19
to nat...@googlegroups.com

Hi Colin,

 

Please don’t mind the address on the code cause I changed the server to different address which is 10.0.1.197, so the code is already connected to nats://10.0.1.197

 

Here’s the server code:

 

package main

import (
       
"log"
       
nats "github.com/nats-io/go-nats"
)
func main() {
        nc
, err := nats.Connect("nats://10.0.1.197:4222")
       
if err != nil {
                 log.Fatal(err)
        }
        defer nc.Close()

       
if err := nc.Publish("updates", []byte("OK")); err != nil {
                 log.Fatal(err)
        }
       

        
nc.Flush()
}

 

It’s connected perfectly using Kotlin version to the server address above, here’s the server log:

 

α gnatsd -a 10.0.1.197 -DV
        [
76051] 2019/03/29 22:39:52.049529 [INF] Starting nats-server version 2.0.0-RC5
        [
76051] 2019/03/29 22:39:52.049849 [DBG] Go build version go1.11.4
       
[76051] 2019/03/29 22:39:52.049860 [INF] Git commit [not set]
        [
76051] 2019/03/29 22:39:52.050229 [INF] Listening for client connections on 10.0.1.197:4222
       
[76051] 2019/03/29 22:39:52.050246 [INF] Server id is NBASFZNQWDE2YGXLUT6ZP7X52UCBIEYEDZRJRN6AXVNQFLXE36PKDEZ4
        [
76051] 2019/03/29 22:39:52.050254 [INF] Server is ready
        [
76051] 2019/03/29 22:40:20.446377 [DBG] 10.0.1.197:50041 - cid:1 - Client connection created
        [
76051] 2019/03/29 22:40:20.484516 [TRC] 10.0.1.197:50041 - cid:1 - <<- [CONNECT {"lang":"java","version":"2.4.1","protocol":1,"verbose":false,"pedantic":false,"tls_required":false,"echo":true}]
        [
76051] 2019/03/29 22:40:20.484764 [TRC] 10.0.1.197:50041 - cid:1 - <<- [PING]
        [
76051] 2019/03/29 22:40:20.484779 [TRC] 10.0.1.197:50041 - cid:1 - ->> [PONG]
        [
76051] 2019/03/29 22:40:20.521737 [TRC] 10.0.1.197:50041 - cid:1 - <<- [SUB updates 1]
        [
76051] 2019/03/29 22:40:32.962686 [DBG] 10.0.1.197:50051 - cid:2 - Client connection created
        [
76051] 2019/03/29 22:40:32.963471 [TRC] 10.0.1.197:50051 - cid:2 - <<- [CONNECT {"verbose":false,"pedantic":false,"tls_required":false,"name":"","lang":"go","version":"1.7.0","protocol":1,"echo":true}]
        [
76051] 2019/03/29 22:40:32.963554 [TRC] 10.0.1.197:50051 - cid:2 - <<- [PING]
        [
76051] 2019/03/29 22:40:32.963568 [TRC] 10.0.1.197:50051 - cid:2 - ->> [PONG]
        [
76051] 2019/03/29 22:40:32.963871 [TRC] 10.0.1.197:50051 - cid:2 - <<- [PUB updates 2]
        [
76051] 2019/03/29 22:40:32.963943 [TRC] 10.0.1.197:50051 - cid:2 - <<- MSG_PAYLOAD: ["OK"]
        [
76051] 2019/03/29 22:40:32.964418 [TRC] 10.0.1.197:50041 - cid:1 - ->> [MSG updates 1 2]
        [
76051] 2019/03/29 22:40:32.964512 [TRC] 10.0.1.197:50051 - cid:2 - <<- [PING]
        [
76051] 2019/03/29 22:40:32.964523 [TRC] 10.0.1.197:50051 - cid:2 - ->> [PONG]
        [
76051] 2019/03/29 22:40:32.964838 [DBG] 10.0.1.197:50051 - cid:2 - Client connection closed
        [
76051] 2019/03/29 22:40:32.976800 [DBG] 10.0.1.197:50052 - cid:3 - Client connection created
        [
76051] 2019/03/29 22:40:32.982610 [TRC] 10.0.1.197:50052 - cid:3 - <<- [CONNECT {"lang":"java","version":"2.4.1","protocol":1,"verbose":false,"pedantic":false,"tls_required":false,"echo":true}]
        [
76051] 2019/03/29 22:40:32.982676 [TRC] 10.0.1.197:50052 - cid:3 - <<- [PING]
        [
76051] 2019/03/29 22:40:32.982690 [TRC] 10.0.1.197:50052 - cid:3 - ->> [PONG]
        [
76051] 2019/03/29 22:40:32.986085 [TRC] 10.0.1.197:50052 - cid:3 - <<- [SUB updates 1]

 

 

Meanwhile, on the Java version. I have the following  IOException connecting to the same server nats://10.0.1.197:4222

W/System.err: java.io.IOException: Unable to connect to gnatsd server.
W/System.err:     at io.nats.client.impl.NatsConnection.connect(NatsConnection.java:
181)
W/System.err:     at io.nats.client.impl.NatsImpl.createConnection(NatsImpl.java:
29)
W/System.err:     at io.nats.client.Nats.createConnection(Nats.java:
218)
W/System.err:     at io.nats.client.Nats.connect(Nats.java:
127)
W/System.err:     at com.testnats.MainActivity.onCreate(MainActivity.java:
23)

 

The error MainActivity.java:23 is indicate this line:

Connection nc = Nats.connect("nats://10.0.1.197:4222");

 

Here’s the android code:

try {
    Connection nc = Nats.connect(
"nats://10.0.1.197:4222");
   
Log.d("MyApp", nc.getServers().toString());
   
Log.d("MyApp", nc.getStatus().toString());
   
Subscription sub = nc.subscribe("updates");
    
Message msg = sub.nextMessage(Duration.ZERO);
   
String str = new String(msg.getData(), StandardCharsets.UTF_8);
   
Log.d("MyApp",str);
   
nc.close();
}catch (IOException ex){
    ex.printStackTrace()
;
}catch (InterruptedException iex){
    iex.printStackTrace()
;
}

 

On the Android log related to:

Log.d("MyApp", nc.getServers().toString());

 

W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Collection io.nats.client.Connection.getServers()' on a null object reference

 

And nothing on the server log:

α gnatsd -a 10.0.1.197 -DV
    [
76145] 2019/03/29 22:45:07.421938 [INF] Starting nats-server version 2.0.0-RC5
    [
76145] 2019/03/29 22:45:07.422269 [DBG] Go build version go1.11.4
   
[76145] 2019/03/29 22:45:07.422275 [INF] Git commit [not set]
    [
76145] 2019/03/29 22:45:07.424518 [INF] Listening for client connections on 10.0.1.197:4222
   
[76145] 2019/03/29 22:45:07.424538 [INF] Server id is NCWFHNDUT3DRLNWRWJZS5PQAOTHN435IBG2EXOJF3PNTW5TBDU74PMZ5
    [
76145] 2019/03/29 22:45:07.424543 [INF] Server is ready

 

It seem it doesn’t connect to the server.

 

I’ve put this in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

Regards,

 

-ak-

--
You received this message because you are subscribed to the Google Groups "nats" group.
To unsubscribe from this group and stop receiving emails from it, send an email to natsio+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

AK

unread,
Apr 2, 2019, 3:15:21 AM4/2/19
to nats
Hi, 

I solved it by implementing a thread.

Thank you.
-ak-

To unsubscribe from this group and stop receiving emails from it, send an email to natsio+unsubscribe@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages