| iMatix home page | << | < | > | >> |
![]() Version 1.91 |
#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 # if (defined (__VMS__)) ppe_struct = { NULL, NULL, 0 }, # endif *ppe; /* Protocol information entry */ int sock_type, /* Type of socket we want */ true_value = 1; /* Boolean value for setsockopt() */ sock_t handle; /* Socket from socket() call */ ASSERT (protocol && *protocol); connect_error_value = IP_NOERROR; /* Assume no errors */ /* On older VAXen, getprotobyname() is not available */ # if (defined (__VMS__)) ppe = &ppe_struct; ppe-> p_proto = 0; # else /* Map protocol name to protocol number */ ppe = getprotobyname (protocol); if (ppe == NULL) /* Cannot get protocol entry */ { connect_error_value = IP_BADPROTOCOL; return (INVALID_SOCKET); } # endif /* 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 */ return (handle); #elif (defined (FAKE_SOCKETS)) return (1); /* Return dummy handle */ #else connect_error_value = IP_NOSOCKETS; return (INVALID_SOCKET); /* Sockets not supported */ #endif }
| << | < | > | >> |
![]() |