1、Connection Establishment(1)connect() (conenection-oriented才用:SOCK_STREAM, SOCK_SEQPACKET)
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr, socklen_t len);
Returns: 0 if OK, -1 on error
|
注:1、如果sockfd没有绑定到地址,那么connect会绑定一个默认地址
2、SOCK_DGRAM也可以用connect,connect只是SOCK-DGRAM的一个优化替代,这样保证了目标地址用于发送和接收
(2)listen()
#include <sys/socket.h>
int listen(int sockfd, int backlog);
Returns: 0 if OK, -1 on error
|
backlog:暗示系统上进程上显著的连接数量,最大值SOMAXCONN,一旦队列满,系统会拒绝多余的连接请求(3)accpet() retrieve 1个连接请求并转化成一个连接
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict len);
Returns: file(socket) descriptor if OK, -1 on error
|
注:1、可以把addr和len设置为NULL,这样可以接受任意客户端连接
2、如果没有连接请求发生,accpet()会block直到有1个连接; 如果设置为nonblock mode,则返回-1并把errno设置为EAGAIN或EWOULDBLOCK
3、server可以用poll或者select等待连接请求到来,这样即将到来的连接请求socket表现出似乎是可读的
2、Data Transfer
(1)send()
#include <sys/socket.h>
ssize_t send(int sockfd, const void *buf, size_t nbytes, int flags);
Returns: number of bytes sent if OK, -1 on error
|
注:1、像fd的write, buf和nbytes同write 2、flags
Flag |
Description |
POSIX.1 |
FreeBSD 5.2.1 |
linux 2.4.22 |
MAC OS X 10.3 |
Solaris 9 |
|
|
|
|
|
|
|
MSG_DONTROUTE |
不要在local network之外路由包 |
|
★ |
★ |
★ |
★ |
MSG_DNOTWAIT |
允许O_NONBLOCK |
|
★ |
★ |
★ |
|
MSG_EOR |
若协议支持,表示记录结束 |
★ |
★ |
★ |
|
|
MSG_OOB |
若协议支持,发送out-of-band数据 |
★ |
★ |
★ |
★ |
★ |
3、若协议支持message boudaries,如果发送的单个message大于协议支持的最大数值,send()会失败并把errno设置为EMSGSIZE; 而byte-stream协议则会等待整个数据都被传送完(2) sendto()
#include <sys/socket.h>
ssize_t sendto(int sockfd, const void *buf, size_t nbytes, int flags, const struct sockaddr *destaddr, socklen_t destlen);
Returns: number of bytes sent if OK, -1 on error
|
注:1、如果是个connection-oriented socket,那么目的地址忽略;而connectionless socket不能用send,除非之前有connect指定了,而用sendto(3) sendmsg()
#include <sys/socket.h>
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
Returns number of bytes sent if OK, -1 on error
|
像writev()函数struct msghdr {
void *msg_name; // optional address
socklen_t msg_namelen; // address size in bytes
struct iovec *msg_iov; // array of I/O buffers
int msg_iovlen; // number of elements in array
void *msg_control; // ancillary data
socklen_t msg_controllen; // number of ancillary bytes
int msg_flags; // flags for received message
.
.
.
};
|
struct iovec
struct iovec {
void *iov_base; // starting address of buffer
size_t iov_len; // size of buffer;
};
|
(4) recv()#include <sys/socket.h>
ssize_t recv(int sockfd, void *buf, size_t nbytes, int flags);
Returns: length of message in bytes,
0 if no messages are available and peer has done an orderly shutdown
-1 on error
|
注:1、像fd的read() 2、flags
Flag |
Description |
POSIX.1 |
FreeBSD 5.2.1 |
Linux 2.4.22 |
MAC OS X 10.3 |
Solaris 9 |
|
|
|
|
|
|
|
MSG_OOB |
若协议支持,接受out-of-band数据 |
★ |
★ |
★ |
★ |
★ |
MSG_PEEK |
返回包裹内容without consuming packet |
★ |
★ |
★ |
★ |
★ |
MSG_TRUNC |
即使被truncated,也返回包裹的实际长度 |
|
|
★ |
|
|
MSG_WAITALL |
等待直到所有数据可取(只对SOCK_STREAM) |
★ |
★ |
★ |
★ |
★ |
3、MSG_PEEK是预读取下一部分数据的意思
(5) recvfrom()
#include <sys/socket.h>
ssize_t recvfrom(int sockfd, void *restrict buf, size_t len, int flags, struct sockaddr *restrict addr, socklen_t *restrict addrlen);
Returns: length of message in bytes,
0 if no messages are available and peer has done an orderly shutdown
-1 on error
|
注:常用于connectionless sockets
(6)recvmsg()
#include <sys/socket.h>
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
Returns: length of message in bytes, 0 if no messages are available and peer has done an orderly shutdown -1 on error
|
注:1、接收数据进multiple buffers,类似readv,或者想接收ancillary 数据 2、flags,
Flags |
Description |
POSIX.1 |
FreeBSD 5.2.1 |
Linux 2.4.22 |
MAC OS X 10.3 |
Solaris 9 |
|
|
|
|
|
|
|
MSG_CTRUNC |
控制数据被truncated |
★ |
★ |
★ |
★ |
★ |
MSG_DNOTWAIT |
recvmsg在nonblocking模式 |
|
|
★ |
|
★ |
MSG_EOR |
接收到记录结束 |
★ |
★ |
★ |
★ |
★ |
MSG_OOB |
接收到out-of-band数据 |
★ |
★ |
★ |
★ |
★ |
MSG_TRUNC |
普通数据被truncated |
★ |
★ |
★ |
★ |
★ |
阅读(735) | 评论(0) | 转发(0) |