Skip to content

Commit cd2070e

Browse files
committed
add some cases
1 parent bba8d08 commit cd2070e

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

python_socket.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ def server_func(port):
2424

2525
# 4. 接收客户端连接
2626
sock_obj, address = server.accept()
27-
print("客户端来自于:", address)
27+
sock_obj.settimeout(3)
28+
print("客户端:%s,超时时间:%s" % (address, sock_obj.gettimeout()))
2829

2930
while True:
30-
# 5. 接收客户端发送的消息
31-
recv_data = sock_obj.recv(1024).decode("utf-8")
32-
print("客户端端(%s) -> 服务端: %s" % (address, recv_data))
33-
if recv_data == "quit":
34-
break
35-
36-
# 6. 给客户端回复消息
37-
send_data = "received[%s]" % recv_data
38-
sock_obj.send(send_data.encode("utf-8"))
39-
print("服务端 -> 客户端(%s): %s" % (address, send_data))
31+
try:
32+
# 5. 接收客户端发送的消息
33+
recv_data = sock_obj.recv(1024).decode("utf-8")
34+
print("客户端端 -> 服务端: %s" % recv_data)
35+
if recv_data == "quit":
36+
break
37+
38+
# 6. 给客户端回复消息
39+
send_data = "received[%s]" % recv_data
40+
sock_obj.send(send_data.encode("utf-8"))
41+
print("服务端 -> 客户端: %s" % send_data)
42+
except Exception as excep:
43+
print("error: ", excep)
4044

4145
# 7. 关闭socket对象
4246
sock_obj.close()

0 commit comments

Comments
 (0)