前面第五篇(一)中的一个Socket例子其实就是单线程的,即Server端一次只能接受来自一个Client端的连接,为了更好的说明socket单线程和阻塞模式,下面对前面的例子做修改。
1.单线程+阻塞+交互式
前面的例子是单线程阻塞和非交互式的,现在改写为交互式的,即不会执行一次就结束,希望达到的效果是,发送的数据由User输入,然后Server端进行接收。
Server端:与上个例子一样,并没有什么变化
import socket #导入socket类 HOST =‘‘ #定义侦听本地地址口(多个IP地址情况下),这里表示侦听所有,也可以写成0.0.0.0 PORT = 50007 #Server端开放的服务端口 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #选择Socket类型和Socket数据包类型 s.bind((HOST, PORT)) #绑定IP地址和端口 s.listen(1) #定义侦听数开始侦听(实际上并没有效果) conn, addr = s.accept() #定义实例,accept()函数的返回值可以看上面的socket函数说明 print ‘Connected by‘, addr while 1: data = conn.recv(1024) #接受套接字的数据 if not data:break #如果没有数据接收,则断开连接 print ‘revc:‘,data #发送接收到的数据 conn.sendall(data) #发送接收到的数据 conn.close() #关闭套接字
Client端:
import socket HOST = ‘192.168.1.13‘ PORT = 50007 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) while True: user_input = raw_input(‘msg to send:‘).strip() #由User输入要发送的数据 s.sendall(user_input) data = s.recv(1024) print ‘Received‘, repr(data) s.close()
演示:
步骤1:Server端运行服务端程序
[email protected]:/mnt/hgfs/Python/day5$ python server4.py ===>光标在此处处于等待状态
步骤2:Client A端运行客户端程序
[email protected]:/mnt/hgfs/Python/day5$ python client4.py msg to send:The first msg. ===>User输入数据 Received ‘The first msg.‘ ===>Server端返回的数据 msg to send:The second msg. Received ‘The second msg.‘ msg to send:The third msg. Received ‘The third msg.‘ msg to send:I‘m A. Received "I‘m A." msg to send: ===>继续等待User输入数据
步骤3:在Server端中观察现象
[email protected]:/mnt/hgfs/Python/day5/[2]sec_4_ver2(单线程,交互式,阻塞模 一般演示)$ python server4.py Connected by (‘192.168.1.13‘, 52645) revc: The first msg. ===>接收到用户发送的数据 revc: The second msg. revc: The third msg. revc: I‘m A. ===>光标在此处处于等待状态
如果此时有另一个Client B端再连接进来,会有下面的情况:
[email protected]:/mnt/hgfs/Python/day5$ python client4.py msg to send:I‘m B ===>光标在此处处于等待状态
这时如果在Client A端断开连接,则服务端也会关闭套接字,Client B端发送的数据仍然无法被Server端接收。
此时服务端即出现阻塞情况,因为服务端还和Client A处于连接状态,无法接收Client B发送的数据,这也说明了此时的Server端是单线程的。
2.单线程+阻塞+交互式的进阶演示
把上面的例子中的代码再做进一步的修改,以使得阻塞模式的现象更加明显。
Server端:
import socket HOST =‘‘ PORT = 50007 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) while 1: conn, addr = s.accept() #在循环中接受Client端连接的请求 print ‘Connected by‘, addr while True: #再做一个内部的循环 data = conn.recv(1024) print ‘Received‘,data if not data:break conn.sendall(data) conn.close()
Client端:与前面例子的代码一样
import socket HOST = ‘192.168.1.13‘ PORT = 50007 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) while True: user_input = raw_input(‘msg to send:‘).strip() s.sendall(user_input) data = s.recv(1024) print ‘Received‘, repr(data) s.close()
演示:
步骤1:Server端运行服务端程序
[email protected]:/mnt/hgfs/Python/day5$ python server4.py ===>光标在此处处于等待状态
步骤2:Client A端运行客户端程序
[email protected]:/mnt/hgfs/Python/day5$ python client4.py msg to send:Hello! Received ‘Hello!‘ msg to send:I‘m Client A. Received "I‘m Client A." msg to send: ===>继续等待User输入数据
步骤3:在Server端中观察现象
[email protected]:/mnt/hgfs/Python/day5$ python server4.py Connected by (‘192.168.1.13‘, 52647) Received Hello! Received I‘m Client A. ===>光标在此处处于等待状态
如果此时有另一个Client B端再连接进来,会有下面的情况:
[email protected]:/mnt/hgfs/Python/day5$ python client4.py msg to send:I‘m Client B. ===>光标在此处处于等待状态
Server端的状态依然为:
[email protected]:/mnt/hgfs/Python/day5$ python server4.py Connected by (‘192.168.1.13‘, 52647) Received Hello! Received I‘m Client A. ===>光标在此处处于等待状态
这时试图把Client A端断开:
[email protected]:/mnt/hgfs/Python/day5$ python client4.py msg to send:Hello! Received ‘Hello!‘ msg to send:I‘m Client A. Received "I‘m Client A." msg to send:^CTraceback (most recent call last): File "client4.py", line 10, in <module> user_input = raw_input(‘msg to send:‘).strip() KeyboardInterrupt
再看看Server端的情况:
[email protected]:/mnt/hgfs/Python/day5$ python server4.py Connected by (‘192.168.1.13‘, 52647) Received Hello! Received I‘m Client A. Received Connected by (‘192.168.1.13‘, 52648) Received I‘m Client B. ===>成功接收到来自Client B端发送的数据 ===>光标在此处处于等待状态
再看看Client B端的情况:
[email protected]:/mnt/hgfs/Python/day5$ python client4.py msg to send:I‘m Client B. Received "I‘m Client B." msg to send: ===>光标在此处处于等待状态
以上的现象,再根据Server端的程序代码,就可以非常好理解单线程模式和阻塞的细节情况了,在这里是这样的:Server端接受Client A端的连接后,即把接受连接的线程释放,但此时仍然占用接收和发送数据的线程,所以Client B端虽然可以连接上Server端,但数据是无法成功被Server端接收的;当Client A端断开与Server端的连接后,Server端的接收和发送数据的线程立即被释放,之后就可以正常接收来自Client B端发送的数据了。
单线程,即数据的串行发送,会导致阻塞,上面的两个例子就非常好地演示了这个阻塞的过程,如果要解决这个问题,当然在Server端就需要支持多线程,即数据折并发。