2022-08-26 17:25:45 +08:00
|
|
|
import socket
|
|
|
|
import random
|
|
|
|
from PikaStdData import String
|
|
|
|
from PikaStdData import ByteArray
|
|
|
|
|
|
|
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
host = "127.0.0.1"
|
2022-08-28 17:53:22 +08:00
|
|
|
port = 9999 + random.randint(0, 1000)
|
2022-08-26 17:25:45 +08:00
|
|
|
print("port:", port)
|
|
|
|
server.bind((host, port))
|
|
|
|
server.listen(5)
|
|
|
|
|
|
|
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
client.connect((host, port))
|
|
|
|
|
|
|
|
res = server.accept()
|
|
|
|
accept = res[0]
|
|
|
|
addr = res[1]
|
|
|
|
|
2022-08-28 06:54:13 +00:00
|
|
|
print("recv from client: %s" % str(addr))
|
2022-08-26 17:25:45 +08:00
|
|
|
|
|
|
|
client.send(String("send test from client").encode())
|
2022-08-28 06:54:13 +00:00
|
|
|
print("server recv:", ByteArray(accept.recv(1024)).decode())
|
2022-08-26 17:25:45 +08:00
|
|
|
|
|
|
|
accept.send(String("send test from server").encode())
|
2022-08-28 06:54:13 +00:00
|
|
|
print("client recv:", ByteArray(client.recv(1024)).decode())
|
2022-08-26 17:25:45 +08:00
|
|
|
|
|
|
|
accept.close()
|
|
|
|
client.close()
|
|
|
|
server.close()
|