BlueSocket icon indicating copy to clipboard operation
BlueSocket copied to clipboard

Unable to bind to source address UDP

Open duhanebel opened this issue 6 years ago • 7 comments

I can't find the way to bind to a specific interface/IP using the UDP socket. I tried with something similar to:

try socket.listen(on: self.port, maxBacklogSize: Socket.SOCKET_DEFAULT_MAX_BACKLOG, allowPortReuse: false, node: "my IP address")
let (bytes, address) = try socket.readDatagram(into: &data)

But that way I don't receive any data. What am I doing wrong?

duhanebel avatar Feb 07 '19 21:02 duhanebel

What are you passing in the node parameter? The value must be a String containing the numeric representation of the address you’re trying to connect to.

billabt avatar Feb 08 '19 14:02 billabt

I'm passing the ip of the wifi card as a string (in my case "10.1.0.16"). Is that right? It seems like it binds properly (if I pass gibberish I get a unable to bind error) but after that I don't receive anything when I try to send. Wireshark sees the packet coming in though.

duhanebel avatar Feb 08 '19 14:02 duhanebel

Can you give me an example? I’ve tried it here and it seems to be working fine.

billabt avatar Feb 08 '19 14:02 billabt

This is what I use at the moment: the data arrives to the interface but the code never goes past the socket.listen line. If I don't specify the node and use directly: func listen(forMessage data: inout Data, on port: Int, maxBacklogSize: Int then it all works, but only locally if I send messages to 127.0.0.1.

let listenSocket = try Socket.create(family: .inet, type: .datagram, proto: .udp)
var data = Data()
try socket.listen(on: self.port, maxBacklogSize: Socket.SOCKET_DEFAULT_MAX_BACKLOG, allowPortReuse: false, node: "10.1.0.16")
let (bytes, address) = try socket.readDatagram(into: &data)
print("Read \(bytes) from \(String(describing: address))")

duhanebel avatar Feb 08 '19 14:02 duhanebel

I’ll look into it a bit deeper. Hopefully I can reproduce it.

billabt avatar Feb 08 '19 14:02 billabt

Thanks, let me know if I can assist grabbing some extra debug data for you.

duhanebel avatar Feb 08 '19 15:02 duhanebel

After a bit of digging it looks like I get nothing on macOS (Mojave) but I get Error code: -9996(0x-270C), The socket is not connected on Linux (ubuntu 18.04) when trying to reply back with a message on the same socket

duhanebel avatar Feb 08 '19 20:02 duhanebel