UDP opening socket failed

edited January 2014 in Library Questions

Hi all,

I'm trying to get this test connection to work: // import UDP library import hypermedia.net.*;

UDP udp;  // the UDP object
byte[] command = new byte[3]; 

void setup(){
  command[0] = 69; // turn on zone 1
  command[1] = 100; // at hue 100
  command[2] = 85; // always end with 85
  udp = new UDP( this, 50000, "192.168.1.100");
  udp.send( command );
}

But I get the following errors:

opening socket failed!

address:192.168.1.100, port:50000 [group:null] Cannot assign requested address: Cannot bind

Can someone tell me where to look to solve this issue? Thanks very much in advance, best, danielle

Tagged:

Answers

  • Answer ✓

    hi danielle, usually for udp you dont need to specify an ip for creating the socket. try UDP(this, 50000); for creating the socket, and then send messages with udp.send(command, ip, port);.

    Whats the other part of the connection? are you connecting two programms on the same machine? if one is already on port 50000 it might be blocked for the other.

    i hope that helps, i've never worked with this library and used java udp directly.

  • Hi, Thank you very much for your response. I've tried out your suggestion but still no luck. This is the adapted program:

    UDP udp;  // the UDP object
    byte[] command = {0x45, 0x0, 0x55};
    int port = 50000;
    String ip = "192.168.1.100";
    
    void setup(){
      udp = new UDP(this, port);
      udp.listen( true );
      udp.send(command, ip, port);
      println("port: "+udp.port());
      //println(udp.isBroadcast ());
    }
    
    void keyPressed() {
      int port = 50000;
      String ip = "192.168.1.100";
      command[0] = 0x40;
      command[1] = 0x12;
      command[2] = 0x55;
      println("closed?: "+udp.isClosed ( ));
      udp.send(command, ip, port);
      println("port: "+udp.port());
    }
    

    I've also tried with another port for the localhost (new UDP(this, port);). I don't get any errors, the port is opened but I also get no result. The program hangs at println(udp.isBroadcast ()); The program is for connecting to a wifi LED lamp (http://www.applamp.nl/service/applamp-api/). This program runs on a PC and it tries to connect to the wifi box of the lamps which has the port and IP address as stated above.

    I must say I don't have so much faith in this library. I've tried working with it before but no luck either. So if you could share a snipet of the UPD Java code you use I'd be very grateful.

    Thanks in advance for your help, best, danielle.

  • Answer ✓

    Hi danielle, i will write a little code snippet for udp communication tonight, and post it here.

  • Super! TIA, danielle

  • edited January 2014 Answer ✓

    Hi danielle, i packaged my helper classes into a library, which you can get here from github. just unzip and put into your library folder. feel free to browse the source here. The interesting parts are in the startListening() method, that sets up the socket and channel etc.

    here a adapted version of the example:

    import com.bckmnn.udp.*;
    import java.net.InetSocketAddress;
    import java.net.InetAddress;
    import java.net.SocketAddress;
    
    /**
     * PRESS a for broadcast message to all
     * PRESS l for message to local
     * PRESS t for message to lamp
     **/
    
    UDPHelper udp;
    SocketAddress local, all, test, testlocal;
    
    void setup() {
      udp = new UDPHelper(this);
      // not sure if you need bidirectional communication. if the lamp expects your programm to listen on port 50000 you can change it here, if not you can leave it like it is.
      udp.setLocalPort(13370);
      udp.startListening();
    
      try {
        // create a new SocketAddress with new InetSocketAddress(InetAddress.getByName("ip number"), target port number);
        local = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 13370);
        all = new InetSocketAddress(InetAddress.getByName("255.255.255.255"), 13370);
        test = new InetSocketAddress(InetAddress.getByName("192.168.1.100"), 50000);
        //testlocal = new InetSocketAddress(InetAddress.getByName("192.168.178.28"), 50000);
      } 
      catch(Exception e) {
        e.printStackTrace();
      }
    }
    
    void draw() {
    }
    
    public void onUdpMessageRecieved(SocketAddress client, byte[] message) {
      String messageString = UDPHelper.stringFromBytes(message);
      println(client + " sent you this message: " + messageString);
    }
    
    void keyPressed() {
      if (key == 'a') {
        udp.sendMessage(UDPHelper.bytesFromString("message to all"), all);
      }
      else if (key == 'l') {
        udp.sendMessage(UDPHelper.bytesFromString("message to self"), local);
      }
      else if (key == 't') {
        udp.sendMessage(new byte[] {0x45, 0x0, 0x55},test);
        //udp.sendMessage(new byte[] {0x45, 0x0, 0x55}, testlocal);
      }
    }
    

    you can check on your local machine or on a remote machine if the sendMessage worked by listening with netcat on the target port. just open a terminal and type nc -lu 50000 to listen for udp on port 50000. I hope that code works for you. if not just let me know.

  • Thank you so very much! Great stuff. I've tested your code. Everything works except the sending of the code to the lamp. Or maybe it works but the lamp doesn't react :( I've tried different things: - working with the testlocal socket (commented it out) - changing the wireless network (it seemed unstable) - changing the command send (still nothing happens)

    I've ran out of idea's for now. Do you have any idea where I can start to solve this mystery? Could it have to do with my localhost? Is it the byte array? Maybe that isn't well formed, although I don't get any errors. Is there a way I can test if the bytes are at least send? The program is just one direction by the way. Just the Processing script sending the bytes to the socket of the lamp.

    If I look at the original code (copied below for you convenience it seems pretty straightforward. But maybe you can see what I'm missing. Thanks ever so much for all your trouble. I hope we can work it out. Best, danielle.

    Original JS code: Usage in Node JS: //load this wifi box class var WifiBoxModule = require('wifibox.js'); var byteCommand = require('commands.js'); //create instance with wifi box ip and port var box = new WifiBoxModule("192.168.1.100", 50000); //send a command ( see commands.js ) box.command(byteCommand.bulb.color.hue(180)); box.command(byteCommand.bulb.white.allOn()); **/

    var http = require('http');
    var dgram = require('dgram');
    
    var WifiBox = function (ip, port) {
    this.client = dgram.createSocket('udp4');
    const default_ip = '192.168.1.100';
    const default_port = 50000;
    this.ip = (ip != undefined && ip.length > 6) ? ip : default_ip;
    this.port = (port != undefined && port > 0) ? port : default_port;
    
    };
    
    
    WifiBox.prototype.command = function (threeByteArray) {
    var buffer = new Buffer(threeByteArray);
    this.client.send(buffer
    , 0
    , buffer.length
    , this.port
    , this.ip
    , function (err, bytes) {
    if (err) {
    console.log("udp error:" + err);
    throw err;
    } else {
    console.log('bytes send: ' + [threeByteArray[0], threeByteArray[1], threeByteArray[2]])
    }
    }
    );
    }
    
    WifiBox.prototype.toString = function () {
    return 'WifiBox: { ip:' + this.ip + ':' + this.port + '}';
    };
    
    
    module.exports = WifiBox;
    
  • OK, I see that you already gave instructions on how to monitor the trafic. I'm trying to get it to work, I'm on windows, it isn't installed yet. I'll be back.

  • I've managed to install and try it out. I've typed: nc -luv 50000 Reply: 50000: inverse host lookup failed: h_errno 11004: NO_DATA

    I've tried pressing the t all the time.

    Hope this means anything to you? Thanks, d

  • edited January 2014 Answer ✓

    hm ... it might be that you need the p parameter with your version of netcat to specify the 50000 port. i put my machines local network ip in the testlocal address so i could check with netcat on the same machine if the commands are sent, you would have to put your ip address in there.

    try nc -luvp 50000

    here are some other thoughts that might be the issue. i am guessing here :)

    whats the output of the processing sketch in the beginning? your machine and the lamp are in the same network segment, like pc's ip=192.168.1.1 lamp's ip=192.168.1.2 ?

    you could try to send the command bytes to the broadcast address (255.255.255.255), every machine in your network listening on your specified port would/should get the message.

    can you write a sketch listening on the needed port, run it on another computer, to see if the connection over the network works?

    is there a firewall involved? windows built in maybe?

    does it work with nodejs?

    have you tried different commands? {0x22, 0x00, 0x55} looks like the on command for white bulbs?

  • edited January 2014

    Hi,

    I've tried nc -lup 50000 there's no error now but also nothing happens, no output.

    I've tried switching of the firewall, still no result.

    I already tried different codes. It is the colour RGBW so it is no use trying to use the code for the white bulbs.

    When I use 255.etc I do get some output in the netcat terminal: B VAdUB U I'm sending the following command:

    udp.sendMessage(new byte[] {0x42, 0x0, 0x55},test);
        myDelay(5000);
        udp.sendMessage(new byte[] {0x41, 0x64, 0x55},test);
        myDelay(5000);
        udp.sendMessage(new byte[] {0x42, 0x0, 0x55},test);
    

    I don't know if that makes sense :)

    This is the output of your helper:

    [UDPHelper] list of network interfaces:
        lo Software Loopback Interface 1
            /0:0:0:0:0:0:0:1
            /127.0.0.1
        net0 WAN Miniport (SSTP)
        net1 WAN Miniport (L2TP)
        net2 WAN Miniport (PPTP)
        ppp0 WAN Miniport (PPPOE)
        eth0 WAN Miniport (IPv6)
        eth1 WAN Miniport (Network Monitor)
        eth2 WAN Miniport (IP)
        ppp1 RAS Async Adapter
        net3 WAN Miniport (IKEv2)
        net4 Atheros AR9485WB-EG Wireless Network Adapter
            /fe80:0:0:0:58cd:5b75:f4b3:a601%11
            /10.10.48.53
        net5 Bluetooth Device (RFCOMM Protocol TDI)
        eth3 Bluetooth Device (Personal Area Network)
            /fe80:0:0:0:dd8f:581:2e:e8c1%13
        eth4 ASIX AX88772B USB2.0 to Fast Ethernet Adapter
        net6 Microsoft Virtual WiFi Miniport Adapter
            /fe80:0:0:0:b490:c6dc:90e8:924f%15
        net7 Microsoft 6to4 Adapter
        eth5 HTC Remote NDIS based Device
        net8 Microsoft ISATAP Adapter #3
        net9 Microsoft ISATAP Adapter
            /fe80:0:0:0:0:5efe:a0a:3035%19
        net10 Atheros AR9485WB-EG Wireless Network Adapter-Virtual WiFi Filter Driver-0000
        net11 Atheros AR9485WB-EG Wireless Network Adapter-QoS Packet Scheduler-0000
        eth6 WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000
        eth7 WAN Miniport (IP)-QoS Packet Scheduler-0000
        eth8 WAN Miniport (IPv6)-QoS Packet Scheduler-0000
        net12 Atheros AR9485WB-EG Wireless Network Adapter-Native WiFi Filter Driver-0000
        net13 Atheros AR9485WB-EG Wireless Network Adapter-WFP LightWeight Filter-0000
        net14 Microsoft Virtual WiFi Miniport Adapter-Native WiFi Filter Driver-0000
        net15 Microsoft Virtual WiFi Miniport Adapter-QoS Packet Scheduler-0000
        net16 Microsoft Virtual WiFi Miniport Adapter-WFP LightWeight Filter-0000
    

    I don't really know anything about networking but am I right when I say that the lamp wifi box net6 Microsoft Virtual WiFi Miniport Adapter
    /fe80:0:0:0:b490:c6dc:90e8:924f%15

    (I think that is it.) Doesn't have an ip address at all? I've discovered some more documentation. This is posted under an other name but apparently it is the same hardware: http://www.limitlessled.com/dev/

    your machine and the lamp are in the same network segment, like pc's ip=192.168.1.1 lamp's ip=192.168.1.2 ?

    I wouldn't know. I don't see these numbers appearing in the helper output... Where can I find them?

    I haven't tried with nodeJS, I don't have it on my pc and don't know anything about it. But I could try if none of this makes sense to you.

    Thanks in advance for your feedback, best, danielle.

Sign In or Register to comment.