Collaboration diagram for UDP Sockets:
![]() |
UDP server and client applications typically use this order of API calls
Assigning a stream to a UDP socket is not supported. Applications must use NutUdpSendTo() and NutUdpReceiveFrom().
For historical reasons, Nut/Net buffers only the last incoming UDP datagram for a specific port by default. Any previously received datagram will be discarded, if it hasn't been passed to the application in the meantime. Most applications will run fine with this. But it will fail for example, if more than one response is expected on a previously broadcasted request. This problem can be solved by calling NutUdpSetSockOpt() to specify a receive buffer size.
#include <sys/socket.h> ... UDPSOCKET *sock; u_short udp_bufsiz = 1024; ... sock = NutUdpCreateSocket(20191); NutUdpSetSockOpt(sock, SO_RCVBUF, &udp_bufsiz, sizeof(udp_bufsiz));
Nut/Net supports connectionless UDP sockets only. A Berkley like bind call is not available.
Data Structures | |
struct | udp_socket |
UDP socket information structure. More... | |
Typedefs | |
typedef udp_socket | UDPSOCKET |
UDP socket type. | |
Functions | |
UDPSOCKET * | NutUdpCreateSocket (u_short port) |
Create a UDP socket. | |
int | NutUdpSendTo (UDPSOCKET *sock, u_long addr, u_short port, void *data, u_short len) |
Send a UDP datagram. | |
int | NutUdpReceiveFrom (UDPSOCKET *sock, u_long *addr, u_short *port, void *data, u_short size, u_long timeout) |
Receive a UDP datagram. | |
int | NutUdpDestroySocket (UDPSOCKET *sock) |
Close UDP socket. | |
UDPSOCKET * | NutUdpFindSocket (u_short port) |
Find a matching socket. | |
int | NutUdpSetSockOpt (UDPSOCKET *sock, int optname, CONST void *optval, int optlen) |
Set value of a UDP socket option. | |
int | NutUdpGetSockOpt (UDPSOCKET *sock, int optname, void *optval, int optlen) |
Get a UDP socket option value. | |
Variables | |
UDPSOCKET * | udpSocketList |
typedef struct udp_socket UDPSOCKET |
Create a UDP socket.
port | Server applications provide the local port number with this parameter. Client applications should pass zero. |
Definition at line 163 of file udpsock.c.
References htons, NutHeapAllocClear(), udp_socket::so_local_port, udp_socket::so_next, and udpSocketList.
Referenced by DiscoveryResponder(), NutDhcpClient(), NutDnsGetResource(), NutSNTPGetTime(), NutWinsNameQuery(), and openlog().
Send a UDP datagram.
sock | Socket descriptor. This pointer must have been retrieved by calling NutUdpCreateSocket(). | |
addr | IP address of the remote host in network byte order. | |
port | Remote port number in host byte order. | |
data | Pointer to a buffer containing the data to send. | |
len | Number of bytes to be sent. |
Definition at line 201 of file udpsock.c.
References memcpy(), _NETBUF::nb_ap, NBAF_APPLICATION, NutNetBufAlloc(), NutNetBufFree(), NutUdpOutput(), and _NBDATA::vp.
Referenced by DiscoveryResponder(), NutDnsGetResource(), NutSNTPGetTime(), NutWinsNameQuery(), vsyslog(), and vsyslog_P().
int NutUdpReceiveFrom | ( | UDPSOCKET * | sock, | |
u_long * | addr, | |||
u_short * | port, | |||
void * | data, | |||
u_short | size, | |||
u_long | timeout | |||
) |
Receive a UDP datagram.
sock | Socket descriptor. This pointer must have been retrieved by calling NutUdpCreateSocket(). | |
addr | IP address of the remote host in network byte order. | |
port | Remote port number in host byte order. | |
data | Pointer to the buffer that receives the data. | |
size | Size of the buffer that receives the data. | |
timeout | Maximum number of milliseconds to wait. |
Definition at line 235 of file udpsock.c.
References htons, ip::ip_src, memcpy(), _NETBUF::nb_ap, _NETBUF::nb_next, _NETBUF::nb_nw, _NETBUF::nb_tp, NutEventWait(), NutNetBufFree(), udp_socket::so_rx_cnt, udp_socket::so_rx_nb, udp_socket::so_rx_rdy, _NBDATA::sz, udphdr::uh_sport, and _NBDATA::vp.
Referenced by DiscoveryResponder(), NutDnsGetResource(), NutSNTPGetTime(), and NutWinsNameQuery().
int NutUdpDestroySocket | ( | UDPSOCKET * | sock | ) |
Close UDP socket.
The memory occupied by the socket is immediately released after calling this function. The application must not use the socket after this call.
sock | Socket descriptor. This pointer must have been retrieved by calling NutUdpCreateSocket(). |
Definition at line 280 of file udpsock.c.
References _NETBUF::nb_next, NutHeapFree(), NutNetBufFree(), udp_socket::so_next, udp_socket::so_rx_nb, and udpSocketList.
Referenced by closelog(), NutDhcpClient(), NutDnsGetResource(), and NutSNTPGetTime().
Find a matching socket.
Loop through all sockets and find a matching one.
port | Local port number. |
Definition at line 320 of file udpsock.c.
References udp_socket::so_local_port, udp_socket::so_next, and udpSocketList.
Referenced by NutUdpInput().
int NutUdpSetSockOpt | ( | UDPSOCKET * | sock, | |
int | optname, | |||
CONST void * | optval, | |||
int | optlen | |||
) |
Set value of a UDP socket option.
The following values can be set:
sock | Socket descriptor. This pointer must have been retrieved by calling NutUdpCreateSocket(). | |
optname | Option to set. | |
optval | Pointer to the value. | |
optlen | Length of the value. |
Definition at line 348 of file udpsock.c.
References SO_RCVBUF, and udp_socket::so_rx_bsz.
Referenced by DiscoveryResponder(), NutDhcpClient(), and NutSNTPGetTime().
int NutUdpGetSockOpt | ( | UDPSOCKET * | sock, | |
int | optname, | |||
void * | optval, | |||
int | optlen | |||
) |
Get a UDP socket option value.
The following values can be set:
sock | Socket descriptor. This pointer must have been retrieved by calling NutUdpCreateSocket(). | |
optname | Option to get. | |
optval | Points to a buffer receiving the value. | |
optlen | Length of the value buffer. |
Definition at line 385 of file udpsock.c.
References SO_RCVBUF, and udp_socket::so_rx_bsz.
Global linked list of all UDP sockets.
Definition at line 149 of file udpsock.c.
Referenced by NutDumpSocketList(), NutUdpCreateSocket(), NutUdpDestroySocket(), and NutUdpFindSocket().