server下的方法:bind() , listen(), accept(), recv(), send(bytes(String)), sendall(),
client的方法:connect(), recv(), send(), sendall()
### 建立通信的过程:创建socket,绑定ip和端口,监听,接收连接socket,用接受到的socket进行通讯。然后才能发送连接数据
server.py
import socket sk = socket.socket() # 创建对象,构造函数里面的参数,一般都不需要参数 # family:AF_INET-服务器之间的通信,AF_UNIX-Unix不同进程之间的通信 type:SOCK_STREAM-TCP连接,SOCK_Dgram-UDP连接 address = (‘127.0.0.1‘,8888) sk.bind(address) sk.listen(3) # 让多少个通信进行等待 # print(sk.accept()) # 等待连接,收到的是一个元组,里面有个 socket对象,以及客户端的ip和端口 # (<socket.socketfd=420,family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM,proto=0,laddr=(‘127.0.0.1‘,8888),raddr=(‘127.0.0.1‘,50510)>, (‘127.0.0.1‘,50510)) conn,addr = sk.accept() # 会在这里阻塞,就是会在这里等着别人连接 ########注意这里用的socket对象 conn.send(bytes(‘不要幻想了‘,‘utf8‘)) # 不能发送String类型,要发送byte类型
服务器通过conn.close()关掉通讯
client.py
1 import socket 2 sk = socket.socket() 3 address = (‘127.0.0.1‘,8888) 4 sk.connect(address) 5 data = sk.recv(1024) 6 print(str(data,‘utf8‘))
客户端关闭自己的socket就可以关闭连接
#######通讯改进一#######
每人发一条信息,不能够停止,停止可以加个判断
server.py
1 import socket 2 def get_server_socket(): 3 sk = socket.socket() 4 server_address = (‘127.0.0.1‘,8888) 5 sk.bind(server_address) 6 sk.listen(5) 7 return sk 8 if __name__ == ‘__main__‘: 9 sk = get_server_socket() 10 print(‘waiting...‘) 11 conn,addr = sk.accept() 12 while True: 13 data = conn.recv(1024) 14 print(str(data,‘utf8‘)) 15 inp = input(‘>>>‘) 16 conn.send(bytes(inp,‘utf8‘)) 17 print(‘waiting...‘) 18 conn.close()
client.py
1 import socket 2 def connect_server(): 3 sk = socket.socket() 4 server_address = (‘127.0.0.1‘,8888) 5 sk.connect(server_address) 6 return sk 7 if __name__ == ‘__main__‘: 8 sk = connect_server() 9 while True: 10 inp = input(‘>>>‘) 11 sk.send(bytes(inp,‘utf8‘)) 12 print(‘waiting...‘) 13 data = sk.recv(1024) 14 print(str(data,‘utf8‘)) 15 sk.close()
##########通讯改进二################
客户端能正常退出
server.py
1 import socket 2 def get_server_socket(): 3 sk = socket.socket() 4 server_address = (‘127.0.0.1‘,8888) 5 sk.bind(server_address) 6 sk.listen(5) 7 return sk 8 if__name__ == ‘__main__‘: 9 sk = get_server_socket() 10 print(‘waiting...‘) 11 conn,addr = sk.accept() 12 while True: 13 data = conn.recv(1024) 14 print(str(data,‘utf8‘)) 15 if not data: 16 print(‘waitconnect...‘) 17 conn, addr = sk.accept() 18 continue 19 inp = input(‘>>>‘) 20 conn.send(bytes(inp,‘utf8‘)) 21 print(‘waiting...‘) 22 conn.close()
client.py
1 import socket 2 def connect_server(): 3 sk = socket.socket() 4 server_address = (‘127.0.0.1‘,8888) 5 sk.connect(server_address) 6 return sk 7 if __name__ == ‘__main__‘: 8 sk = connect_server() 9 while True: 10 inp = input(‘>>>‘) 11 if inp == ‘exit‘: 12 break 13 sk.send(bytes(inp,‘utf8‘)) 14 print(‘waiting...‘) 15 data = sk.recv(1024) 16 print(str(data,‘utf8‘)) 17 18 sk.close()
#############通讯改进三#############################
当一个客户端退出后,能够接受其他客户端的信息
server.py
1 import socket 2 def get_server_socket(): 3 sk = socket.socket() 4 server_address = (‘127.0.0.1‘,8888) 5 sk.bind(server_address) 6 sk.listen(5) 7 return sk 8 def get_conn(sk): 9 print(‘waitconnect...‘) 10 conn, addr = sk.accept() 11 return conn 12 if __name__ == ‘__main__‘: 13 sk = get_server_socket() 14 conn = get_conn(sk) 15 while True: 16 try: 17 data = conn.recv(1024) 18 ##Linux这里不会报错,如果强行关闭了conn,data就会变成空,也就是说Linux的直接不try就行了。 19 except Exception as e: 20 conn = get_conn(sk) 21 print(str(data,‘utf8‘)) 22 if not data: 23 conn = get_conn(sk) 24 continue 25 inp = input(‘>>>‘) 26 conn.send(bytes(inp,‘utf8‘)) 27 print(‘waiting...‘) 28 conn.close()
client.py
1 import socket 2 def connect_server(): 3 sk = socket.socket() 4 server_address = (‘127.0.0.1‘,8888) 5 sk.connect(server_address) 6 return sk 7 if __name__ == ‘__main__‘: 8 sk = connect_server() 9 while True: 10 inp = input(‘>>>‘) 11 if inp == ‘exit‘: 12 break 13 sk.send(bytes(inp,‘utf8‘)) 14 print(‘waiting...‘) 15 data = sk.recv(1024) 16 print(str(data,‘utf8‘)) 17 sk.close()