sever = socket.socket() sever.bind(("127.0.0.1",6969)) sever.listen() while True: #在建立连接之前加一个死循环 conn,address = sever.accept() print("电话来了") count = 0 #加一个计数器 while True: data = conn.recv(1024) if not data:break #这边如果接受客户端数据为空,则重新建立连接 ...
phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) ip_port=("127.0.0.1",8080) phone.bind(ip_port) phone.listen(5) conn,addr=phone.accept() data1=conn.recv(1024) data2=conn.recv(1024) print("第1个包",data1) print("第2个包",data2) #客户端 import socket,time phone=socket.soc...
sk=socket.socket()#在bind方法之前加上这句话,可以让一个端口重复使用sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)#绑定地址端口(在网络上注册主机)sk.bind( ("127.0.0.1",9001) ) sk.listen() conn,addr=sk.accept() conn.send("6".encode("utf-8")) message="hello,my"conn.send(mess...
importsocket#socket.SOCK_DGRAM 代表udp协议sk = socket.socket(type =socket.SOCK_DGRAM)#绑定地址(例子为本机地址)sk.bind(("127.0.0.1", 9000))#udp 作为服务器,第一次一定是先接收消息#接收消息可以得到,客户端发的信息和客户端ip地址msg,cli_addr = sk.recvfrom(1024)#记得一定要将接收到的数据进行解...
socket.SOCK_STREAM) server_socket.bind(('localhost', 8888)) server_socket.listen(5) while...
import socketHOST = 'localhost' # 主机名PORT = 8888 # 端口号with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:s.bind((HOST, PORT))s.listen()print(f"Server listening on {HOST}:{PORT}")while True:conn, addr = s.accept()with conn:print(f"Connected by {addr}")while True...
import socket def udp_receive(port): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(('0.0.0.0', port)) received_packets = {} total_packets = None while True: packet, addr = sock.recvfrom(65535) header, data = packet[:10], packet[10:] ...
socket(socket.AF_INET, socket.SOCK_STREAM) ip = '' # ip可以是127.0.0.1,也可以为空 port = 0000 # 端口号 server_socket.bind((ip, port)) server_socket.listen(5) logger.info('等待连接') while True: try: client_socket, address = server_socket.accept() logger.info(address[0] + '已...
#1、创建socket(买手机) s=socket.socket()#无参数,默认封装TCP协议 #2、为socket绑定ip地址和和端口号(买手机卡,装在手机上) s.bind(ip_port)#参数必须为一个元祖的形式 #3、监听端口号请求,随时准备接收客户端发来的连接(开机等待接电话) s.listen(5)#参数代表最大挂起5个链接,最大接听5个客户端,pyt...
server=socket.socket()server.bind("localhost",6666)# localhost是本地主机,也可以写172.0.0.1server.listen(6)# 同时允许5个客户端whileTrue:# 此位置的while是为了客户端结束后,再等待其它客户端进入。 conn,addr=server.accept()# 接收一套接字信息,和地址。对应的是客户端的connectwhileTrue:conn.recv(1024...