I can't connect to server and database with physical phone - android

I'm programming a password manager app with Android studio and spring-boot as back-end and Mysql as the database.
My server is on localhost:8080 on my computer and my database is also on localhost:3306.
My app functions and communicate well when it's on the emulator of Android Studio. But when I try to connect to it with my physical smartphone (Samsung Galaxy A80), it can't connect and I can't understand why. It should work.
I tried to change the ip adress to the one I have on my phone in the retrofit of Android Studio. and change the port but it didn't work.
application.properties in spring-boot server
spring.datasource.url=jdbc:mysql://localhost:3306/PasswordManager?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=SakeSaske77
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
This is the retrofit service setup on Android Studio
public class RetrofitService {
private Retrofit retrofit;
public RetrofitService() {
initializeRetrofit();
}
private void initializeRetrofit() {
retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.1.185" +
":8080")
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
}
public Retrofit getRetrofit() {
return retrofit;
}
}

because the localhost can be accessed directly only from the host which is your pc/laptop, your physical phone is connected to your machine, but not the localhost, you need a way to access the localhost of your machine from your phone, for this you need to use the adb commands, and specifically, you need to transfer the port of your machine to the phone with this command:
adb reverse tcp:8008 tcp:8080
Change 8080 with your localhost port.
if you're not installing the adb (& other SDK platform tools provided by the Android SDK), download it from here

Related

socket io server is not responding when android emulator or Samsung S9+ are trying to connect but working fine with Kindle device

We are trying to create an android application for video chat using socket.io. We are using node js for the server. The server is running on a laptop. I tried to connect to the server using an Android emulator on the same laptop. But the server is not responding. I thought we shouldn't try to connect the emulator running on the same laptop where the server is running. I tried with Samsung S9+ device but the same result, the server is not responding. But interestingly when I tried to connect to the server from the Kindle device, the server is responding and printing the log. I used the same code for Emulator, S9+, and Kindle devices. Only when kindle device is trying to connect, the server is responding. Below is the code.
Android code:
String SIGNALING_URI = "http://192.168.1.101:7000"; // Laptop IP where the server is running and port 7000 where the server is listening
try {
socket = IO.socket(SIGNALING_URI);
socket.on(CREATEOFFER, new Emitter.Listener() {
#Override
public void call(Object... args) {
createOffer = true;
peerConnection.createOffer(sdpObserver, new MediaConstraints());
}
});
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
Node.js (server) code:
var socketIO = require('socket.io');
var server = require('http').createServer().listen(7000, '0.0.0.0');
var io = socketIO.listen(server);
io.sockets.on('connection', function (client) {
console.log('new connection: ' + client.id);
client.on('offer', function (details) {
client.broadcast.emit('offer', details);
console.log('offer: ' + JSON.stringify(details));
});
client.on('answer', function (details) {
client.broadcast.emit('answer', details);
console.log('answer: ' + JSON.stringify(details));
});
client.on('candidate', function (details) {
client.broadcast.emit('candidate', details);
console.log('candidate: ' + JSON.stringify(details));
});
client.broadcast.emit('createoffer', {});
});
Server code should print "new connection: " log when a client is trying to connect. But it is not printing that log when Android emulator or S9+ device are trying to connect but the log is getting printed when Kindle is trying to connect. I tried to do this using web sockets instead of using socket io. The server is responding for kindle device only even using web sockets also. Can anyone please let me know if there is anything wrong in the code or do I need to change any settings on S9+ device and emulator to make it work?
Update 1:
It seems I am using socket.io-client:0.8.2 version. But on the server node js, it seems I am using socket.io 2.3.0. Does it create any problems. Where I can find compatible versions of socket io for java and node.js. Just wanted to make sure that compatibility is not causing this issue.
Update 2:
I am able to see "new connection: " on the console multiple times with different client IDs. But these logs are not generated when I was trying to connect on Emulator or S9+ device. To make sure, I tried to print socket ID on client side after IO.socket(SIGNALING_URI) by using socket.id() which is printing null on the client side.

Debug a server for android on nodejs

I'm trying to create a node js server for android phones. How can I create it at home in a local network without outer internet connections? I have wifi at home so my phone can connect to local network. I use official socket.io tutorial and I don't know what to write here (instead of http://chat.socket.io):
private Socket mSocket;
{
try {
mSocket = IO.socket("http://chat.socket.io");
} catch (URISyntaxException e) {}
}
open command prompt on your PC and hit ipconfig. note down your wireless IP address. Let's assume it to be 192.168.1.10. you can also set a static IP for your PC on your router. Next from your socket io code check what port number you are using. for example
http.listen(3000, function(){
console.log('listening on *:3000');
says that you are using port 3000.
Hence in your Android code you need to use http://192.168.1.10:3000/

cannot connect server when is in android phone

I'm working on a google cardboard project (Unity that will run on android phone)
I created a server in the Unity program that opens like this:
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
TcpListener client = listener.AcceptTcpClient();
Everything on backgroundworkers and nice things.
Then I connect from another piece of code running in a PC (WPF C# code). If Im running both programs in the PC I can connect flawlessly but when I compile for android and move the server to the phone, I cannot connect to it from the PC
When I try to perform:
host = Dns.GetHostEntry("192.168.1.8");
I get: Unknown host
The IP is correct (I made the server in the phone show it on screen, and I can ping to it) I think the problem is more related to the thing being in a different machine than to the fat that it is compiled for android but nevertheless.. here you have the full story.
Any help ?
Not sure what it was becauseI moved to UDP messages, and now it works.

QTcpServer - Unsupported Socket Operation on Android

I am trying to start a server on Android using QTcpServer with Qt 5.3.1 but the server does not start and I get "Unsupported Socket Operation". It works fine on Windows.
Code below:
void StartListening()
{
QHostAddress hostAddress;
hostAddress.setAddress(QString("localhost"));
hostAddress.toIPv4Address();
quint16 portNumber = 9878;
server->setMaxPendingConnections(1);
server->setProxy(QNetworkProxy::NoProxy);
if (server->listen(hostAddress, portNumber))
{
// Ok
}
else
{
Debug("Server did not start. " + server->errorString());
}
}
server->errorString() returns "Unsupported Socket Operation when it runs on Android
Isn't this supported by Qt Android or am I doing something wrong?
Thx
OK! I worked it out.
The problem is with this line:
hostAddress.setAddress(QString("localhost"));
If I replace "localhost" with "127.0.0.1", the server starts fine but no one outside the "device the server is running on" can connect to it. This means, let's say your network is using 192.168.1.xx and your Android device has the following IP address: 192.168.1.2. If you start the server with "127.0.0.1" on your Android device which has an IP address: "192.168.1.2" and then using your PC with an IP address 192.168.1.3 you do telnet 192.168.1.2 9878 it will fail to connect!
So then I decided to start the server by specifying the IP address of the Android device: 192.168.1.2
hostAddress.setAddress(QString("192.168.1.2"));
Voila! That works too! The server starts and I can connect from outside the device! For example if I do telnet 192.168.1.2 9878 from my PC while the server is started on Android, it connects! So all I need to do now is replace the hard coded IP address with the actual IP address of the device! I think QNetworkInterface::allInterfaces() or something like that will give me the ability to get the default IP address.
So just use the actual IP address of the network card rather than localhost or 127.0.0.1 and all should be Ok. All working now.

Can't connect to local web server from Android device

I've connected my Android phone to my laptop using Connectify.
I have a cherrypy web server running on 192.168.210.1:8080:
import cherrypy
class HelloWorld:
def index(self):
return "Hello world!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
It works on my laptop, but when I try to put 192.168.1.8080 in my Android, there's no response and it keeps waiting indefinitely. I tried pinging to the IP from my phone, and it works, showing replies.
I also tried turning Windows Firewall off - it instanantly results in the message "Oops! Google Chrome could not connect to 192.168.210.1:8080". If I start it again, it's back to the previous state.
Please help.
You need to bind the socket on which the server is listen to '0.0.0.0', by default is bound to localhost, to do that just change the quickstart call, with this config:
config = {'global':
{'server.socket_host': '0.0.0.0'}
}
quickstart(HelloWorld(), config=config)

Categories

Resources