网络编程

//================================================================================================================================
[1] >> 提问:网络基础知识?
<>-->>端口号16(0-65535:无符号类型) = 常用端口(1~1023:知名端口占1字节为1255,unix占用  01023 )  + 不能使用的端口(1024 ~ 5000)  + 用户端口( 5000 ~ 65535 )
<>-->>字节序 = 大端模式 (高字节-->低地址) + 小端模式(低字节-->低地址) #网络字节序,即大端字节序
<>-->>Internet是“冷战”的产物--阿帕网
<>-->>TCP / IP层次
[
	应用层
	表示层
	会话层
	传输层
	网络层
	数据链路层
	物理层
]
//================================================================================================================================
[2] >> socket编程
[
	<>流式套接字SOCK_STREAM: 面向TCP编程,数据被看作是字节流
	<>数据报套接字SOCK_DGRAM:面向UDP编程,数据以独立数据包的形式发送
	<>原始套接字_SOCK_RAW:   穿透TCP/UDP传输层,能在网络层进行编程[raw = 未加工的]
	<>-->>int inet_pton(int af, const char *src, void *dst);[return:-1-->errro]
	[
		 description :
		 	convert IPv4 and IPv6 addresses from text to binary form
		 	This function converts the character string src into a network address structure in the af address family, 
		 	then copies the network address structure to dst.
		The af argument must be either AF_INET or AF_INET6.
	]

	<>-->>convert(转换) values between host and network byte order
	[
		<>uint32_t htonl(uint32_t hostlong); #l = long
		#The htonl() function converts the unsigned integer(整数) hostlong from host(主机) byte order to network byte order.
		<>uint16_t htons(uint16_t hostshort); #s = short
		#The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.	
		<>uint32_t ntohl(uint32_t netlong);
		#The ntohl() function converts the unsigned integer netlong from network byte order to host byte order.	
		<>uint16_t ntohs(uint16_t netshort);
		#The ntohs() function converts the unsigned short integer netshort from network byte order to host byte order.
	]	
]
//================================================================================================================================
[3] >> TCP编程?
[
	----------------------------------------------------------------------------------
								 TCP编程流程:
								 [服务器端]         [客户端] 
								  socket             socket
								    ↓                  |
								   bind		    	   ||
								  listen	           |
								    ↓	               ↓ 
								  accept <--------- connect
								    ↓                  ↓
								   --------------------------
								   read               write
								   write              read
								   --------------------------
								    ↓				   ↓
								   close             close    
	----------------------------------------------------------------------------------
	struct sockaddr.sin_addr = INADDY_ANY = 动态IP
	<>1>>  int socket(int domain, int type, int protocol);
	[
		DESCRIPTION:
		socket() creates an endpoint(末尾节点) for communication and returns a file descriptor that refers to 
		that endpoint
		The domain argument specifies a communication domain(区域)
			 AF_INET      IPv4 Internet protocols     
			 AF_INET6     IPv6 Internet protocols  
			 AF_LOCAL     Synonym for AF_UNIX
		 Currently(一般) defined types are:
			SOCK_STREAM
			SOCK_DGRAM
			SOCK_RAW
		 protocol(协议):
			UDP与TCP编程时写0
		  On success, a file descriptor for the new socket is returned.  On error, -1  is  returned
	]
	<>2>>int bind(int sockfd, const struct sockaddr *addr, socklen_t	addrlen);
	[
		NAME
		     bind -- assign a local protocol address to	a socket
		DESCRIPTION
		     The bind()	system call assigns(分配) the local protocol address to a socket.
		     When a socket is created with socket() it exists in an address family
		     space but has no protocol address assigned(指定).
		     The bind() system call requests that addr be assigned to the socket.
		RETURN VALUES
     		 The bind()	function returns the value 0 if	successful;-1 is error
	]
	<x>3>>int listen(int socket, int backlog);
	[
		Name:
		listen - listen for socket connections and limit the queue of incoming connections
		Description:
		The listen() function shall(将会) mark(标记) a connection-mode socket, specified by the socket argument, 
		as accepting(统一) connections.
		
		backlog = 监听个数
		
		Return Value:
		successful completions, listen() shall return 0 , error return -1 ; 
	]
	<>4>>int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
	[
		NAME:
		       accept, - accept a connection on a socket
		DESCRIPTION:
        The accept() system call is used with connection-based socket types(SOCK_STREAM)
        It extracts(提取) the first connection request on the queue of pending connections for the listening socket
        
        sockfd:
        	The argument sockfd is a socket that has been created with socket(2),
        	bound to a local address with bind(2), and is listening for
       		connections after a listen(2).
		addr :
        	The argument addr is a pointer to a sockaddr structure.
        addrlen:
	       The addrlen argument is a value-result argument: the caller must
	       initialize it to contain the size of the structure pointed to by addr
	    
		RETURN VALUE:
	       On success, these system calls return a new file descriptor for the
	       accepted socket ,error return -1 ;
	]
	<>5>>,,关闭
		ssize_t read(int fd, void *buf, size_t count);
	    ssize_t write(int fd, const void *buf, size_t count);
	    int close(int fd); [-1-->error ; 0--> success]
	<>6>>int connect(int socket, const struct sockaddr *address, socklen_t address_len); [return:-1-->error;0-->success]
	[
		Name:
		connect - connect a socket
		Description
			The connect() function shall attempt to make a connection on a socket. 
		
		The function takes the following arguments:
		socket
			Specifies(指定) the file descriptor associated(联系) with the socket.
		address
			Points to a sockaddr structure containing the peer address. 
			The length and format(格式) of the address depend on the address family of the socket.
		address_len
			Specifies the length of the sockaddr structure pointed to by the address argument.
	]
]
//================================================================================================================================
[4] >>UDP编程
[
	-->>UDP编程流程
	----------------------------------------------------------------------
							[服务器端] 			[客户端]
							  socket     		 socket
							    ↓				   |
							   bind				   |
								↓				   ↓
						     recvfrom<--request--sendto
								↓				   ↓
						 	  sendto -response->recvfrom
								↓				   ↓
							  close              close
	----------------------------------------------------------------------
	-->>UDP 系统调用接口
	[
		-->>UDP发送数据没有实际的发送缓冲区,所以UDP协议中不存在发送缓冲区满的情况,永远都不会阻塞。
		<>1>>  int socket(int domain, int type, int protocol);
		[
			DESCRIPTION:
			socket() creates an endpoint(末尾节点) for communication and returns a file descriptor that refers to 
			that endpoint
	
			The domain argument specifies a communication domain(区域)
				 AF_INET      IPv4 Internet protocols     
				 AF_INET6     IPv6 Internet protocols  
				 AF_LOCAL     Synonym for AF_UNIX
				
			 Currently(一般) defined types are:
				SOCK_STREAM
				SOCK_DGRAM
				SOCK_RAW
			 protocol(协议):
				UDP与TCP编程时写0
		
			 On success, a file descriptor for the new socket is returned,error return -1;
		]
		<>2>> int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
		[
				NAME
     				bind -- assign(分配) a local protocol address to	a socket
    			DESCRIPTION
				     The bind()	system call assigns the	local protocol address to a socket.
				     When a socket is created with socket(2) it	exists in an address family
				     space but has no protocol address assigned.  The bind() system call re-
				     quests that addr be assigned to the socket.
				The bind()	function returns the value 0 if	successful;-1 is error
		]
		<x>3>>ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,struct sockaddr *src_addr, socklen_t *addrlen);
		[
			NAME         
       			recv, recvfrom, recvmsg - receive a message from a socket
			recvfrom()
		       recvfrom() places the received message into the buffer buf.  The
		       caller must specify the size of the buffer in len.
		   flags == 操作方式(一般设置为0)
		   return:
		    These calls return the number of bytes received, or -1 if an error occurred.  
		    The return  value will be 0 when the peer(盯着) has performed an orderly(有序) shutdown.
		]
		<x>4>>ssizex_t sendto(int sockfd, const void *buf, size_t len, int flags,const struct sockaddr *dest_addr, socklen_t addrlen);
		[
			Description
				The system calls send(), sendto(), and sendmsg() are used to transmit a message to another socket.
			The argument sockfd is the file descriptor of the sending socket.
			the message is found in buf and has length len
			flags == 操作方式(一般设置为0)
			return:
			 On  success,  these  calls return the number of characters sent.  On error, -1 is returned, and
       errno is set appropriately.
		]
		<>5>>  int close(int fd);
]
//================================================================================================================================
[5] >> 并发服务器?
<>-->>并发 = 多个客户端接入
<>-->>并发服务器 = 并发多进程服务器 + 并发多线程服务器
//================================================================================================================================
[6] >> IO多路复用?
[
	<>-->>linux中每个进程默认情况下,最多可以打开2*10=1024个文件,最多有1024个文件描述符
	<>-->>文件描述符
	[
		1.非负整数
		2.从最小可用的数字来分配
		3.每个进程启动时默认打开0,1,2三个文件描述符
	]
	-->>Linux下的4种IO模型
	[
		 <>阻塞IO
		 [
			当没有从缓冲区读到数据,当前进程就休眠,当缓冲区内有数据后,进程被唤醒,读取缓冲区的数据
			例:读操作中的read、 recvfrom
			例:写操作中的 write、send
			例:socket中的:accept、 connect
		 ]
		 <>非阻塞IO
		 [
		 	1:IO操作不能够立即完成,请马上返回一个错误给我
			2:使用了非阻塞模式,需要使用循环不停地测试文件描述符是否可以操作,我们称之为polling
		 ]
		 <x>复用IO
		 [
			当文件描述符集合有一个描述符准备就绪时,可以进行IO操作;
			<x>-->> int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);
			[
			NAME        
			       select - synchronous I/O multiplexing
			    DESCRIPTION         top
			       select() allows a program to monitor(监听) multiple(许多) file descriptors,
			       waiting until one or more of the file descriptors become "ready" for
			       some class of I/O operation (e.g.(例如), input possible). 
			       A file descriptor is considered ready if it is possible to perform a
			       corresponding I/O operation (e.g., read(2), or a sufficiently small
			       write(2)) without blocking.
			    nfds: //监听的文件描述符加1
			       This argument should be set to the highest-numbered file descriptor in any of the three sets, plus() 1.
			    readfds:
			    	The file descriptors in this set are watched to see if they are ready for reading
			    	 After select() has returned, readfds will be cleared of all
			    writefds:
			    	 The file descriptors in this set are watched to see if they are ready for writing
			    	 After select() has returned, writefds will be cleared of all
			    exceptfds:
			    	 The file descriptors in this set are watched for "exceptional(异常) conditions(条件)".
			    timeout
		              The timeout argument is a timeval structure (shown below) that
		              specifies the interval that select() should block waiting for
		              a file descriptor to become ready.  The call will block until
			          either:
			              • a file descriptor becomes ready;
			              • the call is interrupted by a signal handler; or
			              • the timeout expires.
				RETURN VALUE         top
				       On success, select() and pselect() return the number of file
				       descriptors contained in the three returned descriptor sets (that is,
				       the total number of bits that are set in readfds, writefds,
				       exceptfds).  -1 is error;
			   ------------------------------------------------------------------------
			   							-->>select()与FD宏的使用流程		
		 						socket()							socket()
		  						   ↓								   ↓
								 bind()								 bind()
								   ↓								   ↓	
								FD_ZERO()  //清空监听集合	        listen()   //监听
								   ↓						           ↓	
								FD_SET()   //socketid加入监听集合    FD_ZERO()  //清空监听集合
								   ↓								   ↓
								select()   //建立复用监听		    FD_SET()   //socketid加入监听集合
								   ↓								   ↓
							   FD_ISSET()  //确认一次				select()   //建立复用监听
								   ↓								   ↓
						recvfrom()     sendto()					   FD_ISSET()  //确认一次
								   ↓								   ↓
								 close()							accept()
								   ↓								   ↓
								 FD_CLR()  //清除监听集合中的socketid FD_SET()close()FD_CLR() 
				------------------------------------------------------------------------
			   -->>select种set集合需要用到的参数
			   <x>void FD_CLR(int fd, fd_set *set);
			   [
					This macro removes the file descriptor fd from set.
			   ]
		       <x>int  FD_ISSET(int fd, fd_set *set);
		 	   [
					  After calling select(), the FD_ISSET()
		              macro can be used to test if a file descriptor is still
		              present in a set.  FD_ISSET() returns nonzero if the file
		              descriptor fd is present in set, and zero if it is not.
			   ]
		       <x>void FD_SET(int fd, fd_set *set);
		       [
					 This macro() adds the file descriptor fd to set.
			   ]
		       <x>void FD_ZERO(fd_set *set);
		       [
					  This macro() clears set.
					  It should be employed(使用) as the first step in initializing a file descriptor set.
			   ]
			]
		 ]
		 信号驱动IO
		 [
			进程要定义一个信号处理程序
		 ]
	]
]
//================================================================================================================================
[7] >> TCP,IP协议
-->>计算机网络的知识点
-->><<计算机网络一书>>
//================================================================================================================================



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值