# 客户端介绍简单版
# import socket
# #1买手机
# phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM 就是TCP协议
# #2 绑定电话卡
# # phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# # phone.connect(('127.0.1',7771)) #0-65535, 但是1 - 1024 系统占用
# phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# phone.connect(('127.0.1',7771))
# #3 接发消息
# phone.send('hello world'.encode('utf-8'))
# data = phone.recv(1024) #1024最大接收的字节数
# print(data.decode('utf-8'))
# phone.close()
# 服务端介绍简单版
# import socket
# #1买手机
# phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # AF_INET 套接字的网络类型 SOCK_STREAM 就是TCP协议
# #2 绑定电话卡
# phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# # 获取或者设置与某个套接字关联的选 项
# # SOL_SOCKET 是socket 描述选项
# # SO_REUSEADDR是让端口释放后立即就可以被再次使用。
# phone.bind(('127.0.1',7771)) #ip 和端口 绑定0-65535, 但是1 - 1024 系统占用
# #开机
# phone.listen(5) #代表能挂起监听5个
# #4等电话
# print('waiting') #判断此处开始连接
# conn,addr = phone.accept() #链接 地址
# print(conn)
# # print(addr)
# #接发信息
# data = conn.recv(1024) #最大接收的字节数 #1024最大接收的字节数
# conn.send(data.upper()) #返回内容的
# conn.close()
# phone.close()
# ---------------------
# 运用while 循环 服务端
# import socket
# phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
# phone.bind(('192.168.21.24',8888))
# phone.listen(5)
# print('waiting')
# while True: #链接循环
# conn,addr = phone.accept()
# while True: #通信循环
# try:
# # print(conn)
# data = conn.recv(1024) # #1024最大接收的字节数
# conn.send(data.upper())
# except ConnectionRefusedError:
# break
# conn.close
# phone.close()
# 客户端
# import socket
# phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# phone.connect(('192.168.21.24',8888))
# while True:
# msg = input('>>:').strip()
# if not msg: continue
# phone.send(msg.encode('utf-8'))
# data = phone.recv(1024)
# print(data.decode('utf-8')) #解码
# phone.close()


德胜的聊天版本
# #导入模块
# import socket
# #创建服务端
# phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# #绑定客户端
# phone.bind(('192.168.21.24',8088))
# #兼听范围
# phone.listen(5)
# print('等着呢')
# while True:
# conn,addr = phone.accept()
# while True:
# try:
# #收消息,作为服务端先收再处理后发送
# data = conn.recv(1024)
# print(data.decode('utf-8'))
# mag = input('请输入:')
# conn.send(mag.encode('utf-8'))
# except ConnectionResetError:
# break
# conn.close()
# phone.close()