python udp client

姬自强
2023-12-01
udp_p = Process(target=udp_client.init, args=(status,HOST,))  # 申请子进程
udp_p.start()  # 运行进程
# 获取本机电脑名
myname = socket.getfqdn(socket.gethostname())
# 获取本机ip
myaddr = socket.gethostbyname(myname)
ip_id= myaddr[8:10]
status = multiprocessing.Value("i", 0)
HOST='192.168.30.88'
if ip_id.endswith("."):
    HOST = "192.168."+ip_id+"88"

 

 

from socket import *

import time

# HOST = '192.168.30.88'
PORT = 9999

def init(param,HOST):
    s = socket(AF_INET, SOCK_DGRAM)
    s.settimeout(5)
    conn =False
    while True:
        if conn:
            try:
                s.sendall(str(param.value).encode("utf-8"))
                data = s.recv(1024)
                print(data)
            except Exception as E:
                print("recv error",E)
                time.sleep(2)
                conn=False
        else:
            try:
                print("init",HOST,PORT)
                ret= s.connect((HOST, PORT))
                conn = True
            except Exception as E:
                print("conn error", E)
                time.sleep(2)
        time.sleep(0.5)

    s.close()
 类似资料:

相关阅读

相关文章

相关问答