11 months ago.

the UDPsocket receivefrom methode of EthernetInterface is not working

hello everybody I wonder if somebody already used the UDPSocket of EthernetInterface library. In fact I am stacked with the socket.receivefrom that doesn't wont to work. I want to send and receive data through UDP to and from a real time windows target simulik Matlab module. I prepared my module that I tested with a small software, it sends and receives data with no problem my code follows this logic:

  • create an EthernetInterface object (eth), initiate it with eth.init() to a specific IP address with a specific Mask and Gate, then connect with eth.connect(). this goes well because both methods returns '0' and I can ping my mbed from my PC;
  • create a UDPSocket 'sock' and bind it to a specific port '1820' using the sock.bin() function, this goes good to because it is displaying a '0' on my LCD screen (I have mbed application board);
  • create an endpoint and set its address to my PC's address and its port to a specific port (1819);
  • here, if I send a buffer using sock.sendto(), it goes very good and I'm receiving data on my matlab module, but if I try to receive data the mbed goes on an infinite sleep, and it doesn't even give an error code when debugging using the keil debugguer, I found that the socket doesn't find any "lastdata" left and it looks like the code looses its way while trying to find an error code. this is my code


#include "mbed.h"
#include "EthernetInterface.h"
#include "C12832.h"
 C12832 lcd(p5, p7, p6, p8, p11);
int main() {

    EthernetInterface eth;
    lcd.printf("Eth %d",eth.init("", "", "")); //here every thing goes well and I have 0 on my 
                                                                                                                                                                                               // lcd
    lcd.printf("Cnx %d",eth.connect()); // we have 0 here too

    UDPSocket sock;
    lcd.printf("bind %d",sock.bind(1820)); //here is ok, we have 0 too
    Endpoint nist;
    nist.set_address("", 1819);
    char in_buffer;

    char out_buffer[] = {11, 12, 13, 14, 15, 16, 17, 18}; // Does not matter
    lcd.printf("send %d",sock.sendTo(nist, out_buffer, sizeof(out_buffer))); //here is ok, I have 8, the number of the sent cararters
                                                                                                                                                                //and I receive the data on matlab
    int n;
    n = sock.receiveFrom(nist, &in_buffer, 1); //here it sleeps for ever
    lcd.printf("receive %d",n);//I never get this printf 


   // eth.disconnect();
    while(1) {}

same result either I use bind() or init(), either I initiate the endpoint address or not, either I use one socket or two, one endpoint or two or whatever when I go to the sockets.c file and I remove the line that goes to look for the error(line 594), the code doesn't sleep but neither receives any data, the sock.receivefrom just returns -1. help please

Comment on this question

1 Answer

10 months, 4 weeks ago.

Hi Khalid,

It appears that you are using Mded OS 2 or Mbed RTOS. These have been replaced by OS 5 - can you possibly upgrade? We have a migration guide here:

We've also created a small OS 5 example for you as the sockets API has changed a bit (see below).

If you can't upgrade to OS 5 - have you tested a different UDP source besides Matlab? What board and OS are you using?

-Ralph, Team Mbed

#include "mbed.h"
#include "EthernetInterface.h"

#define ECHO_SERVER_PORT   7

// Network interface
EthernetInterface net;

Serial pc(USBTX, USBRX);

// Socket demo
int main() {

    // Bring up the ethernet interface
    pc.printf("Ethernet socket example... \n");

    // Show the network address
    const char *ip = net.get_ip_address();
    pc.printf("IP address is: %s\n", ip ? ip : "No IP");

    // Open a socket on the network interface, and create a TCP connection to
    TCPSocket socket;;
    socket.connect("", 80);

    // Send a simple http request
    char sbuffer[] = "GET / HTTP/1.1\r\nHost:\r\n\r\n";
    int scount = socket.send(sbuffer, sizeof sbuffer);
    pc.printf("sent %d [%.*s]\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

    // Recieve a simple http response and print out the response line
    char rbuffer[64];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);
    pc.printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

    // Close the socket to return its memory

    // Run a UDP receive/send test
    UDPSocket server;;
    SocketAddress sockAddr;

    char buffer[256];
    pc.printf("\nWaiting for UDP packet...\n");
    int n = server.recvfrom(&sockAddr, buffer, sizeof(buffer));
    buffer[n] = '\0';

    pc.printf("Received packet from: %s\n", sockAddr.get_ip_address());
    pc.printf("Packet contents : '%s'\n",buffer);
    pc.printf("Sending Packet back to Client\n");
    server.sendto(sockAddr, buffer, n);

    // Bring down the ethernet interface

Yes, actually I was using the Mbed RTOS, the problem is fixed by switching to the mbed-os. ultimately the matlab module was making some trubles but it wasn't the reason why my code wasn't working. your OS-5 exemple works perfectly. still.. when I make the whole communication in a loop (in order to have a ermanant communication) the sendto function blocks after a while with an error code -3003, I fixed this by making a test on the returned value from sendto, and if its <0 I close the socket, I reopen it and I rebind it to the port. this works but I wish I could found a better solution. Thank you

posted by FOUGHALI Khaled 21 May 2018

To post an answer, please log in.