| iMatix home page | << | < | > | >> |
SFL Version 2.11 |
#include "sflsock.h" int read_UDP ( sock_t handle, /* Socket handle */ void *buffer, /* Buffer to receive data */ size_t length, /* Maximum amount of data to read */ const struct sockaddr_in *sin /* Block for address, or null */ )
Reads data from a connected or unconnected UDP socket. To prepare a connected UDP socket you call connect UDP (). This makes a connection to a specific port on a specific host, and returns a socket handle. When you call this function with a null value for the address argument, it assumes you are using a connected UDP socket. To prepare an unconnected UDP socket, call create socket () with the string "udp" as argument. This returns a sock_t handle that you can use in this function. If you use an unconnected UDP socket you must provide an address structure. The function places the remote host and port in this structure. This lets you reply using write UDP (). Generally a server can use unconnected sockets, and a client can use connected sockets. You can also format an address for a specific host and port using the address end point () function. If there is an error on the read this function returns SOCKET_ERROR. You can handle errors (in sockerrno) as fatal except for EAGAIN which indicates that the operation would cause a non-blocking socket to block. Treat EWOULDBLOCK as EAGAIN.
{ #if (defined (DOES_SOCKETS)) argsize_t sin_length; /* Length of address */ int flags = 0, /* Flags for call */ rc; /* Return code from call */ ASSERT (buffer); ASSERT (sin); sin_length = (int) sizeof (*sin); if (sin) /* Read from unconnected UDP socket; we accept the address of the */ /* sending party in the sin argument. */ rc = recvfrom ((SOCKET) handle, buffer, length, flags, (struct sockaddr *) sin, &sin_length); else /* Read from a connected UDP socket; we don't need to get the */ /* address, since we already know it. */ rc = recv ((SOCKET) handle, buffer, length, flags); # if (defined (__WINDOWS__)) return (win_error (rc)); # else return (rc); # endif #else return ((int) SOCKET_ERROR); /* Sockets not supported */ #endif }
| << | < | > | >> | Copyright © 1996-2000 iMatix Corporation |