eyBuildLib API Reference : eyBuildLib
ebsocket - socket wrapper for different OS platform
ebSockInit( ) - init WinSock DLL for WIN32 application
ebSockCleanup( ) - free WinSock DLL for WIN32 application
socket_create( ) - creates a socket and returns a descriptor
socket_close( ) - close an opened socket descriptor
socket_linger_close( ) - close a socket fast but not perform a "graceful"
socket_listen( ) - enable connections to a socket
socket_sockaddr( ) - initialize the sockaddr strut with IP and port
socket_connect( ) - connect a remote host with a given address/port pair
socket_connect_timeout( ) - connect a remote host with a timeout
socket_create_connect( ) - create a socket and connect a remote host
socket_bind( ) - bind socket into a given address/port pair
socket_accept( ) - accept a new connection
socket_create_listen( ) - create a socket and listen on it
socket_socketpair( ) - create a pair socket connection
socket_send( ) - send data to a socket
socket_sendto( ) - sends data to a socket, whether it is connected or not
socket_recv( ) - receive data from a socket
socket_readn( ) - read at most n bytes from a socket in one times
socket_writen( ) - write n bytes to a socket in one times
socket_recv_timeout( ) - receive data from socket with a timeout
socket_recvfrom( ) - receive a message from a socket
socket_recvfrom_timeout( ) - receive a message with a timeout
socket_getpeername( ) - get the name of a connected peer
socket_getsockname( ) - get local side socket name
socket_select( ) - pend on a set of socket descriptors and determines the status
socket_setsockopt( ) - set socket options
socket_getsockopt( ) - get socket options
socket_set_block( ) - sets blocking mode on a socket descriptor
socket_set_nonblock( ) - sets nonblocking mode on a socket descriptor
socket_errno( ) - get last error number of socket
socket_error( ) - map the last error of socket into a string buffer
socket_gethostbyname( ) - get the IP address and the address family
socket_gethostbynamel( ) - get the list of IP addresses and the address family
socket wrapper for WIN32, linux, *nix, and VxWorks.
If your system support IPv6, you can define the macro HAVE_IPV6_SUPPORT to support IPv6 feature. If you are in POSIX like environment, and support Unix socket, you can use a unix path as the socket address.
If the address is separate by ":", it will be looked as an IPv6 address family; If the address is separate by ".", it will be looked as an IPv4 address family; If the address is prefixed with "/" or ".", it will be looked as an Unix address family, and crate a UNIX socket at first.
ebsocket.h
ebSockInit( ) - init WinSock DLL for WIN32 application
int ebSockInit (void)
This routine initialize the WinSock DLL for WIN32 application. In non-Win32 system, it will do nothing now.
OK/ERROR
ebSockCleanup( ) - free WinSock DLL for WIN32 application
void ebSockCleanup (void)
This routine free WinSock DLL for WIN32 application. In non-Win32 system, it will do nothing now.
OK/ERROR
socket_create( ) - creates a socket and returns a descriptor
int socket_create ( int domain, /* address family (for example, AF_INET) */ int type, /* SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW */ int protocol /* socket protocol (usually 0) */ )
This routine creates an endpoint for communication and returns a descriptor.
A socket descriptor, or ERROR.
socket_close( ) - close an opened socket descriptor
int socket_close ( int sock /* socket descriptor */ )
This routine is to close an opened socket descriptor.
OK, or ERROR if the descriptor is invalid.
socket_linger_close( ) - close a socket fast but not perform a "graceful"
int socket_linger_close ( int sock, int timeout )
This routine fast close an opened socket descriptor by setting the SO_LINGER option to avaid enter the TIME_WAIT state. The send side should not call this routine, except he/she knows all data has been received by peer side.
OK/ERROR
socket_listen( ) - enable connections to a socket
int socket_listen ( int sock, /* socket descriptor */ int backlog /* number of connections to queue */ )
This routine enables connections to a socket.
OK, or ERROR if the socket is invalid or unable to listen.
socket_sockaddr( ) - initialize the sockaddr strut with IP and port
int socket_sockaddr ( sockaddr * saddr, /* to return the result */ const char * address, /* IPv4/IPv6 address */ short port /* port */ )
This routine is to initialize the sockaddr strut with IP and port, and automatically fill the address family by the address type (IPv4/IPv6). If address family is PF_UNIX and has defined OS_POSIX, the parameter port will be ignore.
the size of address struct, or ERROR if an error occurred.
socket_connect( ) - connect a remote host with a given address/port pair
int socket_connect ( int sock, const char * address, short port )
This routine connect a remote host with a given address/port pair.
OK, or ERROR if an error occurred.
socket_connect_timeout( ) - connect a remote host with a timeout
#if 1 int socket_connect_timeout ( int sock, /* socket fd */ const char * address, /* remote address */ short port, /* remote port */ int timeout /* microsecond */ )
This routine connect a remote host with a given address/port pair and a timeout (microsecond). If timeout is zero, it means connect remote host with the system default timeout.
0 if OK, or >0 if timeout, or <0 if an error occurred.
socket_create_connect( ) - create a socket and connect a remote host
int socket_create_connect ( const char * address, /* remote address */ short port, /* remote port */ int type /* TCP: SOCK_STREAM, UDP: SOCK_DGRAM */ )
This routine create a socket and connect a remote host. The parameter type maybe one of SOCK_DGRAM, SOCK_STREAM.
A socket descriptor, or ERROR if an error occurred.
socket_bind( ) - bind socket into a given address/port pair
int socket_bind ( int sock, const char * address, short port )
This routine bind socket into a given address/port pair. Note: Before bind it, this routine will try set SO_REUSEADDR option for this socket at first.
OK, or ERROR if an error occurred.
socket_accept( ) - accept a new connection
int socket_accept ( int sock )
This routine accept a new connection.
A socket descriptor, or ERROR if an error occurred.
socket_create_listen( ) - create a socket and listen on it
int socket_create_listen ( const char * address, /* address to bind to */ short port, /* port to bind to */ int backlog /* max length of listen queue */ )
This routine create a socket and listen on the parameter of address. If address equal to NULL or an empty string "", it will listen on the address of "0.0.0.0". The port parameter is to specify the listen port, backlog parameter is to specify the max queue length of new connection.
A socket descriptor, or ERROR if an error occurred.
socket_socketpair( ) - create a pair socket connection
int socket_socketpair ( int domain, /* address family, AF_UNIX, ... */ int type, /* SOCK_STREAM or SOCK_DGRAM */ int protocol, /* socket protocol (usually 0) */ int fds[2] /* to return a pair connection */ )
This routine create a pair socket connection. The parameters domain may be one of AF_UNIX, AF_INET, AF_INET6; the parameters type may be SOCK_STREAM or SOCK_DGRAM, default the protocol is zero. If create success, this routine will fill the pair of description to fds. The fds[0] is the server side, fds[1] is client side.
This implementation don't support write fds[1] and read fds[0], if you choosed the type is SOCK_DGRAM. In all system, we realized the AF_UNIX as AF_INET.
OK/ERROR
socket_send( ) - send data to a socket
int socket_send ( int sock, /* socket to receive data from */ const void * buf, /* buffer to send */ size_t len, /* length of buffer */ int flags /* flags to underlying protocols */ )
This routine send data to a socket.
the number of bytes sent, or ERROR if an error occurred.
socket_sendto( ) - sends data to a socket, whether it is connected or not
int socket_sendto ( int sock, const void * pbuf, /* data to send */ int len, /* length of data */ const char * address, /* peer address */ short port /* peer port */ )
This routine sends data to a socket, whether it is connected or not.
the number of bytes sent, or ERROR if an error occurred.
socket_recv( ) - receive data from a socket
int socket_recv ( int sock, /* socket to receive data from */ void * pbuf, /* buffer to hold data */ size_t buflen, /* length of buffer */ int flags /* flags to underlying protocols */ )
This routine is to receive data from a socket
the number of bytes received, or ERROR if an error occurred.
socket_readn( ) - read at most n bytes from a socket in one times
int socket_readn ( int sock, void * pbuf, size_t maxlen )
This routine is to read at most "maxlen" byte data from a socket in one times. It will not return before completely read "n" bytes, except for read EOF or error occur. If the returns value less than "maxlen", it means This routine has read EOF.
the number of bytes received, or ERROR if an error occurred.
socket_writen( ) - write n bytes to a socket in one times
int socket_writen ( int sock, void * pbuf, size_t dlen )
This routine is to write n bytes to a socket in one times. It will not return before completely write "dlen" bytes, except for error occur.
Don't try to write a noblock descriptor, it will return ERROR if send buffer is full.
the bytes of wrote(always dlen), or ERROR if write error occurred.
socket_recv_timeout( ) - receive data from socket with a timeout
int socket_recv_timeout ( int sock, /* socket */ void * pbuf, /* buffer to hold data */ int len, /* buffer size */ int maxwait /* millisecond of at most wait */ )
This routine is to receive data from socket with a timeout (millisecond). If returns -2, means wait timeout.
the number of bytes received, or ret < 0 if an error occurred.
socket_recvfrom( ) - receive a message from a socket
int socket_recvfrom ( int sock, /* socket */ void * pbuf, /* buffer to hold data */ int len, /* buffer size */ int flags, /* flags to underlying protocols */ char * address, /* to return peer address */ short * port /* to return peer port */ )
This routine receive a message from a socket.
the number of bytes received, or ERROR if an error occurred.
socket_recvfrom_timeout( ) - receive a message with a timeout
int socket_recvfrom_timeout ( int sock, /* socket */ void * pbuf, /* buffer to hold data */ int len, /* buffer size */ char * address, /* to return peer address */ short * port, /* to return peer port */ int maxwait /* millisecond of at most wait */ )
This routine is to receive a message from specify address with a timeout. If returns -2, means wait timeout.
the number of bytes received, or ret < 0 if an error occurred.
socket_getpeername( ) - get the name of a connected peer
int socket_getpeername ( int sock, char * address, short * port )
This routine is to get the name of a connected peer, and convert the IP into a string, convert the port into host order.
OK/ERROR
socket_getsockname( ) - get local side socket name
int socket_getsockname ( int sock, char * address, short * port )
This routine is to get local side socket name, and convert the IP into a string, convert the port into host order.
OK/ERROR
socket_select( ) - pend on a set of socket descriptors and determines the status
int socket_select ( fd_set * readfds, /* read fds */ fd_set * writefds, /* write fds */ fd_set * exceptfds, /* exception fds */ int tv_sec, /* timeout seconds part */ int tv_usec /* timeout usecond part */ )
This routine pend on a set of socket descriptors and determines the status. Usefull macros includes FD_ISSET(s, *set), FD_SET(s, *set), FD_ZERO(*set) and FD_CLR(s, *set).
The number of file descriptors with activity, 0 if timed out, or ERROR if an error occurred.
socket_setsockopt( ) - set socket options
int socket_setsockopt ( int sock, /* socket to receive data from */ int level, /* protocol level of option */ int optname, /* option name */ const void * optval, /* pointer to option value */ size_t optlen /* option length */ )
This routine sets the options associated with a socket.
OK/ERROR
socket_getsockopt( ) - get socket options
int socket_getsockopt ( int sock, /* socket to receive data from */ int level, /* protocol level of option */ int optname, /* option name */ void * optval, /* pointer to option value */ size_t * optlen /* option length */ )
This routine gets the options associated with a socket.
OK/ERROR
socket_set_block( ) - sets blocking mode on a socket descriptor
int socket_set_block ( int sock )
This routine sets blocking mode on a socket descriptor.
OK/ERROR
socket_set_nonblock( ) - sets nonblocking mode on a socket descriptor
int socket_set_nonblock ( int sock )
This routine sets nonblocking mode on a socket descriptor.
OK/ERROR
socket_errno( ) - get last error number of socket
int socket_errno ()
This routine is get last error number of socket. Later, you can all socket_error_string( ) to map the error number into a string.
error number
socket_error( ) - map the last error of socket into a string buffer
char * socket_error ( char errbuf[256] /* the result will copy to */ )
This routine is to map the last error of socket into a string buffer. The map string will be copied into the parameter errbuf.
returned the error message, pointer to the source buffer.
socket_gethostbyname( ) - get the IP address and the address family
char * socket_gethostbyname ( const char * name, /* domain/ip */ char addr[40], /* the result will copy to */ int * addrtype /* to return address family */ )
This routine is to get the IP address and the address family of a given DNS/IP. The address family may be one of AF_INET and AF_INET6.
pointer to the addr, or NULL if an error occur.
socket_gethostbynamel( ) - get the list of IP addresses and the address family
int socket_gethostbynamel ( const char * name, /* domain/ip */ char list[][40], /* the result will copy to */ int maxnum, /* max number of list */ int * addrtype /* to return address family */ )
This routine is to get the list of IP addresses and the address family of a given DNS/IP. The address family may be one of AF_INET and AF_INET6.
the number of address, or ERROR if an error occur.