|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sflsock.h"
sock_t
create_socket (
const char *protocol /* Protocol "tcp" or "udp" */
)
Creates a TCP or UDP socket. The socket is not connected. To use with TCP services you must bind or connect the socket. You can use the socket with UDP services - e.g. read UDP () - immediately. Returns a socket number or INVALID_SOCKET, in which case you can get the reason for the error by calling connect error (). This may be one of:
| IP NOSOCKETS | Sockets not supported on this system |
| IP BADPROTOCOL | Cannot understand protocol name |
| IP SOCKETERROR | Cannot create the socket |
{
#if (defined (DOES_SOCKETS))
struct protoent
*ppe; /* Protocol information entry */
int
# if (!defined (__WINDOWS__))
true_value = 1, /* Boolean value for setsockopt() */
# endif
sock_type; /* Type of socket we want */
sock_t
handle; /* Socket from socket() call */
ASSERT (protocol && *protocol);
connect_error_value = IP_NOERROR; /* Assume no errors */
/* Map protocol name to protocol number */
ppe = getprotobyname (protocol);
if (ppe == NULL) /* Cannot get protocol entry */
{
connect_error_value = IP_BADPROTOCOL;
return (INVALID_SOCKET);
}
/* Use protocol string to choose a socket type */
if (streq (protocol, "udp"))
sock_type = SOCK_DGRAM;
else
sock_type = SOCK_STREAM;
/* Allocate a socket */
handle = (sock_t) socket (AF_INET, sock_type, ppe-> p_proto);
if (handle == INVALID_SOCKET) /* Cannot create passive socket */
{
connect_error_value = IP_SOCKETERROR;
return (INVALID_SOCKET);
}
# if (!defined (__WINDOWS__))
/* On BSD-socket systems we need to do this to allow the server to
* restart on a previously-used socket, without an annoying timeout
* of several minutes. With winsock the reuseaddr option lets the
* server work with an already-used socket (!), so we don't do it.
*/
setsockopt ((SOCKET) handle, SOL_SOCKET, SO_REUSEADDR,
(char *) &true_value, sizeof (true_value));
# endif
prepare_socket (handle); /* Ready socket for use */
ip_sockets++;
return (handle);
#elif (defined (FAKE_SOCKETS))
return (1); /* Return dummy handle */
#else
connect_error_value = IP_NOSOCKETS;
return (INVALID_SOCKET); /* Sockets not supported */
#endif
}
| | << | < | > | >> |
|