6 years, 8 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

UDP_communication

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

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

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


    
    char out_buffer[] = {11, 12, 13, 14, 15, 16, 17, 18}; // Does not matter
    lcd.locate(0,15);
    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;
    lcd.locate(40,15);
    n = sock.receiveFrom(nist, &in_buffer, 1); //here it sleeps for ever
    lcd.printf("receive %d",n);//I never get this printf 
		

 

    sock.close();
    
   // 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

1 Answer

6 years, 7 months 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:

https://os.mbed.com/docs/latest/tutorials/migrating.html

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.baud(115200);
    pc.printf("Ethernet socket example... \n");
    net.connect();

    // 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 arm.com
    TCPSocket socket;
    socket.open(&net);
    socket.connect("www.arm.com", 80);

    // Send a simple http request
    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\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
    socket.close();

    // Run a UDP receive/send test
    UDPSocket server;
    server.open(&net);
    server.bind(ECHO_SERVER_PORT);
    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
    net.disconnect();
    pc.printf("Done\n");
}

Accepted Answer

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