send UDP packets from mbed

Hi guys, I am sending UDP packets with mbed, wanting them to be received on a PC that is running a program that opens the udp port 5000 Through wireshark I see that packets are sent but on the server never gets anything.

mbed code

#include "mbed.h"
#include "EthernetNetIf.h"
#include "UDPSocket.h"

EthernetNetIf eth;
UDPSocket udp;

void onUDPSocketEvent(UDPSocketEvent e)
{
  switch(e)
  {
  case UDPSOCKET_READABLE: //The only event for now
    char buf[64] = {0};
    Host host;
    while( int len = udp.recvfrom( buf, 63, &host ) )
    {
      if( len <= 0 )
        break;
      printf("From %d.%d.%d.%d: %s\n", host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3], buf);
    }
    break;
  }
}

int main() {
  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup OK\n");
  
  Host multicast(IpAddr(192,168,1,35),5000,NULL);

  udp.setOnEvent(&onUDPSocketEvent);
  
  udp.bind(multicast);
  
  Timer tmr;
  tmr.start();
;
  while(true)
  {
    Net::poll();
    if(tmr.read() > 5)
    {
      tmr.reset();
       const char* str = "Hello from mbed\0";
    udp.sendto( str, strlen(str), &multicast );
      printf("%s\n\r", str);
    }
  }
}


pc code


import socket, traceback

host = ''
port =5000

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))

while 1:
    try:
        
        message, address = s.recvfrom(64)
        if message=="": break
        print "Got data from", address
        s.sendto(message , address)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        traceback.print_exc()

udp port is listening on my server

udp        0      0 0.0.0.0:5000            0.0.0.0:*                           6488/python

and i can see that the packet is send.

0000  00 22 43 72 b6 51 00 02  f7 f0 84 90 08 00 45 00   ."Cr.Q.. ......E.
0010  00 2b 00 09 00 00 ff 11  38 20 c0 a8 01 25 c0 a8   .+...... 8 ...%..
0020  01 23 13 88 13 88 00 17  74 72 48 65 6c 6c 6f 20   .#...... tr Hello 
0030  66 72 6f 6d 20 6d 62 65  64 00 00 00               from mbe d ...    


Hi guys, I answer to my problem myself. My laptop's firewall was on and was blocking all my connections. For this reason, could not establish a connection. Thank you all.

13 Feb 2012

Thanks for asking tought, helped us for UDP.