目录
方法三: 设置定时器(timer), 捕捉SIGALRM信号
试总结如何在linux中动态检查到是否有网络以及网络中途的掉线/连接的检查?
access:判断一个文件/路径是否存在,存在返回0;不存在返回-1;
一、域名解析
网络信息检索函数
gethostbyname():
说明:IPv4中使用gethostbyname()函数完成主机名到地址解析,这个函数仅仅支持IPv4,且不允许调用者指定所需地址类型的任何信息,返回的结构只包含了用于存储IPv4地址的空间。IPv6中引入了getaddrinfo()的新API,它是协议无关的,既可用于IPv4也可用于IPv6。
client.c:
/*./client serv_name serv_port 给个域名或者IP地址*/
#include <netdb.h>
#include "net.h"
void usage (char *s)
{
printf ("\n%s serv_ip serv_port", s);
printf ("\n\t serv_name: server domain name or ip address");
printf ("\n\t serv_port: server port(>5000)\n\n");
}
int main (int argc, char **argv)
{
int fd = -1;
int port = -1;
struct sockaddr_in sin;
struct hostent *hs = NULL;
if (argc != 3) {
usage (argv[0]);
exit (1);
}
port = atoi (argv[2]);
if (port < 5000) {
usage (argv[0]);
exit (1);
}
if ((hs = gethostbyname (argv[1])) == NULL) { //等于空意味出错
herror ("gethostbyname error");//打印这个错误不能用perror
exit (1);
}
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*2.连接服务器 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (port); //网络字节序的端口号
#if 1
sin.sin_addr.s_addr = *((uint32_t *) hs->h_addr);//指针类型转换(h_addr是h_addr_list[0],有宏定义)
endhostent ();//释放结构体变量
hs = NULL;//避免野指针
#else
if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
if (connect (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("connect");
exit (1);
}
printf ("Client staring...OK!\n");
int ret = -1;
fd_set rset;
int maxfd = -1;
struct timeval tout;
char buf[BUFSIZ];
while (1) {
FD_ZERO (&rset);
FD_SET (0, &rset);
FD_SET (fd, &rset);
maxfd = fd;
tout.tv_sec = 5;
tout.tv_usec = 0;
select (maxfd + 1, &rset, NULL, NULL, &tout);
if (FD_ISSET (0, &rset)) { //标准键盘上有输入
//读取键盘输入,发送到网络套接字fd
bzero (buf, BUFSIZ);
do {
ret = read (0, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
continue;
}
if (!ret)
continue;
if (write (fd, buf, strlen (buf)) < 0) {
perror ("write() to socket");
continue;
}
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exiting!\n");
break;
}
}
if (FD_ISSET (fd, &rset)) { //服务器给发送过来了数据
//读取套接字数据,处理
bzero (buf, BUFSIZ);
do {
ret = read (fd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read from socket");
continue;
}
if (!ret)
break; /* 服务器关闭 */
//There is a BUG,FIXME!!
printf ("server said: %s\n", buf);
if ((strlen (buf) > strlen (SERV_RESP_STR))
&& !strncasecmp (buf + strlen (SERV_RESP_STR), QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Sender Client is exiting!\n");
break;
}
}
}
/*4.关闭套接字 */
close (fd);
}
net.h:
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/select.h>
#include <sys/time.h>
//#include <sys/types.h>
#include <unistd.h>
#define SERV_PORT 5002
#define SERV_IP_ADDR "192.168.7.246"
#define BACKLOG 5
#define QUIT_STR "quit"
#define SERV_RESP_STR "SERVER:"
#endif
server.c:
#include <pthread.h>
#include <signal.h>
//#include <linux/tcp.h>
#include <netinet/tcp.h>
#include "net.h"
void cli_data_handle (void *arg);
void setKeepAlive (int sockfd, int attr_on, socklen_t idle_time, socklen_t interval, socklen_t cnt);
void sig_child_handle (int signo)
{
if (SIGCHLD == signo) {
waitpid (-1, NULL, WNOHANG);
}
}
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
signal (SIGCHLD, sig_child_handle);
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*允许绑定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
#if 0
/* 允许广播 */
int b_br = 1;
setsockopt (fd, SOL_SOCKET, SO_BROADCAST, &b_br, sizeof (int));
/* 设置接收超时 */
struct timeval tout;
tout.tv_sec = 5;
tout.tv_usec = 0;
setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, &tout, sizeof (struct timeval));
#endif
/*2. 绑定 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
/*优化1: 让服务器程序能绑定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
/*3. 调用listen()把主动套接字变成被动套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("Server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客户端连接请求 */
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
while (1) {
pid_t pid = -1;
if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
perror ("accept");
break;
}
/*创建一个子进程用于处理已建立连接的客户的交互数据 */
if ((pid = fork ()) < 0) {
perror ("fork");
break;
}
if (0 == pid) { //子进程中
close (fd);
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntohs (cin.sin_port));
cli_data_handle (&newfd);
return 0;
} else { //实际上此处 pid >0, 父进程中
close (newfd);
}
}
close (fd);
return 0;
}
void cli_data_handle (void *arg)
{
int newfd = *(int *) arg;
printf ("Child handling process: newfd =%d\n", newfd);
int keepAlive = 1; //设定KeepAlive
int keepIdle = 5; //开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 5; //两次KeepAlive探测间的时间间隔
int keepCount = 3; //判定断开前的KeepAlive探测次数
setKeepAlive (newfd, keepAlive, keepIdle, keepInterval, keepCount);
//..和newfd进行数据读写
int ret = -1;
char buf[BUFSIZ];
char resp_buf[BUFSIZ + 10];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //对方已经关闭
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client(fd=%d) is exiting!\n", newfd);
break;
}
bzero (resp_buf, BUFSIZ + 10);
strncpy (resp_buf, SERV_RESP_STR, strlen (SERV_RESP_STR));
strcat (resp_buf, buf);
do {
ret = write (newfd, resp_buf, strlen (resp_buf));
} while (ret < 0 && EINTR == errno);
}
close (newfd);
}
void setKeepAlive (int sockfd, int attr_on, socklen_t idle_time, socklen_t interval, socklen_t cnt)
{
setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE, (const char *) &attr_on, sizeof (attr_on));
setsockopt (sockfd, SOL_TCP, TCP_KEEPIDLE, (const char *) &idle_time, sizeof (idle_time));
setsockopt (sockfd, SOL_TCP, TCP_KEEPINTVL, (const char *) &interval, sizeof (interval));
setsockopt (sockfd, SOL_TCP, TCP_KEEPCNT, (const char *) &cnt, sizeof (cnt));
}
Makefile:
# Makefile
#
#CROSS_COMPILE = arm-linux-gnu-
CC = $(CROSS_COMPILE)gcc
ifdef CROSS_COMPILE
TARGET = /opt/filesystem
endif
#DEBUG = -g -O0 -Wall
DEBUG = -g -O2
CFLAGS += $(DEBUG)
PROGS = ${patsubst %.c, %, ${wildcard *.c}}
all : $(PROGS)
install: $(PROGS)
ifdef CROSS_COMPILE
mkdir $(TARGET)/root/net -p
cp $(PROGS) $(TARGET)/root/net -f
endif
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: uninstall clean dist
uninstall :
ifdef CROSS_COMPILE
cd $(TARGET)/root/net && rm -f $(PROGS)
endif
clean : uninstall
- rm -f $(PROGS) core *.gz
dist: clean
tar czf ../makeru_2_dns_demo.tar.gz ../2_dns_demo
二、网络属性设置
SYNOPSIS int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); |
1)SOL_SOCKET: 通用套接字选项. .(应用层)
2)IPPROTO_IP:IP选项. (网络层)
3)IPPROTO_TCP:TCP选项 . (传输层)
optname 指定控制的方式 ( 选项的名称 ), 我们下面详细解释
========================================================================
SOL_SOCKET
------------------------------------------------------------------------
SO_BROADCAST 允许发送广播数据 int
SO_DEBUG 允许调试 int
SO_DONTROUTE 不查找路由 int
SO_ERROR 获得套接字错误 int
SO_KEEPALIVE 保持连接(默认2小时检查一次) int
SO_LINGER 延迟关闭连接 struct linger
SO_OOBINLINE 带外数据放入正常数据流 int
SO_RCVBUF 接收缓冲区大小 int
SO_SNDBUF 发送缓冲区大小 int
SO_RCVLOWAT 接收缓冲区下限 int
SO_SNDLOWAT 发送缓冲区下限 int
SO_RCVTIMEO 接收超时 struct timeval
SO_SNDTIMEO 发送超时 struct timeval
SO_REUSERADDR 允许重用本地地址和端口 int
SO_TYPE 获得套接字类型 int
SO_BSDCOMPAT 与 BSD 系统兼容 int
==========================================================================
IPPROTO_IP
-------------------------------------------------------------------------------------------------------------------------------
IP_OPTINOS IP 首部选项 int
IP_TOS 服务类型
IP_TTL 生存时间 int
==========================================================================
IPPRO_TCP
-----------------------------------------------------------------------------------------------------------------------------
TCP_MAXSEG TCP 最大数据段的大小 int
TCP_NODELAY 不使用 Nagle 算法 int
==========================================================
举例:
其中timeval的定义如下:
struct timeval { |
三、网络超时优化
方法一:设置socket的属性 SO_RCVTIMEO
设置socket的属性 SO_RCVTIMEO 参考代码如下 tv.tv_sec = 5; // 设置5秒时间 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, |
方法二: 用select检测socket是否’ready’
参考代码如下 struct timeval tv = {5 , 0}; // 设置5秒时间 FD_ZERO(&rdfs); if (select(sockfd+1, &rdfs, NULL, NULL, &tv) > 0) // socket就绪 } |
方法三: 设置定时器(timer), 捕捉SIGALRM信号
参考代码如下 struct sigaction act; |
•思考:
试总结如何在linux中动态检查到是否有网络以及网络中途的掉线/连接的检查?
方法一:
数据交互双方隔一段时间,一方发送一点数据到对方,对方给出特定的应答。如超过设定次数大小的时间内还是没有应答,这时候认为异常
方法二:
改变套接字的属性来实现
函数:
函数定义: void setKeepAlive (int sockfd, int attr_on, socklen_t idle_time, socklen_t interval, socklen_t cnt) setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE, (const char *) &attr_on, sizeof (attr_on)); |
int keepAlive = 1; //设定KeepAlive
int keepIdle = 5; //开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 5; //两次KeepAlive探测间的时间间隔
int keepCount = 3; //判定断开前的KeepAlive探测次数
setKeepAlive (newfd, keepAlive, keepIdle, keepInterval, keepCount);
四、广播编程(和组播,一对多采用UDP编程)
用于局域网内部一个发送,所有人接收
广播地址:
xxx.xxx.xxx.255
全网广播:255.255.255.255
一、广播发送(相当于client)
二、广播接收(相当于server)
要点:
广播编程是基于UDP编程
1. 发送方要通过setsockopt设置套接字广播属性
2.发送方的发送地址为广播地址:XXX.XXX.XXX.255
3.接收方和发送方的端口号要相同
net.h:
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/wait.h>
#include <arpa/inet.h> //14版本不用加,22版本必须加这两个头文件
#define SERV_PORT 5003
#define SERV_IP_ADDR "192.168.7.246"
#define QUIT_STR "quit"
#endif
Makefile :
# Makefile
#
#CROSS_COMPILE = arm-linux-gnu-
CC = $(CROSS_COMPILE)gcc
ifdef CROSS_COMPILE
TARGET = /opt/filesystem
endif
#DEBUG = -g -O0 -Wall
DEBUG = -g -O2
CFLAGS += $(DEBUG)
PROGS = ${patsubst %.c, %, ${wildcard *.c}}
all : $(PROGS)
install: $(PROGS)
ifdef CROSS_COMPILE
mkdir $(TARGET)/root/long_term/io -p
cp $(PROGS) $(TARGET)/root/long_term/io -f
endif
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: uninstall clean dist
uninstall :
ifdef CROSS_COMPILE
cd $(TARGET)/root/long_term/io && rm -f $(PROGS)
endif
clean : uninstall
- rm -f $(PROGS) core *.gz
dist: clean
tar czf ../makeru_boardcast_demo.tar.gz ../boardcast_demo
receiver.c:
#include "net.h"
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { //udp程序
perror ("socket");
exit (1);
}
/* 2. 允许绑定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
/*2. 绑定 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
/* 让服务器程序能绑定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
char buf[BUFSIZ];
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
printf ("\nBoardcast receiver started!\n");
while (1) {
bzero (buf, BUFSIZ);
if (recvfrom (fd, buf, BUFSIZ - 1, 0, (struct sockaddr *) &cin, &addrlen) < 0) {
perror ("recvfrom");
continue;
}
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Recived boardcast data:%s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Sender(%s:%d) is exiting!\n", ipv4_addr, ntohs (cin.sin_port));
}
}
close (fd);
return 0;
}
sender.c:
/*udp demo */
/* usage:
* ./client serv_ip serv_port
*/
#include "net.h"
void usage (char *s)
{
printf ("\nThis is udp demo!\n");
printf ("\nUsage:\n\t %s serv_ip serv_port", s);
printf ("\n\t serv_ip: udp server ip address");
printf ("\n\t serv_port: udp server port(serv_port > 5000)\n\n");
}
int main (int argc, char *argv[])
{
int fd = -1;
int port = SERV_PORT;
port = atoi (argv[2]);
if (port < 0 || (port > 0 && port <= 5000)) {
usage (argv[0]);
exit (1);
}
struct sockaddr_in sin;
if (argc != 3) {
usage (argv[0]);
exit (1);
}
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { //UDP编程
perror ("socket");
exit (1);
}
/* 允许广播设置 */
int b_br = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &b_br, sizeof(int));
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
#if 0
sin.sin_addr.s_addr = inet_addr (argv[1]);
#else
if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
printf ("broadcast demo started!\n");
char buf[BUFSIZ];
while (1) {
fprintf (stderr, "pls input string:");
bzero (buf, BUFSIZ);
if (fgets (buf, BUFSIZ - 1, stdin) == NULL) {
perror ("fgets");
continue;
}
sendto (fd, buf, strlen (buf), 0, (struct sockaddr *) &sin, sizeof (sin));
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exited!\n");
break;
}
}
close (fd);
return 0;
}
运行结果:(注意查看广播地址)
五、组播(多播)编程
•多播方式既可以发给多个主机,又能避免象广播那样带来过多的负载(每台主机要到传输层才能判断广播包是否要处理)
组播是一个人发送,加入到多播组的人接受数据
组播的IP地址: 224.0.0.1~239.255.255.254(中间除掉广播)
组播必须基于UDP的编程方法
网络地址
一、组播发送
二、组播接收
1、加入多播组
struct ip_mreq
{
struct in_addr imr_multiaddr;
struct in_addr imr_interface;
};
struct ip_mreq mreq;
bzero(&mreq, sizeof(mreq));
mreq.imr_multiaddr.s_addr = inet_addr(“235.10.10.3”);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
net.h:
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/wait.h>
#include <arpa/inet.h> //14版本不用加,22版本必须加这两个头文件
#define SERV_PORT 5004
#define MULTICAST_IP "235.10.10.3"
#define QUIT_STR "quit"
#endif
Makefile:
# Makefile
#
#CROSS_COMPILE = arm-linux-gnu-
CC = $(CROSS_COMPILE)gcc
ifdef CROSS_COMPILE
TARGET = /opt/filesystem
endif
#DEBUG = -g -O0 -Wall
DEBUG = -g -O2
CFLAGS += $(DEBUG)
PROGS = ${patsubst %.c, %, ${wildcard *.c}}
all : $(PROGS)
install: $(PROGS)
ifdef CROSS_COMPILE
mkdir $(TARGET)/root/net -p
cp $(PROGS) $(TARGET)/root/net -f
endif
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: uninstall clean dist
uninstall :
ifdef CROSS_COMPILE
cd $(TARGET)/root/net && rm -f $(PROGS)
endif
clean : uninstall
- rm -f $(PROGS) core *.gz
dist: clean
tar czf ../makeru_multicast_demo.tar.gz ../multicast_demo
sender.c(client):
/*udp demo */
/* usage:
* ./client serv_ip serv_port
*/
#include "net.h"
void usage (char *s)
{
printf ("\nThis is multicast demo!\n");
printf ("\nUsage:\n\t %s serv_ip serv_port", s);
printf ("\n\t serv_ip: udp server ip address(between 224~239 segment)");
printf ("\n\t serv_port: udp server port(serv_port > 5000)\n\n");
}
int main (int argc, char *argv[])
{
int fd = -1;
int port = SERV_PORT;//指定端口号
port = atoi (argv[2]);
if (port < 0 || (port > 0 && port <= 5000)) {
usage (argv[0]);
exit (1);
}
struct sockaddr_in sin;
if (argc != 3) {
usage (argv[0]);
exit (1);
}
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { //UDP编程
perror ("socket");
exit (1);
}
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
#if 0
sin.sin_addr.s_addr = inet_addr (argv[1]);
#else
if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
printf ("multicast started!\n");
char buf[BUFSIZ];
while (1) {
fprintf (stderr, "pls input string:");
bzero (buf, BUFSIZ);
if (fgets (buf, BUFSIZ - 1, stdin) == NULL) {
perror ("fgets");
continue;
}
sendto (fd, buf, strlen (buf), 0, (struct sockaddr *) &sin, sizeof (sin));
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exited!\n");
break;
}
}
close (fd);
return 0;
}
receiver.c:
#include "net.h"
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) { //udp程序
perror ("socket");
exit (1);
}
/* 2. 允许绑定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
/*加入多播组*/
struct ip_mreq mreq;
bzero(&mreq, sizeof(mreq));
mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_IP);//IP在net.h中
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,&mreq, sizeof(mreq));
/*2. 绑定 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
/* 让服务器程序能绑定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
char buf[BUFSIZ];
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
printf ("\nmulticast demo started!\n");
while (1) {
bzero (buf, BUFSIZ);
if (recvfrom (fd, buf, BUFSIZ - 1, 0, (struct sockaddr *) &cin, &addrlen) < 0) {
perror ("recvfrom");
continue;
}
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Recived from(%s:%d), data:%s", ipv4_addr, ntohs (cin.sin_port), buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client(%s:%d) is exiting!\n", ipv4_addr, ntohs (cin.sin_port));
}
}
close (fd);
return 0;
}
运行结果:
六、UNIX域套接字
socket(AF_LOCAL, SOCK_STREAM, 0)
socket(AF_LOCAL, SOCK_DGRAM, 0
用于本地进程间的通信
进程间通信: 1.进程间的数据共享: 管道、消息队列、共享内存、unix域套接字 易用性: 消息队列 > unix域套接字 >管道 > 共享内存(经常要和信号量一起用) 2. 异步通信: 信号 2. 同步和互斥(做资源保护) 信号量 |
struct sockaddr_un // <sys/un.h>
{
sa_family_t sun_family;
char sun_path[108]; // 套接字文件的路径(要求1:必须事先不存在;要求2:一般给绝对路径)
};
struct sockaddr_un myaddr;
bzero(&myaddr, sizeof(myaddr));
myaddr.sun_family = AF_UNIX;
strcpy(myaddr.sun_path, “/tmp/mysocket”);
一、UNIX域(流式)套接字,TCP
二、UNIX域(用户数据报)套接字,UDP
编程时:先bind()在socket_un的结构体变量
TCP编程:accept()..
UDP编程:sendto()/recvfrom()
access:判断一个文件/路径是否存在,存在返回0;不存在返回-1;
该模式指定要执行的辅助功能检查,并且
F_OK的值,或由一个按位 OR 组成的掩码
或更多R_OK、W_OK和X_OK。 F_OK检验
文件。 R_OK、W_OK和X_OK测试文件是否存在并授予
分别具有读取、写入和执行权限。
unlink:删除一个文件/路径
代码:
net.h:
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/select.h>
#include <sys/time.h>
//#include <sys/types.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/wait.h>
#define UNIX_DOMAIN_FILE "/tmp/my_domain_file.1"
#define BACKLOG 5
#define QUIT_STR "quit"
#define SERV_RESP_STR "SERVER:"
#endif
Makefile:
# Makefile
#
#CROSS_COMPILE = arm-linux-gnu-
CC = $(CROSS_COMPILE)gcc
ifdef CROSS_COMPILE
TARGET = /opt/filesystem
endif
#DEBUG = -g -O0 -Wall
DEBUG = -g -O2
CFLAGS += $(DEBUG)
PROGS = ${patsubst %.c, %, ${wildcard *.c}}
all : $(PROGS)
install: $(PROGS)
ifdef CROSS_COMPILE
mkdir $(TARGET)/root/net -p
cp $(PROGS) $(TARGET)/root/net -f
endif
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: uninstall clean dist
uninstall :
ifdef CROSS_COMPILE
cd $(TARGET)/root/net && rm -f $(PROGS)
endif
clean : uninstall
- rm -f $(PROGS) core *.gz
dist: clean
tar czf ../makeru_unix_domain_demo.tar.gz ../unix_domain
client.c:
/*./client unix_domain_file */
#include "net.h"
void usage (char *s)
{
printf ("\n%s unix_domain_file\n\n", s);
}
int main (int argc, char **argv)
{
int fd = -1;
int port = -1;
struct sockaddr_un sun;
if (argc != 2) {
usage (argv[0]);
exit (1);
}
/* 1. 创建socket fd */
if ((fd = socket (AF_LOCAL, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*2.连接服务器 */
/*2.1 填充struct sockaddr_un结构体变量 */
bzero (&sun, sizeof (sun));
sun.sun_family = AF_LOCAL;
/*确保UNIX_DOMAIN_FILE要先存在并且可写,不存在则退出 */
if( access(UNIX_DOMAIN_FILE, F_OK| W_OK) < 0){
exit(1);
}
strncpy(sun.sun_path, UNIX_DOMAIN_FILE, strlen( UNIX_DOMAIN_FILE));
if (connect (fd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
perror ("connect");
exit (1);
}
printf ("Unix domain Client staring...OK!\n");
int ret = -1;
fd_set rset;
int maxfd = -1;
struct timeval tout;
char buf[BUFSIZ];
while (1) {
FD_ZERO (&rset);
FD_SET (0, &rset);
FD_SET (fd, &rset);
maxfd = fd;
tout.tv_sec = 5;
tout.tv_usec = 0;
select (maxfd + 1, &rset, NULL, NULL, &tout);
if (FD_ISSET (0, &rset)) { //标准键盘上有输入
//读取键盘输入,发送到网络套接字fd
bzero (buf, BUFSIZ);
do {
ret = read (0, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
continue;
}
if (!ret)
continue;
if (write (fd, buf, strlen (buf)) < 0) {
perror ("write() to socket");
continue;
}
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exiting!\n");
break;
}
}
if (FD_ISSET (fd, &rset)) { //服务器给发送过来了数据
//读取套接字数据,处理
bzero (buf, BUFSIZ);
do {
ret = read (fd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read from socket");
continue;
}
if (!ret)
break; /* 服务器关闭 */
//There is a BUG,FIXME!!
printf ("server said: %s\n", buf);
if ((strlen(buf) > strlen(SERV_RESP_STR))
&& !strncasecmp (buf+strlen(SERV_RESP_STR), QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Sender Client is exiting!\n");
break;
}
}
}
/*4.关闭套接字 */
close (fd);
}
server.c:
#include <pthread.h>
#include <signal.h>
#include "net.h"
void cli_data_handle (void *arg);
void sig_child_handle(int signo)
{
if(SIGCHLD == signo) {
waitpid(-1, NULL, WNOHANG);
}
}
int main (void)
{
int fd = -1;
signal(SIGCHLD, sig_child_handle);
/* 1. 创建socket fd */
if ((fd = socket (AF_LOCAL, SOCK_STREAM, 0)) < 0) { //基于本地的TCP通信
perror ("socket");
exit (1);
}
/* 允许绑定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
/* 2.1 填充sockaddr_un结构体变量 */
struct sockaddr_un sun;
bzero(&sun, sizeof(sun));
sun.sun_family = AF_LOCAL;
/* 如果UNIX_DOMAIN_FILE所指向的文件存在,则删除 */
if(!access(UNIX_DOMAIN_FILE, F_OK)) {
unlink(UNIX_DOMAIN_FILE);
}
strncpy(sun.sun_path, UNIX_DOMAIN_FILE, strlen( UNIX_DOMAIN_FILE));
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sun, sizeof (sun)) < 0) {
perror ("bind");
exit (1);
}
/*3. 调用listen()把主动套接字变成被动套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("Unix domain server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客户端连接请求 */
while(1) {
pid_t pid = -1;
if ((newfd = accept (fd, NULL,NULL)) < 0) {
perror ("accept");
break;
}
/*创建一个子进程用于处理已建立连接的客户的交互数据*/
if((pid = fork()) < 0) {
perror("fork");
break;
}
if(0 == pid) { //子进程中
close(fd);
printf ("Clinet is connected!\n");
cli_data_handle(&newfd);
return 0;
} else { //实际上此处 pid >0, 父进程中
close(newfd);
}
}
close (fd);
return 0;
}
void cli_data_handle (void *arg)
{
int newfd = *(int *) arg;
printf ("Child handling process: newfd =%d\n", newfd);
//..和newfd进行数据读写
int ret = -1;
char buf[BUFSIZ];
char resp_buf[BUFSIZ+10];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //对方已经关闭
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client(fd=%d) is exiting!\n", newfd);
break;
}
bzero(resp_buf, BUFSIZ+10);
strncpy(resp_buf, SERV_RESP_STR, strlen(SERV_RESP_STR));
strcat(resp_buf, buf);
do {
ret = write(newfd, resp_buf, strlen(resp_buf));
}while(ret < 0 && EINTR == errno);
}
close (newfd);
}
运行结果:
作业:
day8:
1.请编写简要的DNS的解析代码
2. 请编写简要套接字编程中,用setsockopt()函数实现允许地址快速重用,以及设置接收10秒超时的相关服务器和客户端代码
(作业要求:做作业的时候不要再翻看视频上的教程,对函数理解不明白的全部通过man手册去查看,
自己思考框架,使用makefile编译,然后将测试的记录和结果添加到readme.txt文件中提交上来,代码实现完成测试通过后
再提交作业,网络部分学习不写代码不测试看不出问题的,良好的习惯帮助你们快速成长。)
3.请解释网络编程中的“心跳检测”概念和给出实现方法
答案:
1.
#include "net.h"
int main(int argc,char *argv[])
{
struct sockaddr_in sin;
int fd;
int ret;
struct hostent *hs = NULL; //定义hostent 结构体
char buf[BUFSIZ] = "hello world";
if(argc < 2){
printf("please input %s + ip 192.168.x.xxx\n",argv[0]);
exit(1);
}
//将输入的域名进行转化
if ((hs = gethostbyname (argv[1])) == NULL) {
herror ("gethostbyname error"); //用这个打印报错信息
exit (1);
}
//socket
if((fd = socket(AF_INET,SOCK_STREAM, 0) )< 0){
perror ("socket");
exit (1);
}
//connect
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT);
sin.sin_addr.s_addr = *(uint32_t *) hs->h_addr; //转化成IP
endhostent (); //释放结构体
hs = NULL; //防止野指针
if( connect (fd, (struct sockaddr *)&sin,sizeof(sin)) < 0){
perror ("connect ");
exit (1);
}
printf("connect success\n");
//write
if((ret =write (fd, buf, strlen(buf))) < 0){
perror("write");
exit(1);
}
//close
close (fd);
exit(0);
}
2.
net.h:
#ifndef __MAKEU_NET_H__
#define __MAKEU_NET_H__
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/select.h>
#include <sys/time.h>
//#include <sys/types.h>
#include <unistd.h>
#define SERV_PORT 5002
#define SERV_IP_ADDR "192.168.7.246"
#define BACKLOG 5
#define QUIT_STR "quit"
#define SERV_RESP_STR "SERVER:"
#endif
Makefile:
# Makefile
#
#CROSS_COMPILE = arm-linux-gnu-
CC = $(CROSS_COMPILE)gcc
ifdef CROSS_COMPILE
TARGET = /opt/filesystem
endif
#DEBUG = -g -O0 -Wall
DEBUG = -g -O2
CFLAGS += $(DEBUG)
PROGS = ${patsubst %.c, %, ${wildcard *.c}}
all : $(PROGS)
install: $(PROGS)
ifdef CROSS_COMPILE
mkdir $(TARGET)/root/net -p
cp $(PROGS) $(TARGET)/root/net -f
endif
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY: uninstall clean dist
uninstall :
ifdef CROSS_COMPILE
cd $(TARGET)/root/net && rm -f $(PROGS)
endif
clean : uninstall
- rm -f $(PROGS) core *.gz
dist: clean
tar czf ../makeru_2_dns_demo.tar.gz ../2_dns_demo
client.c:
/*./client serv_name serv_port 给个域名或者IP地址*/
#include <netdb.h>
#include "net.h"
void usage (char *s)
{
printf ("\n%s serv_ip serv_port", s);
printf ("\n\t serv_name: server domain name or ip address");
printf ("\n\t serv_port: server port(>5000)\n\n");
}
int main (int argc, char **argv)
{
int fd = -1;
int port = -1;
struct sockaddr_in sin;
struct hostent *hs = NULL;
if (argc != 3) {
usage (argv[0]);
exit (1);
}
port = atoi (argv[2]);
if (port < 5000) {
usage (argv[0]);
exit (1);
}
if ((hs = gethostbyname (argv[1])) == NULL) { //等于空意味出错
herror ("gethostbyname error");//打印这个错误不能用perror
exit (1);
}
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*2.连接服务器 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (port); //网络字节序的端口号
#if 1
sin.sin_addr.s_addr = *((uint32_t *) hs->h_addr);//指针类型转换(h_addr是h_addr_list[0],有宏定义)
endhostent ();//释放结构体变量
hs = NULL;//避免野指针
#else
if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
if (connect (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("connect");
exit (1);
}
printf ("Client staring...OK!\n");
int ret = -1;
fd_set rset;
int maxfd = -1;
struct timeval tout;
char buf[BUFSIZ];
while (1) {
FD_ZERO (&rset);
FD_SET (0, &rset);
FD_SET (fd, &rset);
maxfd = fd;
tout.tv_sec = 5;
tout.tv_usec = 0;
select (maxfd + 1, &rset, NULL, NULL, &tout);
if (FD_ISSET (0, &rset)) { //标准键盘上有输入
//读取键盘输入,发送到网络套接字fd
bzero (buf, BUFSIZ);
do {
ret = read (0, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
continue;
}
if (!ret)
continue;
if (write (fd, buf, strlen (buf)) < 0) {
perror ("write() to socket");
continue;
}
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client is exiting!\n");
break;
}
}
if (FD_ISSET (fd, &rset)) { //服务器给发送过来了数据
//读取套接字数据,处理
bzero (buf, BUFSIZ);
do {
ret = read (fd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read from socket");
continue;
}
if (!ret)
break; /* 服务器关闭 */
//There is a BUG,FIXME!!
printf ("server said: %s\n", buf);
if ((strlen (buf) > strlen (SERV_RESP_STR))
&& !strncasecmp (buf + strlen (SERV_RESP_STR), QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Sender Client is exiting!\n");
break;
}
}
}
/*4.关闭套接字 */
close (fd);
}
server.c:
#include <pthread.h>
#include <signal.h>
//#include <linux/tcp.h>
#include <netinet/tcp.h>
#include "net.h"
void cli_data_handle (void *arg);
void setKeepAlive (int sockfd, int attr_on, socklen_t idle_time, socklen_t interval, socklen_t cnt);
void sig_child_handle (int signo)
{
if (SIGCHLD == signo) {
waitpid (-1, NULL, WNOHANG);
}
}
int main (void)
{
int fd = -1;
struct sockaddr_in sin;
signal (SIGCHLD, sig_child_handle);
/* 1. 创建socket fd */
if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror ("socket");
exit (1);
}
/*允许绑定地址快速重用 */
int b_reuse = 1;
setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
/*2. 绑定 */
/*2.1 填充struct sockaddr_in结构体变量 */
bzero (&sin, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
/*优化1: 让服务器程序能绑定在任意的IP上 */
#if 1
sin.sin_addr.s_addr = htonl (INADDR_ANY);
#else
if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
perror ("inet_pton");
exit (1);
}
#endif
/*2.2 绑定 */
if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
perror ("bind");
exit (1);
}
/*3. 调用listen()把主动套接字变成被动套接字 */
if (listen (fd, BACKLOG) < 0) {
perror ("listen");
exit (1);
}
printf ("Server starting....OK!\n");
int newfd = -1;
/*4. 阻塞等待客户端连接请求 */
struct sockaddr_in cin;
socklen_t addrlen = sizeof (cin);
while (1) {
pid_t pid = -1;
if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
perror ("accept");
break;
}
/*创建一个子进程用于处理已建立连接的客户的交互数据 */
if ((pid = fork ()) < 0) {
perror ("fork");
break;
}
if (0 == pid) { //子进程中
close (fd);
char ipv4_addr[16];
if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
perror ("inet_ntop");
exit (1);
}
printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntohs (cin.sin_port));
cli_data_handle (&newfd);
return 0;
} else { //实际上此处 pid >0, 父进程中
close (newfd);
}
}
close (fd);
return 0;
}
void cli_data_handle (void *arg)
{
int newfd = *(int *) arg;
printf ("Child handling process: newfd =%d\n", newfd);
int keepAlive = 1; //设定KeepAlive
int keepIdle = 5; //开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 5; //两次KeepAlive探测间的时间间隔
int keepCount = 3; //判定断开前的KeepAlive探测次数
setKeepAlive (newfd, keepAlive, keepIdle, keepInterval, keepCount);
//..和newfd进行数据读写
int ret = -1;
char buf[BUFSIZ];
char resp_buf[BUFSIZ + 10];
while (1) {
bzero (buf, BUFSIZ);
do {
ret = read (newfd, buf, BUFSIZ - 1);
} while (ret < 0 && EINTR == errno);
if (ret < 0) {
perror ("read");
exit (1);
}
if (!ret) { //对方已经关闭
break;
}
printf ("Receive data: %s\n", buf);
if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
printf ("Client(fd=%d) is exiting!\n", newfd);
break;
}
bzero (resp_buf, BUFSIZ + 10);
strncpy (resp_buf, SERV_RESP_STR, strlen (SERV_RESP_STR));
strcat (resp_buf, buf);
do {
ret = write (newfd, resp_buf, strlen (resp_buf));
} while (ret < 0 && EINTR == errno);
}
close (newfd);
}
void setKeepAlive (int sockfd, int attr_on, socklen_t idle_time, socklen_t interval, socklen_t cnt)
{
setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE, (const char *) &attr_on, sizeof (attr_on));
setsockopt (sockfd, SOL_TCP, TCP_KEEPIDLE, (const char *) &idle_time, sizeof (idle_time));
setsockopt (sockfd, SOL_TCP, TCP_KEEPINTVL, (const char *) &interval, sizeof (interval));
setsockopt (sockfd, SOL_TCP, TCP_KEEPCNT, (const char *) &cnt, sizeof (cnt));
}
3.
1.应用层,使用心跳检测(客户端每过一段时间就给服务器发一个心跳包,告诉我还活着。)
2. setsockopt属性修改参数中,有个SO_KEEPALIVE 保持连接的属性,它在不修改的情况下默认是2个小时检查一次连接。
int keepAlive = 1; //设定KeepAlive
int keepIdle = 5; //开始首次KeepAlive探测前的TCP空闭时间
int keepInterval = 5; //两次KeepAlive探测间的时间间隔
int keepCount = 3; //判定断开前的KeepAlive探测次数
setKeepAlive (newfd, keepAlive, keepIdle, keepInterval, keepCount);//这个函数内部调用了setsockopt函数。
来改变套接字的属性,来完成网络的检测。
day9zuoye:
1.【不定项选择】 以下功能用到UDP协议实现的是 ( AB)
A. 广播
B. 组播
C.带外数据
D.ping
2.用于同一主机内部进程间通信的socket应使用的地址族是( D)
A. AF_INET
B. AF_NETLINK
C. AF_PACKET
D. AF_UNIX或AF_LOCAL
3. 请编写组播通信的服务器和客户端的代码:
(作业要求:做作业的时候不要再翻看视频上的教程,对函数理解不明白的全部通过man手册去查看,
自己思考框架,使用makefile编译,然后将测试的记录和结果添加到readme.txt文件中提交上来,代码实现完成测试通过后
再提交作业,网络部分学习不写代码不测试看不出问题的,良好的习惯帮助你们快速成长。)
见上文