第一部分:常用结构
struct sockaddr {
u_short sa_family;
char sa_data[14];
};
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
u_int16_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* Internet address. */
struct in_addr {
u_int32_t s_addr; /* address in network byte order */
};
第二部分:协议类型
Name Purpose Man page
PF_UNIX, PF_LOCAL Local communication unix(7)
PF_INET IPv4 Internet protocols ip(7)
PF_INET6 IPv6 Internet protocols
PF_IPX IPX - Novell protocols
PF_NETLINK Kernel user interface device netlink(7)
PF_X25 ITU-T X.25 / ISO-8208 protocol x25(7)
PF_AX25 Amateur radio AX.25 protocol
PF_ATMPVC Access to raw ATM PVCs
PF_APPLETALK Appletalk ddp(7)
PF_PACKET Low level packet interface packet(7)
第三部分:socket类型
SOCK_STREAM
Provides sequenced, reliable, two-way, connection-based byte
streams. An out-of-band data transmission mechanism may be sup-
ported.
SOCK_DGRAM
Supports datagrams (connectionless, unreliable messages of a
fixed maximum length).
SOCK_SEQPACKET
Provides a sequenced, reliable, two-way connection-based data
transmission path for datagrams of fixed maximum length; a con-
sumer is required to read an entire packet with each read system
call.
SOCK_RAW
Provides raw network protocol access.
SOCK_RDM
Provides a reliable datagram layer that does not guarantee
ordering.
SOCK_PACKET
Obsolete and should not be used in new programs; see packet(7).
Some socket types may not be implemented by all protocol families; for
example, SOCK_SEQPACKET is not implemented for AF_INET.
第四部分:函数
==============================================================================
NAME
socket - create an endpoint for communication
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
RETURN VALUE
-1 is returned if an error occurs; otherwise the return value is a
descriptor referencing the socket.
==============================================================================
NAME
connect - initiate a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t
addrlen);
RETURN VALUE
If the connection or binding succeeds, zero is returned. On error, -1
is returned, and errno is set appropriately.
==============================================================================
NAME
bind - bind a name to a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
==============================================================================
NAME
accept - accept a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
RETURN VALUE
The call returns -1 on error. If it succeeds, it returns a non-nega-
tive integer that is a descriptor for the accepted socket.
==============================================================================
NAME
send, sendto, sendmsg - send a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int s, const void *msg, size_t len, int flags);
ssize_t sendto(int s, const void *msg, size_t len, int flags, const
struct sockaddr *to, socklen_t tolen);
ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
RETURN VALUE
The calls return the number of characters sent, or -1 if an error
occurred.
==============================================================================
NAME
recv, recvfrom, recvmsg - receive a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
ssize_t recv(int s, void *buf, size_t len, int flags);
ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sock-
addr *from, socklen_t *fromlen);
ssize_t recvmsg(int s, struct msghdr *msg, int flags);
RETURN VALUE
These calls return the number of bytes received, or -1 if an error
occurred.
==============================================================================
NAME
getpeername - get name of connected peer socket
SYNOPSIS
#include <sys/socket.h>
int getpeername(int s, struct sockaddr *name, socklen_t *namelen);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
==============================================================================
NAME
getsockname - get socket name
SYNOPSIS
#include <sys/socket.h>
int getsockname(int s, struct sockaddr *name, socklen_t *namelen);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
==============================================================================
NAME
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O
multiplexing
SYNOPSIS
/* According to POSIX 1003.1-2001 */
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);
int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set
*exceptfds, const struct timespec *timeout, const sigset_t *sigmask);
FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);
FD_ZERO(fd_set *set);
RETURN VALUE
On success, select and pselect return the number of descriptors con-
tained in the descriptor sets, which may be zero if the timeout expires
before anything interesting happens. On error, -1 is returned, and
errno is set appropriately; the sets and timeout become undefined, so
do not rely on their contents after an error.
ERRORS
EBADF An invalid file descriptor was given in one of the sets.
EINTR A non blocked signal was caught.
EINVAL n is negative or the value contained within timeout is invalid.
ENOMEM select was unable to allocate memory for internal tables.
EXAMPLE
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don鈥檛 rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now./n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds./n");
return 0;
}
struct sockaddr {
u_short sa_family;
char sa_data[14];
};
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */
u_int16_t sin_port; /* port in network byte order */
struct in_addr sin_addr; /* internet address */
};
/* Internet address. */
struct in_addr {
u_int32_t s_addr; /* address in network byte order */
};
第二部分:协议类型
Name Purpose Man page
PF_UNIX, PF_LOCAL Local communication unix(7)
PF_INET IPv4 Internet protocols ip(7)
PF_INET6 IPv6 Internet protocols
PF_IPX IPX - Novell protocols
PF_NETLINK Kernel user interface device netlink(7)
PF_X25 ITU-T X.25 / ISO-8208 protocol x25(7)
PF_AX25 Amateur radio AX.25 protocol
PF_ATMPVC Access to raw ATM PVCs
PF_APPLETALK Appletalk ddp(7)
PF_PACKET Low level packet interface packet(7)
第三部分:socket类型
SOCK_STREAM
Provides sequenced, reliable, two-way, connection-based byte
streams. An out-of-band data transmission mechanism may be sup-
ported.
SOCK_DGRAM
Supports datagrams (connectionless, unreliable messages of a
fixed maximum length).
SOCK_SEQPACKET
Provides a sequenced, reliable, two-way connection-based data
transmission path for datagrams of fixed maximum length; a con-
sumer is required to read an entire packet with each read system
call.
SOCK_RAW
Provides raw network protocol access.
SOCK_RDM
Provides a reliable datagram layer that does not guarantee
ordering.
SOCK_PACKET
Obsolete and should not be used in new programs; see packet(7).
Some socket types may not be implemented by all protocol families; for
example, SOCK_SEQPACKET is not implemented for AF_INET.
第四部分:函数
==============================================================================
NAME
socket - create an endpoint for communication
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
RETURN VALUE
-1 is returned if an error occurs; otherwise the return value is a
descriptor referencing the socket.
==============================================================================
NAME
connect - initiate a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t
addrlen);
RETURN VALUE
If the connection or binding succeeds, zero is returned. On error, -1
is returned, and errno is set appropriately.
==============================================================================
NAME
bind - bind a name to a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
==============================================================================
NAME
accept - accept a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
RETURN VALUE
The call returns -1 on error. If it succeeds, it returns a non-nega-
tive integer that is a descriptor for the accepted socket.
==============================================================================
NAME
send, sendto, sendmsg - send a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
ssize_t send(int s, const void *msg, size_t len, int flags);
ssize_t sendto(int s, const void *msg, size_t len, int flags, const
struct sockaddr *to, socklen_t tolen);
ssize_t sendmsg(int s, const struct msghdr *msg, int flags);
RETURN VALUE
The calls return the number of characters sent, or -1 if an error
occurred.
==============================================================================
NAME
recv, recvfrom, recvmsg - receive a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
ssize_t recv(int s, void *buf, size_t len, int flags);
ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sock-
addr *from, socklen_t *fromlen);
ssize_t recvmsg(int s, struct msghdr *msg, int flags);
RETURN VALUE
These calls return the number of bytes received, or -1 if an error
occurred.
==============================================================================
NAME
getpeername - get name of connected peer socket
SYNOPSIS
#include <sys/socket.h>
int getpeername(int s, struct sockaddr *name, socklen_t *namelen);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
==============================================================================
NAME
getsockname - get socket name
SYNOPSIS
#include <sys/socket.h>
int getsockname(int s, struct sockaddr *name, socklen_t *namelen);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
==============================================================================
NAME
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O
multiplexing
SYNOPSIS
/* According to POSIX 1003.1-2001 */
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);
int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set
*exceptfds, const struct timespec *timeout, const sigset_t *sigmask);
FD_CLR(int fd, fd_set *set);
FD_ISSET(int fd, fd_set *set);
FD_SET(int fd, fd_set *set);
FD_ZERO(fd_set *set);
RETURN VALUE
On success, select and pselect return the number of descriptors con-
tained in the descriptor sets, which may be zero if the timeout expires
before anything interesting happens. On error, -1 is returned, and
errno is set appropriately; the sets and timeout become undefined, so
do not rely on their contents after an error.
ERRORS
EBADF An invalid file descriptor was given in one of the sets.
EINTR A non blocked signal was caught.
EINVAL n is negative or the value contained within timeout is invalid.
ENOMEM select was unable to allocate memory for internal tables.
EXAMPLE
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don鈥檛 rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now./n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds./n");
return 0;
}