eyBuildLib API Reference : eyBuildLib

ebsocket

NAME

ebsocket - socket wrapper for different OS platform

ROUTINES

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

DESCRIPTION

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.

NOTE

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.

INCLUDE

ebsocket.h


eyBuildLib : Routines

ebSockInit( )

NAME

ebSockInit( ) - init WinSock DLL for WIN32 application

SYNOPSIS

int ebSockInit (void)

DESCRIPTION

This routine initialize the WinSock DLL for WIN32 application. In non-Win32 system, it will do nothing now.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

ebSockCleanup( )

NAME

ebSockCleanup( ) - free WinSock DLL for WIN32 application

SYNOPSIS

void ebSockCleanup (void)

DESCRIPTION

This routine free WinSock DLL for WIN32 application. In non-Win32 system, it will do nothing now.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_create( )

NAME

socket_create( ) - creates a socket and returns a descriptor

SYNOPSIS

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) */
    )

DESCRIPTION

This routine creates an endpoint for communication and returns a descriptor.

RETURNS

A socket descriptor, or ERROR.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_close( )

NAME

socket_close( ) - close an opened socket descriptor

SYNOPSIS

int socket_close
    (
    int sock                  /* socket descriptor */
    )

DESCRIPTION

This routine is to close an opened socket descriptor.

RETURNS

OK, or ERROR if the descriptor is invalid.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_linger_close( )

NAME

socket_linger_close( ) - close a socket fast but not perform a "graceful"

SYNOPSIS

int socket_linger_close
    (
    int sock,
    int timeout
    )

DESCRIPTION

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.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_listen( )

NAME

socket_listen( ) - enable connections to a socket

SYNOPSIS

int socket_listen
    (
    int sock,                 /* socket descriptor */
    int backlog               /* number of connections to queue */
    )

DESCRIPTION

This routine enables connections to a socket.

RETURNS

OK, or ERROR if the socket is invalid or unable to listen.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_sockaddr( )

NAME

socket_sockaddr( ) - initialize the sockaddr strut with IP and port

SYNOPSIS

int socket_sockaddr
    (
    sockaddr *   saddr,       /* to return the result */
    const char * address,     /* IPv4/IPv6 address */
    short        port         /* port */
    )

DESCRIPTION

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.

RETURNS

the size of address struct, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_connect( )

NAME

socket_connect( ) - connect a remote host with a given address/port pair

SYNOPSIS

int socket_connect
    (
    int          sock,
    const char * address,
    short        port
    )

DESCRIPTION

This routine connect a remote host with a given address/port pair.

RETURNS

OK, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_connect_timeout( )

NAME

socket_connect_timeout( ) - connect a remote host with a timeout

SYNOPSIS

#if 1 int socket_connect_timeout
    (
    int          sock,        /* socket fd */
    const char * address,     /* remote address */
    short        port,        /* remote port */
    int          timeout      /* microsecond */
    )

DESCRIPTION

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.

RETURNS

0 if OK, or >0 if timeout, or <0 if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_create_connect( )

NAME

socket_create_connect( ) - create a socket and connect a remote host

SYNOPSIS

int socket_create_connect
    (
    const char * address,     /* remote address */
    short        port,        /* remote port */
    int          type         /* TCP: SOCK_STREAM, UDP: SOCK_DGRAM */
    )

DESCRIPTION

This routine create a socket and connect a remote host. The parameter type maybe one of SOCK_DGRAM, SOCK_STREAM.

RETURNS

A socket descriptor, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_bind( )

NAME

socket_bind( ) - bind socket into a given address/port pair

SYNOPSIS

int socket_bind
    (
    int          sock,
    const char * address,
    short        port
    )

DESCRIPTION

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.

RETURNS

OK, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_accept( )

NAME

socket_accept( ) - accept a new connection

SYNOPSIS

int socket_accept
    (
    int sock
    )

DESCRIPTION

This routine accept a new connection.

RETURNS

A socket descriptor, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_create_listen( )

NAME

socket_create_listen( ) - create a socket and listen on it

SYNOPSIS

int socket_create_listen
    (
    const char * address,     /* address to bind to */
    short        port,        /* port to bind to */
    int          backlog      /* max length of listen queue */
    )

DESCRIPTION

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.

RETURNS

A socket descriptor, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_socketpair( )

NAME

socket_socketpair( ) - create a pair socket connection

SYNOPSIS

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 */
    )

DESCRIPTION

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.

LIMITATION

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.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_send( )

NAME

socket_send( ) - send data to a socket

SYNOPSIS

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 */
    )

DESCRIPTION

This routine send data to a socket.

RETURNS

the number of bytes sent, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_sendto( )

NAME

socket_sendto( ) - sends data to a socket, whether it is connected or not

SYNOPSIS

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 */
    )

DESCRIPTION

This routine sends data to a socket, whether it is connected or not.

RETURNS

the number of bytes sent, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_recv( )

NAME

socket_recv( ) - receive data from a socket

SYNOPSIS

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 */
    )

DESCRIPTION

This routine is to receive data from a socket

RETURNS

the number of bytes received, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_readn( )

NAME

socket_readn( ) - read at most n bytes from a socket in one times

SYNOPSIS

int socket_readn
    (
    int    sock,
    void * pbuf,
    size_t maxlen
    )

DESCRIPTION

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.

RETURNS

the number of bytes received, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_writen( )

NAME

socket_writen( ) - write n bytes to a socket in one times

SYNOPSIS

int socket_writen
    (
    int    sock,
    void * pbuf,
    size_t dlen
    )

DESCRIPTION

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.

NOTE

Don't try to write a noblock descriptor, it will return ERROR if send buffer is full.

RETURNS

the bytes of wrote(always dlen), or ERROR if write error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_recv_timeout( )

NAME

socket_recv_timeout( ) - receive data from socket with a timeout

SYNOPSIS

int socket_recv_timeout
    (
    int    sock,              /* socket */
    void * pbuf,              /* buffer to hold data */
    int    len,               /* buffer size */
    int    maxwait            /* millisecond of at most wait */
    )

DESCRIPTION

This routine is to receive data from socket with a timeout (millisecond). If returns -2, means wait timeout.

RETURNS

the number of bytes received, or ret < 0 if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_recvfrom( )

NAME

socket_recvfrom( ) - receive a message from a socket

SYNOPSIS

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 */
    )

DESCRIPTION

This routine receive a message from a socket.

RETURNS

the number of bytes received, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_recvfrom_timeout( )

NAME

socket_recvfrom_timeout( ) - receive a message with a timeout

SYNOPSIS

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 */
    )

DESCRIPTION

This routine is to receive a message from specify address with a timeout. If returns -2, means wait timeout.

RETURNS

the number of bytes received, or ret < 0 if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_getpeername( )

NAME

socket_getpeername( ) - get the name of a connected peer

SYNOPSIS

int socket_getpeername
    (
    int     sock,
    char *  address,
    short * port
    )

DESCRIPTION

This routine is to get the name of a connected peer, and convert the IP into a string, convert the port into host order.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_getsockname( )

NAME

socket_getsockname( ) - get local side socket name

SYNOPSIS

int socket_getsockname
    (
    int     sock,
    char *  address,
    short * port
    )

DESCRIPTION

This routine is to get local side socket name, and convert the IP into a string, convert the port into host order.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_select( )

NAME

socket_select( ) - pend on a set of socket descriptors and determines the status

SYNOPSIS

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 */
    )

DESCRIPTION

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).

RETURNS

The number of file descriptors with activity, 0 if timed out, or ERROR if an error occurred.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_setsockopt( )

NAME

socket_setsockopt( ) - set socket options

SYNOPSIS

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 */
    )

DESCRIPTION

This routine sets the options associated with a socket.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_getsockopt( )

NAME

socket_getsockopt( ) - get socket options

SYNOPSIS

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 */
    )

DESCRIPTION

This routine gets the options associated with a socket.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_set_block( )

NAME

socket_set_block( ) - sets blocking mode on a socket descriptor

SYNOPSIS

int socket_set_block
    (
    int sock
    )

DESCRIPTION

This routine sets blocking mode on a socket descriptor.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_set_nonblock( )

NAME

socket_set_nonblock( ) - sets nonblocking mode on a socket descriptor

SYNOPSIS

int socket_set_nonblock
    (
    int sock
    )

DESCRIPTION

This routine sets nonblocking mode on a socket descriptor.

RETURNS

OK/ERROR

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_errno( )

NAME

socket_errno( ) - get last error number of socket

SYNOPSIS

int socket_errno ()

DESCRIPTION

This routine is get last error number of socket. Later, you can all socket_error_string( ) to map the error number into a string.

RETURNS

error number

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_error( )

NAME

socket_error( ) - map the last error of socket into a string buffer

SYNOPSIS

char * socket_error
    (
    char errbuf[256]          /* the result will copy to */
    )

DESCRIPTION

This routine is to map the last error of socket into a string buffer. The map string will be copied into the parameter errbuf.

RETURNS

returned the error message, pointer to the source buffer.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_gethostbyname( )

NAME

socket_gethostbyname( ) - get the IP address and the address family

SYNOPSIS

char * socket_gethostbyname
    (
    const char * name,        /* domain/ip */
    char         addr[40],    /* the result will copy to */
    int *        addrtype     /* to return address family */
    )

DESCRIPTION

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.

RETURNS

pointer to the addr, or NULL if an error occur.

SEE ALSO

ebsocket


eyBuildLib : Routines

socket_gethostbynamel( )

NAME

socket_gethostbynamel( ) - get the list of IP addresses and the address family

SYNOPSIS

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 */
    )

DESCRIPTION

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.

RETURNS

the number of address, or ERROR if an error occur.

SEE ALSO

ebsocket