【卷二】网络二—TCP服务器与客户端

经过上回简单地介绍,大家对服务器多少应该清楚一些了吧!还记得TCP: (Transmission Control Protocol)

传输控制协议? 还记得IP: (Internet Protocol)因特网协议, IPv4, IPv6? 如有不清楚,还请回去补充下概念......

传送门: http://www.cnblogs.com/Ruby517/p/5808224.html

参考: 《Python核心编程(3rd)》 P51 ~ P57

-----------------------------------------------------------------------------------------------------------------

名称                                  描述                            
                                                
服务器套接字方法                                                
s.bind()                             将地址(主机名,端口号对)绑定到套接字上                            
s.listen()                            设置并启动TCP监听器                            
s.accept()                          被动接受TCP客户端连接,一直等待直到连接到达(阻塞)                            
                                                
客户端套接字方法                                                
s.connect()                        主动发起TCP服务器连接                            
                                                
普通的套接字方法                                                
s.recv()                             接收TCP消息                            
s.send()                            发送TCP消息

创建TCP服务器

TCP是控制面向连接的套接字的协议,通常和IP一起用,搭建这样一个类型的服务器有何妙用呢?其实我也不知道!

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXMAAADKCAIAAADYajfMAAAcGUlEQVR4nO2df2xVx5XHR11tpKrdVqk2UbtSq80fQLYBtTGt1EhRpZIQ2oQQ2BKxgaVp86NSS5uWbIgNZsHbKmnY8ivBbjCJA7YxOMT8SEIgYLAfhGAc8yOY3wRoICSlAUKIDZFClrd/XHwZ35k5c+6989671+/70ZF179wzZ87Mm/N91+a9i8hms98DAACniCFDhqxbt+69997LAgCAI8TJkyfnr34bBoPBHJqoXr0bBoPB3Jqofm03DAaDuTWxYE0nDAaDuTXx3No9MBgM5tZEzet7YTAYzK2JF9bthcFgMLcmFq7fD4PBYG4NygKDwdwblAUGg7k3Udt8AAaDwdwalAUGg7k3KAsMBnNvUBYYDObeRN2Gg3m2P//6ZiFuHldL+Tx2l+j363V1Gw7WbVjwIzHmMb/jXQskt3XjbrpyqVfwXj4He/uLHz3Zu7F2Sj+hNAZtwY+ElaszkpLXzggG6/tmU5bm/XXN+9dv3r527euErd+8va55vz5C7ZR+9rK8UpmP3cVyFUJ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XdqXIJEXfygXbObb+2XKP5M7sQs2Y6RE5DS9g01JiBvupPkGQSpyzqE7atTJw4cfToe0ePvjdCen0eYjEJyVD7mpSFM2hMIdDqXZyAIA8k7ntDEfbN1q1bKyurKiujpAcAyAWpfIqCTNNkcbT2es+aJuOtDIBEkD5lKS2dsmHjVt8CyiJfKi2d4nZoAAATKAsAwD3pU5by8nLvryqe/XG8kE2+VF5e7nZoAACT9CkLACD5QFkAAO6BsgAA3ANlAQC4B8oCAHAPlAUA4B4oCwDAPVAWAIB7oCwAAPdAWQAA7oGyAADcA2UBALgHygIAcA+UBQDgnv8HbXp44RAukvYAAAAASUVORK5CYII=" alt="" />

下面是创建TCP服务器的一般伪代码(就是掺杂了很多中文的代码), 由于只是个框架,所以直接按执行只会报错 !

 # coding: utf-8

 from socket import *             # 导入socket模块的所有特性

 ss = socket()                    # ss: (server socket)服务器表接字,创接服务器表接字
ss.bind() # 套接字与本地地址绑定
ss.listen() # 开启TCP监听器 while True: # 服务器无限循环
cs = ss.accept() # cs: (client-side)客户端, 被动接受客户端连接
while True: # 通信循环
cs.recv()/cs.send() # 对话 (接收/发送)
cs.close() # 关闭客户端套接字
ss.close() # 关闭服务器套接字

点我

所有套接字都是通过使用socket.socket()函数来创建的。因为服务器需要占用一个端口并等待

客户端的请求, 所以它们必须绑定一个本地地址。因为TCP是一种面向连接的通信系统, 所以在TCP

服务器开始之前,必须安装一些基础设施。特别地,TCP服务器必须监听传入的连接。

默认情况下,accept()是闭塞的,意味着执行将会停止,直到一个连接到达。这也就是说,服务

器只能无可奈何地等待它的顾客,客户端联系它。

一旦服务器接受了一个连接,就会返回(利用accept())一个独立的客户端套接字,用来与即将

到来的消息进行交换。使用新的套接字类似于接线员将客户的电话切换给客服代表。当一个客户电话最

后接进来时,主要的总机接线员会接到这个电话,并使用另一条线路将这个电话转接给合适的人  (如客

服) 来处理客户的需求。

这将能够空出主线(旧的,原始的套接字),以便接线员可以继续等待新的电话(客户请求),

而此时客户及其连接的客服代表能够进行他们自己的谈话。同样地,当一个传入的请求到达时, 服务

器会创建一个新的通信端口来直接来客户端进行通信,再次空出主要端口, 以使其能够接受新的客户

端连接!

TCP时间戳服务器

【时间戳指的是ctime()输出的内容】

注意,格式化字符串%s无法传递元组,所以要先用str()函数将元组转化为字符串

 # coding: utf-8

 from socket import *
from time import ctime # Host: 主机, Host变量是空白的,表示它可以使用任何可用的地址
# Port: 端口, 端口号范围为0~65535 (尽管小于1024的端口号都预留给了系统)
# Buffer Size: 缓冲区大小, 这里设置为1024B, 即1KB
# listen()方法/函数的参数是在连接被转接或拒绝之前,请求的最多次数 Host = ""
Port = 21568
Bufsiz = 1024
Addr = (Host, Port) tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(Addr)
tcpSerSock.listen(5) while True:
print "Waiting for connection..."
tcpCliSock, addr = tcpSerSock.accept()
# %s 可以代替str(字符串) int(整数)、float(浮点数)、list(列表)、dict(字典), 就是不能代替tuple(元组)
print "...Connected from: %s" % str(addr) while True:
# 从Buffer: 缓冲区, 取出客户端发来的数据data进行加工
data = tcpCliSock.recv(Bufsiz)
# 如果data是空白值,那么它的布尔值即为假,not data即为真,那么执行break语句,跳出循环
if not data:
break
# ctime()表示当前的系统时间,即时间戳, 再把处理好的数据发回缓冲区,供客户端利用
tcpCliSock.send("[%s] %s" % (ctime(), data))
# 关闭客户端套接字
tcpCliSock.close() tcpSerSock.close()

点我

一旦进入到服务器的无限循环中,我们就只有被动等待客户端地连接。当一个连接请求出现时,

我们进入到对话循环中(第二个while语句范围), 在该循环中等待客户端发送的信息。 如果信息是空

白的, 这意味着客户端已经退出,所以此时将跳出对话循环,关闭当前客户端连接,然后等待另一个

客户端连接 (只退出第二个while对话循环,第一个服务器循环还在运行)!

错误1, 如果出现这种报错, 说明port: 端口号已经被其他程序占用, 也许你已经打开过的服

务器没有关掉就又新打开一个了!  此时关掉开着的服务器或者换一个端口号就行了。 (有效的端口号

范围是0~65535,小于1024的端口号预留给了系统!)

查看端口使用:  >-|->> http://jingyan.baidu.com/article/3c48dd34491d47e10be358b8.html

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApcAAAAWCAIAAAAafSCSAAAGt0lEQVR4nO1XUbbbSgjz/lc1S3t/7nRAQoCd5PWgj3viAYQQE6e9rsFgMBgMBoPBYKBgrSVG11rHY8gc5ijo8KRqUXKh+8cGJwlNDW53hfMpD99gOEj6V/SLO+K0j9zAwWDwZbjvLAuRhPyiK90LCXqvVCFiy56H3ZuZ5Z/MrAC9XF/ZG3pSUAT8Yzsqf1UHg8GPgr/Osm9k9x3BW2ffsx2pb7zlURelUH91okxRTyG056S661tzXdqvkO5PtkRXLq6ycC7mZE3g5cv7hpaVI8eamlvot//yAD8G9ELUX5o/CP5SI7OQaDh+mVnBg1/FBX5QUYssv0uyvBeTWJvVo9xkotNmigxIakGzGNplp8r1LbhT7IdZefql5coPDfqKOVtWj15+ZyqOWVoyUYiykrfQ7/1N9b8HtOzUhfgdcKl8HCVqPyu1obDagOGCUML9N9R8RW8oZRyrKizZ9YsdiRtLeBuKwvTyVPeyKtciJIOAMBAZYuHhgD1x8/nq9zT9gn1lR0cm2Yh7yHcUtuaydeVPot/4a9J/EsiN/6lL5NIf56nHy/v6icxWQ3m6i/4iuiOLbKR2RSi0UExASwzLkfO8dVkSYjg6crtSayWFKMHeWJ2BZCrd7Wj7Eu1e0KYOzWE50uM+vrqjPY2T67dLb928P39McVNrUXsd3buFmJdBMENSlUvOox3mPhAb79KZ10YRf3lSt5B4G0Z5clgbnofYW7vnNmSjLng7lwQ9hjx67aL/L+HlIS1PQ01RR5RzjOA25bWh5tBDJJv7gEZOdUc5x5iWH3l1mYmQmUgG8pmYH9byz66M4wSF3HLxUE8I+qLhm9HDR6ug05cjVIUSeLTD3Adi410684bj9Id1q1AjJZpKJpoL44jehsxuVBFjJz206YVo0aiQhEh3m7M3UrzinGGO7RjyhCa7W1YGUSwSa/mjW+5u8DAqK4yc6258bEdc3q7H5eGcRK2YwEx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umXq9Kd/QL4f6W6K9p8YcBtVO6K3pE88PuRJXY9O0dde68uKyUThvKyhv8ItaGb2v5l/Ggz4tCLN8fOwKyVbUutiNis+duZiimv6/CmngJquUiF/gNUKYL3bY8orEkIZV2AdOUQlTLScLtpNxw+Y8Pl7asLB4nHHwNhas/KOB3fH5KQ/n19FK+LeeTKvxfX5YI5VenQ16uHWRB9vhsl1nrYDAYDAaDwWAwGAwGg8FgMBgMBn38B9gJ7vzTR4sMAAAAAElFTkSuQmCC" alt="" />

错误2, bad  descriptor, 一定是因为你把客户端的结束语句放在对话循环里面了(第二个while语句范围)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlUAAAAWCAIAAABmN/4OAAADQklEQVR4nO2ZUY7kIAxE+/6n4mj7MdIqAlyUy2QmHep9bWJjVxlIj7SfjzHGPInWWmvtr1UYY4wxxhhjjDHGGGOMMcYYQ+D/3LriaZjXUj/cvh5X2oXofZTzTLBazcveOQgVcPe9ZgUedTZkMY9yYcyEP/n6vJjoEzn56fuGuS0Fa172jqLeXViYl5ngK87Gkne4MG+mfkZ9yq9E0/jGKXXfevDp136BKhX2dr+13TNb/ALvcGGeAv7zU4t2b6bLQeU2sMXR/zfT4jhaqVwnqoa7VPyO0ah+1imYXsqdUHm5O9dQvfu0703LcdnIETmNVLRrN+aAsuNa0HSZkFJlTgSfJzl6fRTO8fKUa47G0KgztZysXCeqhrtU/C7tyGajRll3TH73JuuI7I4n+Yk/ylg8o1kQRlaWo5Vp4IW3qjLHER2g678rj9MWy7WRNt7RUuS0MhkVKhdNgYVtIIritSAqPJJ2gPJrGlmTLzutX3E07TuuxQXBkqIq+TGSR5bCOmW/UeUtqsxB4APB3IFlZXBSs28YGM3Yb2UaS8Hy9cOTjKZ9t19GYcS1O3Yn1FwOZEzY5UhOmEYrqio7OJ0eqQGvxUUYv1HxiiNzKG2gC43JUXRalmzKn/KsnaxmPspXrhPV3+sIR7HfygQ0d0z+qJk/G/XuqYRpFGvO6uGnMU2oGCHTmF2I6jN+l6rMuXQHvXKeopszjU7TtPOKKzOa+ehv3q6o/l5HOIr9ViaguWPyec11R8t8YTtagKZn9EtW7qKCETKN2YWoPuN3qcocBHlD5EfcBR9H7bwy11jryPu9A/7O3xfldx/Dr81OldGMo5ojUq2wHYKMaG3KYGVWpGDG71RkVH+LKnMWbWBLFD8uK48JkfKUI3wHljdErpxN4xeOkq6ZFb9jdGoZ7xEuvlxbL4sFMwnZ7qSwXVFGEu93i6qlyJSksdRNqsyhgKMmR/k34FwyrVML8R1gbohWOZvGL2wBTLtsVNhBALM2WzmaA0iIHMndBWEp2byebu1UIa6sRZc6gd9OKjPPXaqMMQ/lzNt7puuT8Y4bY3rwX+Lv4zS/5gfvuDGm57Tfg9P8mh+848YYY07Ev3/GGGOMMXfxD6Y/icQnnaNPAAAAAElFTkSuQmCC" alt="" />

单独运行服务器的话,由于此时尚没有客户端的请求,服务器输出的只有:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAewAAAAaCAIAAAD69PiHAAAB8ElEQVR4nO3XUW7EIAxFUfa/KpbWnyhKY3i2gZmQ9p6vNICNibCmpQAAAAB4sVprrfX7a7EDviDwJHsDB+6kWOJGe6oF1N++v4G32PYLAihlURNPxd9BNZ7e0b44H2Br/7mJP72Ld+CsgK25TVz/YtU/ZvUPXjfsdY4OHii0HV+MipPRZ+LmzVakT2P+rMRoNeJhhysCkHC7TsVr4qlrPLzWLhRrU12gGTlYr7s2mzo4mt1wPPLC0VS97p4BRJ33xz7o+e5Ld8gNeL5P/ak120c20UDHmanoqT+vL93SFtYLIOG8PPbhOuGmF0TEdzegX9o7n8oS3FUk71iv0Qtn6l07mt15b8KnvyCAw3l57EPpdPDIjY0MiQmfbgG9+fMtL5txLG92tPcFIxXt+QUBHM7LYx9K+LKJG7hnC+jNz+adzziWNztqRXYVnPPIFwRwOC+PfSh7N/HenxG9+TryTKOJRI7nzY7qr6Mnu1U3J8xUBCChdkRGmxN0fJ36NmQn621nqw4eSGRVPOny0xg4q/iu7IQvVKRfLhkF/pRIR9C3UbSA2xwdubkld2PN0XjJ2Q3Hs8QjZ/Ne36TOKpU3uDGxfKwi/XLJKIC9cGMB4K30r0gAwHbcf+QBAPuigwMAAAAAAAAAAAAAAAAAAABAww8M1JqqowgwMAAAAABJRU5ErkJggg==" alt="" />

----------------------------------------------------------------------------------------------------------------

TCP时间戳客户端

【注意】 由于客户端的connect()函数是主动发起连接的,所以若是先启动客户端而不是服务器的话,

没有服务器的响应,那么将会出现如下错误:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkwAAAAXCAIAAAB20IeJAAACr0lEQVR4nO3b3W7UMBCG4VwkFQih/p1tdisQotDL86VxsGKVYvvLeGazdaz3OUoyznjGAbuVYPo0HycAAIbEIQcAGBaHHABgWBxyAIBh3c2nTfOnlFJKH5thJGmh9rw2pluiVN1LJBqZF8Bu3B045PZksEPOfkpZWvZFHYUB2I3Ph5dN83PIXZc+D25dTZg4S/57fsVbPW9eg7c5AB34cvh+udZ/833R4uZiz5wy5s5MVRWT62gkc1wtm54l0m8ereV3d1p8UaytI2qft2kAgN59nX+cL1JmOcwdXd16IvNqq1XVBuhoJHNcLZueJdLvajvxZotv1SbyRe3zNg0A0Ltv88/JtmVEbotTrL6bV2JnLLKY2Rh1ZA42JV5MmVpUvyuijttIU5eH+UVrtFbPaqmOXgD05f74a2rfC4obinhXb6CtTywsNet+I6uxWrB799QrWVvtrfu1VCgUX7k8zC+MUbEgxlLdnwlALx6Or+cLsSm0bnP5SL2B6v3It9HozJaa7VF75rha/ut2pKO6X8cKFF+JzFv83JYpWgcA6N3j6Xf+cPVIsG/r6T0dLQ7zbTQ6s6Vme9SeOa6W/7od6aju17EC4s+Gb15jVaulbv01AWzu6fRniv1cbLnNr4uDi3wbjc6sc1qiln63UMsf78getX99o+IrlnX23ep5mwYA6N3T6e18kTLLYe6ovl3NnA/IW9DPLVUZE8Yztw6zv5iXtBwZ6TePFlvW30hzfMFgVM9rHwCgd8//Drnp/b6Qj/RF7U/EhuLequxV2aORzK3D7C+mCst0rVHHF9SCn94X1fMaBwDo3fPL2/ogdINt95ZYbWD3lr/JoX/B35lgxDoDgzj/wxPsBZvvbbDOwCA45AAAw3o8Fv6fHAAAI3iYOeQAAIO6n18/ugQAADbxF8ucnFqBn3wcAAAAAElFTkSuQmCC" alt="" />

 # coding: utf-8

 from socket import *

 # Host:主机, Port:端口, 指服务器的主机名和端口号, 因为在同一个计算机进行测试(本例中)
# 所以Host包含本地主机名 (如果你的服务器运行在另一台主机上,那么需要进行相应修改)。端
# 口号Port 应该与服务器设置的完全相同(否则将无法进行通信)! Host = "localhost"
Port = 21568
Bufsiz = 1024
Addr = (Host, Port) tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(Addr) while True:
data = raw_input("> ")
# 如果data是空白值,那么BOOL(data)布尔值将会是False,not data即是True,执行break语
# 句,跳出while循环
if not data:
break
# 把数据data发送到Buffer(缓冲区), 供服务器提取, 进行加工
tcpCliSock.send(data)
# 把服务器处理好的数据从Buffer(缓冲区), 取出来, 并打印出来!
data = tcpCliSock.recv(Bufsiz) print data

展开

客户端的主机名和端口号必须与服务器的一致,否则,将无法进行通信!  因为此处是在同一台计算机上进行测

试,所以在Host包含本地主机名(localhost), 如果你的服务器运行在另一台主机上,那么需要进行相应地修改。

如果出现这种错误,说明服务器和客户端没有连接上,可能是还没启动服务器就先启动客户端了,也可能是两者的

端口或地址对不上 (比如忘记写本地主机名称'localhost' !!!)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfAAAAAVCAIAAAA2MCs0AAACIUlEQVR4nO2WS3YDIQwEff9TcbTs/JKAmpbE2BOnamVooQ+M9Px4AAAACMYYY4z3evgkxjei/cjmtohUdS0dtRMX4J/CQD/Lhw10fyI7JdfUQmIA/xQG+ln07Ht1Nm3E3Py1f3Cp4845VIsDeCv6K6+py0byPY+JIxU9d5bOtdrx3CfypqN06p3VyH+50uVBcbcF1Y+bMgC4KWPiiLpts07cckWzNOeZOm567hN501E69W7L6Re7PBUFqql+3JQBwE3Ztkdn+dhN8+VyziRVjpPk0rOpFjw3ixIHx0Sk6rNCLSw7RT035x9ZNcpnm2qhFoBbkPrul80jzuphkd1xcHLW9XZuY5tweVLom4xu++p6nQwFyyPPzfmHqYoLMVMtPxPAmxENkG3ppVszqI7VKSebs6/6nvtE/s9WpFVdb+EGlkc6cZfP7YTIGgD8Abbjzx9h4ydaXZrVmkp7dnL2Vd9zn8j/2Yq0qust3ID4Nmpxzay2qV79mgBXsW2PznL+vTSOEis0lfasfTqqU+8VRP77Ffmq//omyyPOPdeWOm7KAOCmjIkjql5uPc8GUeapinSjbtu47Dlr5h+cU/pu2al3Vpcl6zfSFF6wqeq4vgHAfXG+/qzq74jmKbeln5WvdjxnzfyDI8AJl1ULL6hpPn1N1XFNAwCAOoyYV8JtA8CFNP8Lgwn3DACXw6B5DdwzAAAAAAAAAAAAAAAAAADAXfkCUWp5ed4ZnkUAAAAASUVORK5CYII=" alt="" />

客户端的输出,可以看出,它先把自行输入的数据发送到缓冲区,然后服务器将数据从缓冲区取出来加工 (还是

通过服务器代码里的tcpCliSock.recv()方法),最后输出的是经过服务器处理过的数据,即我们输入的数据加

上时间戳!

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY8AAABSCAIAAAAjL8/1AAAHFUlEQVR4nO2cXZbzKAxEvf9VsbR5mJkcGpBUSDiddu59+I7NT1EIRXGSnrmuQ7TWWmv97SllAICTDOWJagUAn4j4YEUJA4ASQ63JKcyCFpWFAODbKdaRfrr1kEWdAoAzpAuW8zGQhykAuIVEcfl3/PLBar4FADjGVsEKixTVCgBuRC9YerWibAHAYXLfNCnPVhQsADhG5Yv28Hq+BQDIUPn9Tq9WFCwAKFH/e6vhwrqlWgFAnnoRGf6OYWj3WwAA3odVg6hNAPBZLJ+h+NwHAB8HVQkAAAAAAAAAns3wd4V8GQIAn8hQnihVz4F3HngY/Cdlv0nrsNqtMaJ4xV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...Connected from..."的消息。 当继续接收

"服务"时,服务器会等待新客户端的连接!

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAagAAABFCAIAAACQfeV/AAAGIklEQVR4nO2aSZalMAwEuf+pOFrveL8RTqcsM1bECqxZlv2oYVkAAAAAACzWdV3X9XpbeALsILyPOLUDcyxMut7uOjbr/1yfwFt47A4CjDPl4kv5fwJr4O6Mngv9gQ/yly++u7N4B/QKPkj34tNfRvqjSX9Ydd3+6mjnRqHH/oVUdEb3pBs3W5HuRr1XQroGfLfDFQGczm4El97Flxr9YdtoKGxTJ+fQs1lv1zYb2pRmE/Y9T5Sm6u3mDHAu28zFB63fXeyKug639dSr5vDIZQMNnNJKRXe9/i52S5tYL8DpbAMXH34VdrScCP/dBPRiPCepKGZWTtyx86kNK/XOlWYzbymcvYMAJbaBiw9L49ZzptwRCYWzj01Lv35NZCOOxc1KWzvoVPTMHQQosQ1cfFjsARVT+8xj09LPxq1HHIublUacrEydW3YQoMQ2cPFhefbF13p1aOlrz5XD6Xj242alene0crfqQ4VKRQCnszZwpIcK2r8OvRNFZZ12tmqzIY6VH3R6NwZ65WcVFS6oSC9OkQJYp0hPsDg2Ox3t+TClbmKHUr/kbMJ+FN9zNu7vSqpXqbhmYsJ8rCK9OEUK8AWYcgD4W+ivFQCAj9D9IQsA4Gtw6wEAAAAAAAAAAABMg9+0wo6/PhJv/BPErGwH/LzxjzYn/Zux6EP8m36qV5UOF3en0qthKp2s204u5hVUpvNGZqVaOZBvaVc3z5aCLtM/jdleVZo8y1br+D6zcbOdrO/CGRU9nV0vBmblLmblOXY26nGvpJtwq6juafEnx+9bZSaL8yzq3emYDlNBD191ArN2YW5FL8DZxdY0bCsDUu25K10DY56XeRdfrLfVk26vlqmDmC1w2INQS+Ug+nOqbcWkQirnnahia4q+SbfgNSBEvlR7vli6Zg5Vy1ZLBzppZmWmfbYHkXM2gaif3aNK9DGTInGQumr+StdtV/pBnHas4Wzf+6ozr3vudmNHlI5ldfjglOykn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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAPAs/gHQ7is4kD9C8gAAAABJRU5ErkJggg==" alt="" />

【127.0.0.1是回送地址,指本地机,一向用来测试使用!】

-------------------------------------------------------------------------------------------------------------

客户端改进

上面的代码有个问题,就是当服务器与客户端还是通话的时候,我们若是按 "Ctrl + C" 引发服务器异常从而关闭,

那么此时客户端的显示是这样的,输出空白,个人猜测是由于服务器在执行tcpSerSock.recv()时引发异常,但两

者好歹对话过,所以系统返回了空白符给客户端,

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa4AAAArCAIAAACvnfsSAAABDUlEQVR4nO3dQQ7CIBAFUO9/qh7NhYnRFlGYmVTxvZWafuLqhykLLpcVbc/O/jsAAH8usiOzmwNWEBlODbbAr9qV1+3rh40WyQKcrHNA8bbOIlmAZMFJduhzVhYg33QbNmvruFRz/UgWoMRc4xzrrLMHbE7Bc1mAKhONs6uzZvz+e78Kh7IAhUbbsPOO764iC1BrqINe1Vl1FqBQyq6wefSRmwWoMveu8DjPbi25WYASc6Xzdrs39NjnWYB805svVQgsIn0IjdSZKgQWoQoBQkcfjk2ARUTKS/EBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAsKLHezun7y8eDb56uPoC5azFXXYK3+AKSJak0LTvLYAAAAAASUVORK5CYII=" alt="" />

可是第二次就出错了,因为此时服务器已关,也就是没有TCP连接了,所以也就没有了交互,系统怎么返回信息给

你所以这回连空白符都没有(纯属个人猜测)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaEAAABoCAIAAADXWthdAAALDklEQVR4nO2bWZbbvA6Evf9VaWn34Z7TvyIShcJAWpLre7I4AIVBiDvpfD5CCCGEEEIIIcSrOY7j/Pn8GDISumgdTgso+v2WHSHEWi4zJf3qJmbTYZPTQDrFMtJ2hBB35PKupl/y3IybLi4aH9YkzY14zTghHgD5JY4ZRuNscqfV5u9x1jRPzziNOSHWUn/NplOJnDtgxpGjihmdjfzf7DjaNOOE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0czv8pbrWPZ7I8K7ON5EBqZXev2C3Utv8DbFg3EnQnReWG10GFi+EiFMLaffh+kub7lO7m3cGW8iA6A3uvzmdldXU+yD6afKI/aC2yjXZ9hy/X1g4l1B7m3s3eWrTzK9wuR5xaMrTDyVY6BlFz+6lscDlvJQRLh33c5OW44e4y+Oks4nK/GOu9OQcY0wiQou3XWFiaeCS57b5VfcVsOuQxdx7zKdnbMcPcZfPAwYd9HdRAUxxdKv2HWFCSGWoLduJ8q2ELspfmMSJMqzEN9B794elGchhBBCCCGEEEKs43/ujBd3Gmi3tAAAAABJRU5ErkJggg==" alt="" />

所以我们要在客户端代码的 tcpCliSock.recv() 后面这么改...

 data = tcpCliSock.recv(BUFSIZ)

 if not data:
break print data

这样就可以在引发服务器异常,然后客户端收到空白符时直接退出客户端

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAY4AAAAqCAIAAAArnCtnAAADNElEQVR4nO3cUZKrIBCFYbebXWUpLu2+pbw0tIemSQb8v4epMkrTEOlSx5nj/X6/Xq9jU+f/fp0OgKi9SxWATUwtVdcLGS5qAMQllip7k1WUKqoVgKDcqyqnVNlNAFDNK1XOZRQ1C0Cfa6nKvUc727K6APAUxVVVYikpQlGhAMTZG8BAtbIXTdefn2MGUwXwXNVnVeFrq6JCcQ8IIEfrsXqsoNhSVewCgAjnN4CxO8HWJqUKQNyMq6rqJqUKQNyMZ1XH5U6QZ1UAEqT8BvDa9uBZFYB0ue9VFc0pVQBy5L6tLpYqyhaAPrP/XJnHVQASzPt/VdQjAGkoVQAWwD8sBrAAShWABVCqACyAUgVgAZQqAAv4lCrlvaeRt6JibWf/GtEf6ex+q70476D5WaW8v+Y0Gfn2UxLAozml6rx7n1PvJdz2Vwtjar+983x+pVT5TUa+/S5/6vTAH1KUqs/n1UXS2vSNtLWJ5fJX5rwe9dmofi/KkeHEbJDBbzCQxrzjsapWqSo+GTk7U5aQv6ua3u0npyFGts31rGIDjB0cUA2iz23vXBV7A5FbOWM3Yqk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