day05
本章目标
read、write与recv、sendreadline实现- 用
readline实现回射客户端/服务器 getsockname、getpeernamegethostname、gethostbyname、gethostbyadr
read、write与recv、send函数
man recv
recv函数只能用于套接口的I/O·,read函数可以用于任何的I/O
recv函数增加了可选参数flgs,flags取值为MSG_OOB:接收紧急指针数据。MSG_PEEK:接收数据但是不把缓冲区的数据清除。
1 | #include <sys/types.h> |
##flags标志:
1 | MSG_OOB |
readline实现
readline:是为了解决粘包问题。
getsockname、getpeername
getsockname:获取套接口本地的地址
getpeername:获取远程套接口的地址
1 | #include <sys/socket.h> |
1 | #include <sys/socket.h> |
gethostname、gethostbyname和gethostbyaddr
gethostname:获取主机名字
gethostbyname:通过主机名获取所有的IP地址
1 | #include <unistd.h> |
1 | #include <netdb.h> |
注:
struct hostent的成员h_addr_list虽然是char **但是是需要用inet_ntoa转化一下才能输出IP地址。