QTcpServer - Unsupported Socket Operation on Android - 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.

Related

Flutter "Connection Refused" (errno=111) when trying to send an http get request to from physical device to pc

I built a flutter app which communicates with a web server that I wrote with flask. Everything works as intended without any errors if I use a virtual device. As soon as I try it in release mode on a physical device I get problems when it comes to the communication with the server
The only thing that I changed when using a physical device is the ip. I use 10.0.2.2 on the virtual device and my computers ip4 adress - that I get with ipconfig in windows 10 - on the physical device
Both devices are in the same network connected to the same router
Internet Permission is enabled in the AndroidManifest for all modes (Debug, Main, Profile)
I even disabled the firewall
The line that causes the issue is
await http.get(url).timeout(Duration(seconds: 15), onTimeout: () {
// Handle timeout
// This entire thing is in a try-catch block in an async function
});
In debug mode on the physical device when the HTTP get request is sent VSCode immediately says
Exception has occurred.
SocketException (SocketException: OS Error: Connection refused, errno = 111, address = 192.168.178.20, port = 43378)
First Question: Why Port 43378? Is that the port the HTTP request is sent to? Because when I run the flask app it says:
Running on http://127.0.0.1:5000/
Could that be the issue? I would have expected the exception to say the port is 5000 as declared in the URL. Or do I have to change something with how I set up the flask app? Currently it is the development server because I am still testing before I pay money and deploy
However I hope I didnt forget any important information. Any advice on what could be wrong or how to debug here is highly aprecciated
Pass an Uri object to http.get func. Uri classes let you specify the port as Documentation https://api.dart.dev/stable/2.12.0/dart-core/Uri-class.html

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)

Bluecove on PC cannot detect connected android (galaxy tab 7.0)

I use this code
luugiathuy.com/2011/02/android-java-bluetooth/
The server side is the PC
the client is the device, with the app based on bluetooth chat example
The device (galaxy tab 7.0) can establish connection with the PC.
However the PC server (written in java and bluecove) did nothing, as nothing is connected.
The loop for trying to find connected device is
while(true) {
try {
System.out.println("waiting for connection...");
connection = notifier.acceptAndOpen();
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();
} catch (Exception e) {
e.printStackTrace();
return;
}
Output on PC:
uuid: 0000110100001000800000805f9b34fb
waiting for connection...
EDIT: source downloadhttps://github.com/luugiathuy/Remote-Bluetooth-Android
Same issue I got when I was trying in linux. But the reason (still not sure) when you run the bluetooth android application without turning on the Java server using bluecove, It will try to connect with the already installed bluetooth software. You may see the bluetooth icon asking for granting access to the mobile device.
To solve this, I just changed the uuid in the server and application (say from 1103 to 1101 and vice versa) and then started the server first and then the android application. Java server part started listening.
The reason I think may be the uuid when it did not found the bluecove stack service server, it got connected to the device server listening on same uuid. So after changing the uuid and making sure that the server is running before launching the android application should solve the issue.
If you are getting connected to the bluetooth system application and not to the Java bluecove server,
1) First change the uuid both server and android application.
2) Second make sure your server is running and listening on same uuid.
3) Launch the android application which try to communicate on same rfcomm connection uuid.
Server part code I took from : http://www.jsr82.com/jsr-82-sample-spp-server-and-client/
Library : http://code.google.com/p/bluecove/downloads/list
Yes, it happens with me too, I suggest you to fire following commend on shell, when it shows waiting for connection.
hcitool cc 58:C3:8B:D7:FA:F4
here 58:C3:8B:D7:FA:F4 is my device's bluetooth address, which should be replaced by your device's bluetooth address.
To get your device's bluetooth address, just start bluetooth in your device with discoverable mode and execute hcitool scan command, it will display all the active device with their name and bluetooth address.
Well you may run the above hcitool cc 58:C3:8B:D7:FA:F4 command via Java code as follows,
try
{
Process p=Runtime.getRuntime().exec("hcitool cc 58:C3:8B:D7:FA:F4");
}
catch ( Exception e )
{
}
The output from your program says it listens on UUID 0x1101. Is that true? The sample you reference shows it listening on a different UUID. Its Service Class Id is 0x04c6093b and is set as follows:
34 UUID uuid = new UUID(80087355); // "04c6093b-0000-1000-8000-00805f9b34fb"
35 String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
36 notifier = (StreamConnectionNotifier)Connector.open(url);
The two need to match on client and server.

java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused

I am using xampp apache server to serve resources to the application from my machine. But i am getting the above error.
I got something on the google. pointing towards possible solution here
http://groups.google.com/group/android-beginners/browse_thread/thread/599a06416fb37b4a
What is the solution for the above problem?
Since you have not specified you are connected to a server from the device or emulator so I guess you are using your application in the emulator.
If you are referring your localhost on your system from the Android emulator then you have to use http://10.0.2.2:8080/ Because Android emulator runs in a Virtual Machine therefore here 127.0.0.1 or localhost will be emulator's own loopback address.
Refer: Emulator Networking
in android
Replace: String webServiceUrl = "http://localhost:8080/Service1.asmx"
With : String webServiceUrl = "http://10.0.2.2:8080/Service1.asmx"
Good luck!
localhost and 127.0.0.1 are both ways of saying 'the current machine'. So localhost on your PC is the PC and localhost on the android is the phone. Since your phone isn't running a webserver of course it will refuse the connection.
You need to get the IP address of your machine (use ipconfig on windows to find out) and use that instead of 127.0.0.1. This may still not working depending on how your network/firewalls are set up. But that is a completely different topic.
Add Internet permission in Androidmanifest.xml file
uses-permission android:name="android.permission.INTERNET
Open cmd in windows
type "ipconfig" then press enter
find IPv4 Address. . . . . . . . . . . : 192.168.X.X
use this URL
"http://192.168.X.X:your_virtual_server_port/your_service.php"
You just have to use your local (but real) IP address and port number like this:
String webServiceUrl = "http://192.168.X.X:your_virtual_server_port/your_service.php"
And make sure you did set the internet permission within the manifest
<uses-permission android:name="android.permission.INTERNET" />
If you are using localhost in your url and testing your application in emulator , simply you can replace system's ip address for localhost in the URL.or you can use 10.0.2.2 instead of localhost.
http://localhost/webservice.php to http://10.218.28.19/webservice.php
Where 10.218.28.19 -> System's IP Address.
or
http://localhost/webservice.php to http://10.0.2.2/webservice.php
You just have to use your local IP address:using the cmd command "ipconfig" and your server port number like this:
String webServiceUrl = "http://192.168.X.X:your_local_server_port/your_web_service_name.php"
And make sure you did set the internet permission in your project manifest
It's working perfectly for me
Good Luck
its working for me. I use genymotion for Run App.
1.Firstly i was checked my local ip. goto command mode>> And write ipconfig.
Example: Windows 10>> search cmd>>then Write ipconfig .
2. Then get your local ip information >>>
3.Use give your localhost ip and virtual box ip. You need to use virtual box ip for genymotion.Check below screenshot. You can you below any ip under virtualbox host network
Replacing localhost with 10.0.2.2 is correct, but you can alsor replace localhost with your physical machine's ip(it is better for debug purposes).
Ofc, if ip is provided by dhcp you would have to change it each time...
Good luck!
Solution is very simple.
1 Add Internet permission in Androidmanifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
[2] Change your httpd.config file
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
TO
Order Deny,Allow
Allow from all
Allow from 127.0.0.1
And restart your server.
[3] And most impotent step.
MAKE YOUR NETWORK AS YOUR HOME NETWORK
Go to Control Panel > Network and Internet > Network and Sharing Center
Click on your Network and select HOME NETWORK

Can't use ServerSocket on Android

I'm trying to listen on a port using ServerSocket on an Android device. I want to be able to connect to this port over WiFi using a computer on the same network.
I get no exception when binding it to a port, however when I check netstat it says:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 (null):4040 (null):* LISTEN
I've tried countless ways of binding it to localhost, 0.0.0.0, the WiFi LAN IP address of the device with SocketInetAddress and InetAddress.getByName. Nothing seems to work.
When I try to connect to the port from a computer in the same WiFi (I've tried both netcat and Java's Socket.connect()), all I can see in Wireshark is an ARP request:
Who has [phone's LAN address]? Tell [computer LAN address].
This request repeat itself until timed out.
I've tried the reverse way, by setting the ServerSocket on the computer and connecting to that port from the phone, that works very well.
My testing phone is an Samsung Spica i5700 with a custom ROM.
Any ideas?
Edit:
The code is simple as this:
ServerSocket server = new ServerSocket();
server.setReuseAddr(true);
server.setTimeout(0);
server.bind(new InetSocketAddress(4040));
Socket client = null;
while((client = server.accept()) == null);
// Connected
enter code here
enter code here
Instead of using server.bind, try initializing the server socket like this:
server = new ServerSocket(4040);
Also, server.accept() will actually block until a connection is made, so you don't need that while loop (see: http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html#accept() )
I struggled with this too and was only able to connect to my Android server by using:
ServerSocket myServerSocket = new ServerSocket();
String hostname = getLocalIpAddress();
myServerSocket.bind(new InetSocketAddress(hostname, myPort));
Where hostname was the local IP, which I got using the getLocalIpAddress() function from this page:
https://github.com/Teaonly/android-eye/blob/master/src/teaonly/droideye/MainActivity.java
I was able to get this working by using
ServerSocket server = new ServerSocket( myTcpPort, 0, addr );
where addr = InetAddress of your phone. Otherwise, it only seems to bind to localhost (127.0.0.1). Also, I'm using port 8080.

Categories

Resources