lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] UDP socket receive


From: Gisle Vanem
Subject: Re: [lwip-users] UDP socket receive
Date: Wed, 30 Jan 2013 13:19:50 +0100

"Fabian Cenedese" <address@hidden> wrote:

I'm trying to use socket and UDP (DGRAM) to receive data
from other devices.

addr.sin_len=sizeof(addr);
addr.sin_family=AF_INET;
addr.sin_port=PP_HTONS(MyPort);
addr.sin_addr.s_addr=PP_HTONL(INADDR_ANY);
s=socket(AF_INET, SOCK_DGRAM, 0);
ret=connect(s, (struct sockaddr*)&addr, sizeof(addr));

ret=read(s, buffer, size);

The calls are all successful, read is waiting. I can see the udp frame coming
in with the right port. However the frame is discarded in udp_input:

You'll have to add a call to bind(). Something like:

if (bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == -1)
{
  perror ("bind");
  return (-1);
}

From "man bind":
   The bind(2) function assigns an address addr to an unnamed socket s. When
   a socket is created with socket(2) it exists in a name space (address
   family) but has no name assigned. The bind(2) function requests that name
   be assigned to the socket.

When you don't call  bind(), the socket remains "unnamed" (in BSD-lingo).
Hence, in lwIP lingo, the socket has "no recv function registered". Makes sense?

BTW. It's smarter to clear 'addr' before settting up the rest of it:
 memset (&addr, 0, sizeof(addr));

--gv



reply via email to

[Prev in Thread] Current Thread [Next in Thread]