网络编程从大的方面说就是对信息的发送到接收,中间传输为物理线路的作用。
它的含义是使用套接字来达到进程间的通信。套接字可以看成是两个网络应用程序进行通信时,各自通信连接中的一个端点。
套接字Socket=(IP地址:端口号)
端口号是区分不同的程序,因为一台电脑只有一个IP地址。
一般的网络系统提供了三种不同类型的套接字,以供用户在设计网络应用程序时根据不同的要求来选择。
- 流式套接字(SOCK-STREAM):提供可靠的、面向连接的双向数据传输服务,实现了数据无差错、无重复的发送。内设流量控制,被传输的数据看作是无记录边界的字节流。(TCP)
- 数据报套接字(SOCK-DGRAM):提供无连接、不可靠的双向数据传输服务。(UDP)
- 原始套接字(SOCK-RAW):该套接字允许对较低层协议(如IP或者ICMP)进行直接访问。
Socket工作流程
Python中提供socket.py标准库,非常底层的接口库。Socket是一种通用的网络编程接口,和网络层次没有一一对应关系。
Functions:
(1)socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)
Create a new socket using the given address family,socket type and protocol number.
- main family:
名称 | 含义 |
---|---|
AF_INET(默认) | IPV4 |
AF_INET6 | IPV6 |
AF_UNIX | Unix Domain Socket,windows没有 |
- type:
名称 | 含义 |
---|---|
SOCK_STREAM(默认) | 流式套接字 |
SOCK-DGRAM | 数据报套接字 |
SOCK-RAW | 原始套接字 |
- protocol number
协议号通常为零,可以省略,或者在地址族为AF_CAN的情况下,协议应为CAN_RAW或CAN_BCM之一。
Socket Objects
socket.bind(address)
Bind the socket to address. The socket must not already be bound. (The format of address depends on the address family )
其中address为元祖,(IP,端口)
socket.listen([backlog])
Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it is lower, it is set to 0); it specifies the number of unaccepted connections that the system will allow before refusing new connections. If not specified, a default reasonable value is chosen.
简单来说,这里的backlog表示socket的”排队个数“
一般情况下,一个进程只有一个主线程(也就是单线程),那么socket允许的最大连接数为: backlog+ 1
如果服务器是多线程,比如上面的代码例子是开了2个线程,那么socket允许的最大连接数就是: backlog+ 2
换句话说:排队的人数(就是那个n) + 正在就餐的人数(服务器正在处理的socket连接数) = 允许接待的总人数(socket允许的最大连接数)
socket.accept()
Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address)
where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.
该方法阻塞等待客户端建立连接。
socket.recv(bufsize[, flags])
Receive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero.
socket.send(bytes[, flags])
Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv()
above. Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. For further information on this topic, consult the Socket Programming HOWTO.
socket.close()
Mark the socket closed. The underlying system resource (e.g. a file descriptor) is also closed when all file objects from makefile()
are closed. Once that happens, all future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed).
Sockets are automatically closed when they are garbage-collected, but it is recommended to close() them explicitly, or to use a with statement around them.
简单例子:
1 | # |
1 | # |
结果:
服务端
客户端