Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection refused on android #690

Closed
reiterasrls opened this issue Jul 8, 2019 · 20 comments
Closed

connection refused on android #690

reiterasrls opened this issue Jul 8, 2019 · 20 comments
Labels
bug Something isn't working support Support is needed

Comments

@reiterasrls
Copy link

reiterasrls commented Jul 8, 2019

Hi,i'm using mqttnet in xamarin forms project (android and uwp) as a server and xamarin forms android as a client.
Id i run server on uwp app it work well but if i run android server client receive connection refused from server.
can you help me to resolve it
I see that the problem is to start broker on android because return exception (access denied).
It' s possibile to not work broker on Android?

@chkr1011 chkr1011 added the support Support is needed label Jul 8, 2019
@chkr1011
Copy link
Collaborator

chkr1011 commented Jul 8, 2019

Did you gave the app the proper permissions? Maybe opening a server (port) requires additional permissions.

@reiterasrls
Copy link
Author

reiterasrls commented Jul 8, 2019

i gave interner permission and network state and installed mqttnet in android project too.
my code is sample
Task.Factory.StartNew(async () =>
{
var optionsBuilder = new MqttServerOptionsBuilder()
.WithConnectionBacklog(100)
.WithDefaultEndpointPort(1883);
var mqttServer = new MqttFactory().CreateMqttServer();
await mqttServer.StartAsync(optionsBuilder.Build());
});
i have removed await and used continuewith to have exception. Result is Set socket option exception
if i start uwp project work well

@denisgeom
Copy link

I had the same problem on BeagleBoardx15 running Linux Debian
I found out the problem is in the method MqttTcpServerListener::Start( ...)
The call to _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
generate an exception access denied.
It seems it should be SocketOptionLevel.Tcp instead of SocketOptionLevel.Socket
change to: _socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
After modifying this the server works.

@SeppPenner SeppPenner added the bug Something isn't working label Jul 11, 2019
@SeppPenner
Copy link
Collaborator

@denisgeom Okay, this is interesting...

@reiterasrls
Copy link
Author

@denisgeom thanks. one question...it's possibile set your code in library without modify source code?
I don't know the library well

@chkr1011
Copy link
Collaborator

@reiterasrls At the moment you can set NoDelay to false in the MQTT Server options. Then the failing method is not called.

@denisgeom Are you sure that your mentioned solution is correct? I am not sure what the difference in using TCP instead of Socket as the parameter is. All examples I found are using the Socket parameter.

@denisgeom
Copy link

I am guessing, since you open the socket as TCP, you may only change socket option for TCP.

Leaving, SocketOptionLevel.Socket will generate access denied exception.
As soon as I changed it to SocketOptionLevel.Tcp
no more exception and server works.
This only apply to linux, I also ran the code without any change in Windows and running in Windows I did not have this issue. I also targeted .net core 2.2 and change the debug symbol to portable. As of now, they were all set for windows, and could not debug under Linux.
So my guess is the code was never run under Linux, or Android and issue was not noticed since it works fine under Windows.
Anyway, this code should have fail or work on both OS without any change, so something is different in the underlying code. Linux, seems to be more strict on what you can access.

@chkr1011
Copy link
Collaborator

You are right. I found this: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.setsocketoption?view=netframework-4.8

There TCP is used for NoDelay...

@reiterasrls
Copy link
Author

thank you all
the problem remains and can be solved by changing the source code ... right?

@chkr1011
Copy link
Collaborator

Please try the latest beta I released yesterday. It contains a fix.

@denisgeom
Copy link

I did try the latest NuGet package 3.0.6-beta1 and it does not crash anymore when opening the server on Linux and Windows

@reiterasrls
Copy link
Author

I try it and let you know, thanks for all

@reiterasrls
Copy link
Author

reiterasrls commented Jul 18, 2019

i tried beta release. now with the same code and now i have always socket exception "address already in use"

@chkr1011
Copy link
Collaborator

Let me try to build a small Xamarin sample. I never had the use case that a smartphone is the server...

@chkr1011
Copy link
Collaborator

After doing some research I found that the default bound IP address is not supported in Android. So you have to use the actual address of the device.

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];

var server = new MqttFactory().CreateMqttServer();
server.StartAsync(new MqttServerOptionsBuilder()
    .WithDefaultEndpointBoundIPAddress(ipAddress)
    .WithDefaultEndpointBoundIPV6Address(IPAddress.None)
    .Build()).GetAwaiter().GetResult();

But I recommend to do some more research in order to better understanding of the implications.

@reiterasrls
Copy link
Author

reiterasrls commented Jul 28, 2019

thanks...try it and inspect eventually problems

@chkr1011
Copy link
Collaborator

@reiterasrls Please reopen the ticket if you experience problems.

@SeppPenner Do you think the above snipped is a good thing for the Wiki?

@SeppPenner
Copy link
Collaborator

Yeah, that is a good idea. I will add this soon.

@ozersenol
Copy link

@chkr1011 I tried it but it is not working. IP return 127.0.0.1 with this example. I dont know it as it should be? I also tried real device ip and did not work. could you please check my request #1960 ? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working support Support is needed
Projects
None yet
Development

No branches or pull requests

5 participants