L6网络编程--网络编程扩展(day8、day9)

目录

一、域名解析

gethostbyname():

二、网络属性设置

三、网络超时优化 

 方法一:设置socket的属性 SO_RCVTIMEO

 方法二: 用select检测socket是否’ready’

 方法三: 设置定时器(timer), 捕捉SIGALRM信号

•思考:

试总结如何在linux中动态检查到是否有网络以及网络中途的掉线/连接的检查?

 方法一:

方法二:

四、广播编程(和组播,一对多采用UDP编程)

 一、广播发送(相当于client)

 二、广播接收(相当于server)

五、组播(多播)编程

网络地址

一、组播发送

二、组播接收 

 1、加入多播组

 六、UNIX域套接字

一、UNIX域(流式)套接字,TCP

 二、UNIX域(用户数据报)套接字,UDP

access:判断一个文件/路径是否存在,存在返回0;不存在返回-1;

unlink:删除一个文件/路径

 作业:

day8:

day9zuoye:


一、域名解析

网络信息检索函数

gethostname()  获得主机名
getpeername()  获得与套接口相连的远程协议地址
getsockname()  获得本地套接口协议地址
gethostbyname()  根据主机名取得主机信息
→endhostent()
gethostbyaddr()  根据主机地址取得主机信息
getprotobyname()  根据协议名取得主机协议信息
getprotobynumber()  根据协议号取得主机协议信息
getservbyname()  根据服务名取得相关服务信息
getservbyport()  根据端口号取得相关服务信息

gethostbyname():

L6网络编程--网络编程扩展(day8、day9)_第1张图片

 说明:IPv4中使用gethostbyname()函数完成主机名到地址解析,这个函数仅仅支持IPv4,且不允许调用者指定所需地址类型的任何信息,返回的结构只包含了用于存储IPv4地址的空间。IPv6中引入了getaddrinfo()的新API,它是协议无关的,既可用于IPv4也可用于IPv6。

client.c:


/*./client serv_name serv_port   给个域名或者IP地址*/
#include 
#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 
#include 
#include 
#include 
#include 
#include 
#include 			/* See NOTES */
#include 
#include 
#include 			/* superset of previous */

#include 
#include 
//#include 
#include 



#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 
#include 
//#include 
#include 
#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空闭时间
	in

你可能感兴趣的:(网络,服务器,linux)