IP address for connecting server on the same LAN using SignalR - java

I'm trying to create a SignalR connection between an ASP.NET server (SignalR-Server 3.0.0) and an Android client on a real device. So far I've managed to successfully establish a connection on the same computer using a console application client targeting the IP http://127.0.0.1:5000/.
The server computer and the Android device are both connected to the same LAN (over Wi-Fi), so I tried to set the target IP to be the server computer LAN IP, but an exception is thrown:
java.net.SocketTimeoutException: failed to connect to /192.168.14.167
(port 5000) after 15000ms
Here's the relevant client app, using SignalR Java client:
Platform.loadPlatformComponent(new AndroidPlatformComponent());
String serverUrl = "http://192.168.14.167:5000/signalr";
mHubConnection = new HubConnection(serverUrl);
mHubProxy = mHubConnection.createHubProxy("Hub");
ClientTransport clientTransport = new ServerSentEventsTransport(mHubConnection.getLogger());
SignalRFuture<Void> signalRFuture = mHubConnection.start(clientTransport);
try {
signalRFuture.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return;
}
Am I missing anything/doing anything wrong? Any help will be appreciated.

Related

How to avoid hard coding the IP of my server on my server and client file

I have a server running in a machine (lets call it MACHINE1). In this file, I have created the start_server = websockets.serve(recv, "serverIP", port). The server is correctly running.
On the other side, I have one android application running working as a client connected to the server (note that both devices are running in the same network)
try {
uri = new URI("ws://" + serverIP + ":port");
} catch (URISyntaxException e) {
e.printStackTrace();
}
mWebSocketClient = new WebSocketClient(uri) {
// my code
}
As I mentioned below, currently the solution is setting serverIP to the IP of MACHINE1 (192.168...), and therefore, if I change the machine to another IP the connection will fail. Is there any way to create a general solution? I tried using localhost but I did not manage to connect to MACHINE1

AppEngine gets "Communication link failure" error while trying to connect to Google Cloud MySql instance

I have a Java/Jetty based appengine project that successfully connects to Google cloud MySql server when using TCP connection with JDBC, BUT fails to connect to the same server when trying to make it through Instance name and JDBC socketFactory.
the error I am getting in the GCP debug console:
"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."
try {
Class.forName("com.mysql.jdbc.GoogleDriver");
String url= String.format("jdbc:google:mysql:///"
+ "MY_DB_NAME" + "?"
+ "cloudSqlInstance=%s&"
+ "socketFactory=%s&"
+ "useSSL=false",
"PROJECT-ID:ZONE:INSTANCE_NAME",
"com.google.cloud.sql.mysql.SocketFactory");
return DriverManager.getConnection(url, USER_NAME, USER_PASSWORD);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
The problem might be in the public IP address that you specify in the cloud mysql instance. Just check the public IP address by typing What is my public IP? in the browser. Then update the public IP given for the google cloud instance. This might be help to solve your issue.

Server java app not working on a local connection

hi have at home a rasperry pi running a server java app, connected to de router with the dynamic DNS configured and the in/out communication ports openned.
When i run the android apication client througt 4g everithing is working sucessfull. But when i run the same app connected to the wifi on my local net, where the server are running, the server application looks like death.
Any ideas?
Thanks.
router config
I think the problem is related to your DNS. If you are connected using your wifi, you have to return a local IP-Address. Do you have any chance of configuring your router to return the raspberries local IP-Address for wifi-clients?
Take a look here: link
A simple solution, even if it is not very elegant, is adding the following conditions to your code:
If there is an error connecting to the Dyn DNS, try to connect to the local IP address. (In case you are in the Wifi LAN)
If the local IP address fails, try again your Dyn DNS (in case the user is a real user with real communication problems)
(repeat until the connection is successful)
You can also identify your testing devices (using Settings.Secure.ANDROID_ID, or the IMEI) and use the local IP only for them. Another option is making the URL configurable (with a hidden option for example).
Because of my app can run local, only needs to connect to server for updates. I have block the connection on the server app if the ip from client and server are equals.
At the moment is the best solution to keep the server app running properly.
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new
InputStreamReader(whatismyip.openStream()));
String ip = in.readLine();
try {
SSLServerSocketFactory sslFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) sslFactory.createServerSocket(PORT);
int idSession = 0;
while (true) {
SSLSocket socket = (SSLSocket)ss.accept();
if(socket.getInetAddress().getHostAddress().equals(ip)){
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
((ServidorThread) new ServidorThread(socket, idSession)).start();
idSession++;
}
} catch (IOException ex) {
ex.printStackTrace(System.out);
Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
}
}

Java bluecove: connection to a remote service, timeout error [10060]

I am using Java Bluecove to establish a connection between my pc and a remote bluetooth device.
I use a a DiscoveryListener object to discover the URL of a certain service on the device; then I use that string URL as argument of Connector.open(URL) to open the connection but I get:
Connection timeout; [10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to
respond.
Here are two snippets from my program. The first one searches for a specified set of services
UUID[] uuidSet = new UUID[1];
uuidSet[0] = new UUID(0x1101); //serial port
int[] attrIDs = new int[]{0x0100};
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
agent.searchServices(attrIDs, uuidSet, device, transport); //if the specified service is found its URL is print to screen; so I know it
The following code is to establish the connection.
String bt_addr = remDev.getBluetoothAddress(); //remDev is the remote device; I am sure it's the correct one
try {
connection = (StreamConnection) Connector.open("btspp://" + bt_addr + ":1;authenticate=false;encrypt=false;master=true;"); //HERE OCCURS THE TIMEOUT !
} catch (javax.bluetooth.BluetoothConnectionException e) {}
Now, it may be that there are other services on the meter but I only look for 0x1101 which is not the one I have to use to connect to. But I don't know how to look for different services.

Socket writing java (android) to python

I want to create a super basic Android App that connects to a python server running on my PC but the python server never gets the connection
my java code:
public class WriteToSocket {
Socket sock;
public void Test() {
try {
this.sock = new Socket("PCName", 9871);
} catch (UnknownHostException e) {
System.out.println("Unknown host: PCName");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
public void Test1(){
try {
this.sock.close();
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
and
public void onClick(View v) {
WriteToSocket a = new WriteToSocket();
a.Test();
}
and my python server is
import socket
sock = socket.socket()
name = "PCName"
port = 9871
sock.bind((name,port))
sock.listen(1)
s,a = sock.accept()
I expected after the button click for the python server to accept the connection (I also tried changing "PCName" to "127.0.0.1")
I've looked around but nothing helped me so far :S
Bind your server socket to one of the IP addresses of your PC which is accessible from your android, and not to 127.0.0.1. Or alternatively bind it to all available interfaces (0.0.0.0).
Then connect from your android to that IP.
E.g. if your PC has IP address 1.2.3.4 then use this IP in both applications.
Use netstat to see if the port is really open on your PC.
Check to see if your android application has the permission to use the internet (specified in the manifest: "USES_INTERNET" or something like that).
Also your python script discards the connection as soon as it is made.
In python change bind address to 0.0.0.0. It will bind for all IPs attached to your machine. Then in android app change to correct IP of your computer.
IP 127.0.0.1 is a loopback and you can't connect to it from outside of the system.
The android phone doesn't know what PCName is, change "PCName" in the python code back to '127.0.0.1', then in the android project put in the local IP address of the server.
This of course assuming that both the phone and the server are on the same local network.

Categories

Resources