import socket
import os
import threading
import time
class SocketClient(threading.Thread):
def __init__(self):
super(SocketClient, self).__init__()
self._addr = "/tmp/socket/unix_socket"
self._socket = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_DGRAM)
def connect(self):
self._socket.connect(self._addr)
def run(self):
while True:
msg = "hello, I am is client socket"
self._socket.sendall(msg.encode())
time.sleep(1)
if __name__ == "__main__":
try:
client = SocketClient()
client.connect()
except Exception as e:
print(f"exception {
e}")
else:
client.start()
import socket
import os
import threading
import time
class SocketServer(threading.Thread):
def __init__(self):
super(SocketServer, self).__init__()
self._addr = "/tmp/socket/unix_socket"
self._socket = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_DGRAM)
def bind(self):
if os.path.exists(self._addr):
os.unlink(self._addr)
self._socket.bind(self._addr)
def run(self):
while True:
msg = self._socket.recv(1024)
print(f"server recv msg:{
msg.decode()}")
time.sleep(1)
if __name__ == "__main__":
try:
server = SocketServer()
server.bind()
except Exception as e:
print(f"exception {
e}")
else:
server.start()