How to change port of Dart HTTP request? - flutter

I'm writing a Flutter application which involves getting some data from an HTTP server. Here's my code:
// the local IP of my testing server
final String SERVER = 'http://192.168.1.13:5000';
class Database {
Future<String> _getJob(int jobID) async {
var response = http.get('$SERVER?jobID=$jobID');
return response.body;
}
}
Currently this is the output:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception:
SocketException: OS Error: Connection refused, errno = 111, address =
192.168.1.13, port = 41966
Obviously this is happening because there isn't anything on port 41966, but the request shouldn't be going to that port, it should be going to port 5000. Is there a different way of specifying the port?
(my async code probably isn't very good, i'm going to work on it once i've figured this out)

Please specify which platform (Web, iOS, Android, Windows, Linux, mac, Flutter is amazing 😅) you're using. Its probably because Android/ iOS do not allow HTTP connections as they can be read by others on the same network. You need to use HTTPS.
I think for debugging, its better if you just called an API that already exists. Pick one from here: https://apilist.fun/ Then in the future, you can figure out how to get a TLS/SSL certificate assigned to your server (e.g. Using LetsEncrypt)
Alternatively you can go into the configuration of your application (platform specific config) to allow insecure connections (HTTP). For example, on Android and on iOS. However, Android and iOS default to this to make sure you consider security, not just send your data in plain text.
Note: its not actually because the server doesn't exist or the port is wrong. If the server didn't exist, it would say something like connection timed out or server unreachable. In your case, your OS/ platform immediately tells you OS Error: Connection refused, because it can see you're using HTTP.

I had the same issue.
Try using ngrok or a similar proxy. I think the dart HTTP client picks a random port when the host is not resolvable.
If you are pushing to an mobile device for testing your app, there is probably no reason a local IP address will be resolved.

Related

How to bind a Socket to a Network interface

The app must connect to a socket server hosted at 192.168.0.10:35000 by an IoT device through a WiFi network it provides while having an active data connection.
The following code throws an error:
SocketException: Connection timed out, host: 192.168.0.10, port: 35000
this._socket = await Socket.connect('192.168.0.10', 35000);
If I disable the mobile's Data connection, it connects as it should. I think that confirms that the Socket is not bound to the WiFi network.
On a native Android environment, I believe that the ConnectivityManager.bindProcessToNetwork() method, or the Network.getSocketFactory() method, would do the trick.
Is there any way to achieve this without writing native code?
Some extra information:
Running InternetAddress.lookup() on '192.168.0.10' gives the following:
Running NetworkInterfaces.list() gives the following:

I/flutter (30720): SocketException: OS Error: Connection timed out, errno = 110, address = 192.19.170.13, port = 47763

I'm using a real device (not an android emulator)
backend: MongoDb Atlas,
API: strapi-beta, getting this error:
I/flutter (30720): SocketException: OS Error: Connection timed out, errno = 110, address = 192.19.170.13, port = 47763
(found a lot of similar questions but none of them were using a real device for testing)
try {
var ip = '192.19.170.13';
setState(() => _isSubmitting = true);
http.Response response =
await http.post('http://$ip:1337/auth/local/register',
/* I've also tried using localhost(it didn't work) everyone suggested to use my loopback adapter's (Ethernet's ip address) even that isn't working.*/
body: {
'username': _username,
'email': _email,
'password': _password,
});
final responseData = json.decode(response.body);
setState(() => _isSubmitting = false);
_successSnackBar();
print(responseData);
} catch (e) {
setState(() => _isSubmitting = false);
print(e.toString());
}
}
check your device network connection(emulator or real device) and machine network connection(computer) are same.
mobile data turn off
user wifi data which are used on your machine.
problem solved.
If you're working on Chrome, you need to go to
chrome://inspect/#devices
and add your port to 'Port Forwarding' like this
And make sure you have USB Debugging on your device.
Well, if you are doing the tests with a local server and you are on windows. The error is probably due to the windows firewall. You could disable the firewall and it should work or add a rule to the firewall to allow tcp access to the mentioned port.
I had also the same issue with my an Android Device. i.e. IP Address Link works fine in Localhost but not works on physical devices.
So I found that for use yours IP Address link on Physical Devices (Android Devices) you must need to Host your website(mostly WebAPI's) on WAN Network to access yours IP Address links.
So answer is Host your website (mostly WebAPI's) on WAN Network.
I had the same issue with the emulator and the reason for my problem was the IP.
Can you check if you have connectivity to that IP in the device, without using the flutter application?
My problem was trying to connect to the host localhost using 127.0.0.1, and found this page:
https://developer.android.com/studio/run/emulator-networking
Method 1:
install apache2
install curl
access www and fixed with www.IP ADDRESS
Method 2:
config your router NAT setting to your local IP address.
Now you can use your Internet IP Address.
I had this problem many times too. what i did was to use the hotspot on my android mobile i use for debugging to connect to the pc, and voila it works.
Well, what i did to fix this error was to turn windows firewall off, then I opened the needed ports in my router, and it worked!. Also you can set the inbound rules in windows firewall so that you don't need to turn it off
I had this issue occur when hosting both my Flask server and Mongo database locally, by moving the database to a separate device and hosting it there.
It should also be expected that if both are hosted on the same IP you would run into the same issue.
turn your connection on pc to private not public and restart server then pick your pc ip by ipconfig command in cmd and use it as base url
So i was using mobile data and gave the ip of mobile data network when i switched back to my wifi i was getting this error what worked for me was i went to cmd typed ipconfig and copied my ip4 address and replaced it in the api url in the program

Connect to C++ REST sdk SERVER on Windows from LAN

I have a server written on C++ REST SDK.
There's http_listener which listens to "http://localhost:34568".
When I try to send a request in browser or from the client to localhost it works fine and I get the responses from my server. But the point is to use the application in network. And here where the problem comes.
When I try to request the server from the other PC using IP(192.168.1.103:34568) I get "HTTP Error 400. The request hostname is invalid."
I'm aware that that could be some firewall issues but it's turned off. Also I tried to set port rules in brandmauer and it didn't help.
And even more! I got XAMPP running Apache server and when I do the same thing but with (192.168.1.103:80) I do get the response from Apache and have an access.
Anybody had something similar or somebody knows what the problem is about?
Listen to local ip address or to your network name (dns):
"http://xxx.xxx.xxx.xxx:34568" or
"http://your_network_name:34568"
So, if you have multiple network adapters, you can choose which one.

getting SocketTimeoutException while using smack 4.1.2 in android to connect to my ejabberd server

I am trying to connect to ejabberd server using smack API 4.1.2 (no asmack) on android device. while running smackClient program , I am getting below error
java.net.SocketTimeOutException:Failed to connect to abc.example.com/182.*.*.* (on port 5222) after 30000ms,'abc.example.com:5222' failed because java.net.ConnectionException: Failed to connect to abc.example.com/182.*.*.* (on port 5222) after 30000ms
Connection to same ejabberd server using same android device is working fine using xmpp clients like xabber. so Issue is surely with client code I have written. Below is the snippet of my code
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword(userName, password)
.setServiceName("abc.example.com")
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setSendPresence(true)
.build();
connection = new XMPPTCPConnection(config);
connection.connect();
connection.login(userName, password);
I am missing something in my client code that xabber is having so xabber connection is working from same device using same credential.
Please help
Hard to tell without real IP and names in your example. However, my best guess would be about how the address to your IP server is resolved.
There seems to be discrepancy in your example with server (example.com) and service name in your code (abc.example.com).
My guess it that your client is attempting to connect to another machine that the one the XMPP server is running on.
So, here are the things to check when you have issues with a server not replying:
Check how the address of the domain is resolved. You may need to specify another machine name that the domain. If this is a test domain, there is possibly not a DNS setup, so you may even need to specify server IP (while still configuring the client to use an XMPP domain, that's two different things).
In client, log the IP you are trying to connect to, to make sure this is the one where the server is running.
If server is not on the main domain server, you may even need to do DNS SRV record queries for XMPP C2S service.
For me , it took hours to find the solution.
I forget to turnoff the VPN application(Express VPN) .Network tunneling was the root cause .
And change the Network protocol version properties as below ,
Choose the 1st option (Obtain DNS server address automatically).
My experience: I used following code
DomainBareJid xmppServiceDomain = JidCreate.domainBareFrom("desktop-urvfr83");
//DomainBareJid xmppServiceDomain = JidCreate.domainBareFrom("192.168.1.3");
InetAddress addr = InetAddress.getByName("192.168.1.3");
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("alir", "111111")
.setHostAddress(addr)
.setResource("phonn")
.setXmppDomain(xmppServiceDomain)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setPort(5222)
.build();
but it could not connect and produced timeout connection exception. When I disabled windows firewall it worked correctly and connected.

Bluetooth: how to create a socket for connecting to a service with specific UUID with Bluez?

I am working with the BlueZ libraries for managing the bluetooth stack under linux. I am trying to open a socket that should connect with a specific service whose UUID is known. I have successfully tried to open sockets between a server and a client following the examples here:
http://people.csail.mit.edu/albert/bluez-intro/c404.html
where it is also described how to explore services in a remote host; however, what I cannot figure out is how to specify the UUID while connecting the socket. This is something quite straightforward on other contexts, as it happens when using Android libraries; however, with BlueZ I haven't found examples on the web.
Thanks for the help!
Stefano
-- added some details in a reply... don't know if it's right or not; if not, sorry
thanks for your help!
In my post I said I wanted to connect a socket with a specified UUID since I had in mind a function like createRfcommSocketToServiceRecord as can be found in:
http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createRfcommSocketToServiceRecord%28java.util.UUID%29
which returns a socket given a certain UUID. I am developing two softwares, one being the client and the other the server, therefore it's a service I am creating, not a standard one. I publish such service on the server with a specified UUID, and I would like to connect to it from the client. The server is running fine, since with an android platform, exploiting the mentioned createRfcommSocketToServiceRecord method, I am able to connect to the server with the right service.
The example you posted is very clear as well, but unfortunately it is in Python, while (I forgot to point out) I am using Bluez as a C library. I am able to exploit the SDP and inspect services on a remote server; however, when it is the time of setting up the socket, I don't see how to specify the port. I thought about the rc_channel, but as far as I understood it is the bluetooth channel (ranging from 1 to 11 or something similar). Could you please point out Where I should be able to specify the port while creating the socket in the client? And where am I able to find the right port in the complex structure the sdp is dealing with? -- referred to:
http://people.csail.mit.edu/albert/bluez-intro/x604.html
Thanks again for the help!
You don't connect sockets by UUID. You use a socket to connect to remote device and browse its SDP to discover the UUIDs it offers, and map that to a socket port. Here's an example of this process using PyBluez, python wrappers above BlueZ
http://people.csail.mit.edu/albert/bluez-intro/x290.html