BSD Sockets API |
int socket (int family, int type, int protocol);
Creates a socket. Currentlycan only be
family
(OT does not support IPv6 so there is no AF_INET6 support).
AF_INET
can be
protocol
or
PF_INET
(both have the same effect of creating an internet socket.
PF_UNSPEC
can be
type
for TCP sockets or
SOCK_STREAM
for UDP sockets.
SOCK_DGRAM
returns a socket file descriptor (
socket()
) which is a small non-negative integer. This file descriptor number should be used for all other socket operations on that socket. If
sockFD
encounters an error, it will return -1, in which case the application should call
socket()
GetMITLibError()
to get the error code.
NOTE: errno is not supported by the Sockets Library. Use ErrorLib.
int socket_bind (int sockFD, const struct sockaddr *myAddr, int addrLength);
Binds a socket to a local port and/or IP address. Either the port or the IP address in thestructure may be wildcarded in which case the library will select appropriate values. To wildcard the port set the
struct sockaddr
field of the address to 0. To wildcard the IP, set the
sin_port
field of the address to
sin_addr.s_addr
TCP has a port reuse time limit. If an application attempts to bind to the same local port twice in rapid succession, the second attempt will fail with
INADDR_ANY
. Just wait and try again.
EADDRINUSE
returns 0 on success and -1 on failure. If -1 is returned, call
socket_bind()
GetMITLibError()
to get the error code.
Note that the second argument to
is actually a
socket_bind()
which is cast to the more general
struct sockaddr_in*
struct sockaddr*
because internet addresses are used by the Socket Library.
Also, it is not necessary to bind a socket prior to connecting it. If a socket is not bound the library will choose the local port and IP.
int socket_connect (int sockFD, struct sockaddr *servAddr, int addrLength);
Connects a socket to a remote host on a given port. If this is a(TCP) socket,
SOCK_STREAM
will actually perform TCP negotation to open a connection. If this is a
socket_connect()
(UDP) socket,
SOCK_DGRAM
will just store the address for use later with other socket operations (this is important to note because if
socket_connect()
refers to a UDP socket, errors will not be reported prior to an attempt to read or write data on the socket).
sockFD
returns 0 on success and -1 on failure. If -1 is returned, call
socket_connect()
GetMITLibError()
to get the error code.
Note that the second argument to
is actually a
connect
which is cast to the more general
struct sockaddr_in*
because internet addresses are used by the Socket Library.
struct sockaddr*
int socket_select (int maxFDsExamined, fd_set *readFDs, fd_set *writeFDs, fd_set*exceptFDs, struct timeval *timeOut);
allows for conditions to be checked on one or more sockets.
socket_select()
should be one greater than value of the largest valued socket descriptor in any of the lists. If you don't wish to calculate this, use
maxFDsExamined
instead. For small numbers of sockets,
FD_SETSIZE
socket_select()
will be less efficient if FD_SETSIZE is passed.
If the bit corresponding to a socket in an
is set, that socket will be checked for the condition the
fd_set
corresponds to. If the condition they are checked for is true, they will still be set to
fd_set
when
true
returns (otherwise they will be set to false). Note that the sets are modified by
socket_select()
: thus they must be reset between each call to the function.
socket_select()
s can be manipulated using the following macros:
fd_set
Currently only the
FD_SET(fd, fdset)
Sets socket in
fd
to true.
fdset
FD_CLR(fd, fdset)
Sets socket in
fd
to false.
fdset
FD_ISSET(fd, fdset)
Returns iff socket
true
is set to true in
fd
.
fdset
FD_ZERO(fdset)
Sets all the sockets in to false.
fdset
condition (whether there is data to read on a socket) is supported. However, in order to stay compatible with most clients,
readfds
(whether there is room in the kernel buffers to write to a socket) behaves as though writing data will succeed (this is usually fine) and
writefds
behaves as though there are no exception conditions on the socket (
exceptfds
exceptfds
will always be returned with all sockets set to false).
If
is NULL,
timeout
blocks until one of the conditions becomes true for one of the sockets. If
socket_select()
is non-NULL,
timeout
checks for the amount of time corresponding to
socket_select()
and then returns (regardless of whether it has found any conditions to be true). If one of the conditions becomes true for one of the sockets it finds before the timeout has expired, it always returns immediately. To use
timeout
in non-blocking mode call it with a non-null
socket_select()
whose
timeOut
and
tv_sec
fields are both set to zero.
tv_usec
returns the number of sockets for which the specified conditions are true. If it encounters an error, it will return -1 (in which case
socket_select()
can be called to retrieve the error code.)
GetMITLibError()
int socket_fcntl (int sockFD, int command, int flags);
socket_fcntl()
sets various options on a socket. Currently the only flag supported is O_NONBLOCK which sets a socket to non-blocking mode.
If
is called with
socket_fcntl()
equal to
command
it will return the current flags for the socket
F_GETFL
The parameter
sockFD.
flags
is ignored in this case.
If the function is called with
equal to
command
it will replace the socket's flags with those specified by
F_SETFL
The correct way to change a flag on a socket is as illustrated in the following example:
flags.
Getting the current flags and modifying them avoids the potential error of clearing other flags.
flags = socket_fcntl(sockfd, F_GETFL, 0); flags |= O_NONBLOCK; err = socket_fcntl(sockfd, F_SETFL, flags);
returns 0 on success and -1 on failure. If -1 is returned, call
socket_fcntl()
GetMITLibError()
to get the error code.
Note: the corresponding BSD call,
takes a variable number of arguments while
fcntl
does not.
socket_fcntl
int socket_getpeername (int sockFD, struct sockaddr *peerAddr, int *addrLength);
gets the address of the remote host the socket is connected to, if any. If the socket is not connected,
socket_getpeername()
will return
GetMITLibError()
.
ENOTCONN
returns 0 on success and -1 on failure. If -1 is returned, call
socket_getpeername()
to get the error code.
GetMITLibError()
int socket_getsockname (int sockFD, struct sockaddr *localAddr, int *addrLength);
gets the socket's local address and port.
socket_getsockname()
returns 0 on success and -1 on failure. If -1 is returned, call
socket_getsockname()
to get the error code.
GetMITLibError()
int socket_read (int sockFD, void *buffer, UInt32 numBytes);
Reads data from the socket into.
buffer
should be the size of the buffer.
numBytes
may not fill the entire buffer.
socket_read()
returns the amount of data which was read. If there is an error, -1 is returned and
socket_read()
can be called to retrieve the error code. If 0 is returned, this means that the socket received an EOF (the remote host closed the connection gracefully.) To perform a full read on a socket, continue to call
GetMITLibError()
until the desired number of bytes have been accumulated. Note that
socket_read()
may block if no data is available to be read. This condition can be checked using
socket_read()
socket_select()
.
The socket must be connected.
Note: the standard library call
is not supported for sockets.
read()
int socket_write (int sockFD, void *buffer, UInt32 numBytes);
Writes data to the socket from.
buffer
should be the amount of data in the buffer.
numBytes
may not write out the entire buffer.
socket_write()
returns the amount of data which was written. If there is an error, -1 is returned and
socket_write()
GetMITLibError()
can be called to get the error code.
The socket must be connected.
int socket_readv (int sockFD, struct iovec *iov, UInt32 iovCount);
Thefunction is equivalent to
socket_readv()
, but places the input data into the
socket_read()
buffers specified by the members of the
iovcnt
array:
iov
. The
iov0, iov1, ..., iov[iovcnt-1]
argument is valid if greater than 0 and less than or equal to
iovcnt
IOV_MAX
.
The
structure contains the following members:
iovec
Each
caddr_t iov_base;
int iov_len;
entry specifies the base address and length of an area in memory where data should be placed. The
iovec
socket_readv()
function always fills an area completely before proceeding to the next.
Upon successful completion,
marks for update the
socket_readv()
field of the file.
st_atime
returns the total amount of data which was read into all buffers. If there is an error, -1 is returned and
socket_readv()
GetMITLibError()
can be called to get the error code. If 0 is returned, this means that the socket got an EOF (the remote host closed the connection gracefully.
The socket must be connected.
int socket_writev (int sockFD, struct iovec *iov, UInt32 iovCount);
Thefunction performs the same action as
socket_writev()
, but gathers the output data from the
socket_write()
buffers specified by the members of the
iovcnt
array:
iov
. The
iov[0], iov[1], ..., iov[iovcnt-1]
buffer is valid if greater than 0 and less than or equal to
iovcnt
IOV_MAX
.
The
structure contains the following members:
iovec
caddr_t iov_base;
Each
int iov_len;
entry specifies the base address and length of an area in memory from which data should be written.
iovec
always writes all data from an area before proceeding to the next.
socket_writev()
returns the amount of data which was written. If there is an error, -1 is returned and
socket_writev()
GetMITLibError()
can be called to get the error code.
The socket must be connected.
int socket_recv (int sockFD, void *buffer, UInt32 numBytes, int flags);
This function is similiar towith the addition of a final parameter.
socket_read()
reads data from the socket into
socket_recv()
.
buffer
should be the size of the buffer.
numBytes
may not fill the entire buffer. If
socket_recv()
is set to
flags
, then
MSG_DONTWAIT
will not block if not data is available.
socket_recv
returns the amount of data which was read. If there is an error, -1 is returned and
socket_recv()
GetMITLibError()
can be called to get the error code. If 0 is returned, this means that the socket received an EOF (the remote host closed the connection gracefully.
The socket must be connected.
int socket_send (int sockFD, void *buffer, UInt32 numBytes, int flags);
Similiar to,
socket_write()
writes data to the socket from
socket_send()
.
buffer
should be the amount of data in the buffer.
numBytes
may not write out the entire buffer. If
socket_send()
is set to
flags
, then
MSG_DONTWAIT
will not block waiting for buffers to become free.
socket_send()
returns the amount of data which was written. If there is an error, -1 is returned and
socket_send()
GetMITLibError()
can be called to receive the error code.
The socket must be connected.
int socket_recvfrom (int sockFD, void *buffer, UInt32 numBytes, int flags, struct sockaddr *fromAddr, socklen_t *addrLength);
Reads data from the remote host specified byinto
fromAddr
. The socket must be a
buffer
(UDP) socket.
SOCK_DGRAM
should be the size of the buffer.
numBytes
may not fill the entire buffer. If
socket_recvfrom()
is set to
flags
, then
MSG_DONTWAIT
socket_recvfrom()
will not block waiting for data.
Note that
will actually be a
fromAddr
struct sockaddr_in*
returns the amount of data which was read. If there is an error, -1 is returned and
socket_recvfrom()
may be called to get the error code. If 0 is returned, this means that the socket got an EOF (the remote host closed the connection gracefully.
GetMITLibError()
int socket_sendto (int sockFD, void *buffer, UInt32 numBytes, int flags, struct sockaddr *toAddr, socklen_t addrLength);
Writes data the remote host specified byinto
fromAddr
. The socket must be a
buffer
(UDP) socket.
SOCK_DGRAM
should be the amount of data in the buffer.
numBytes
may not write out the entire buffer. If
socket_sendto()
is set to
flags
, then
MSG_DONTWAIT
socket_sendto()
will not block waiting for buffers to become free.
Note that
will actually be a
toAddr
struct sockaddr_in*
returns the amount of data which was written. If there is an error, -1 is returned and
socket_sendto()
can be called to get the error code.
GetMITLibError()
int socket_shutdown (int sockFD, int howTo);
closes one or both directions of a connected socket.
socket_shutdown()
can be
howTo
,
SHUT_RD
or
SHUT_WR
.
SHUT_RDWR
tells it to close the reading side of the connection (reads from the socket will no longer be possible).
SHUT_RD
tells it to close the writing half of the socket (this will cause it to send an orderly disconnect to the remote host, telling that host it no longer has anything to write).
SHUT_WR
tells it to close both halves of the connection (It is still necessary to free the socket with
SHUT_RDWR
).
socket_close()
returns 0 on success and -1 on failure. If -1 is returned, call
socket_shutdown()
to get the error code.
GetMITLibError()
int socket_close (int sockFD);
frees a socket's resources, disconnecting it from the remote host, if necessary. Both TCP and UDP sockets should be closed with this function.
socket_close()
returns 0 on success and -1 on failure. If -1 is returned, call
socket_close()
to get the error code.
GetMITLibError()
來自:http://web.mit.edu/macdev/Development/MITSupportLib/SocketsLib/Documentation/sockets.html
作者:Alexliu(alex dotNet Learning)
出處:http://alexliu.cnblogs.com/