Server:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/2/6 14:15
# @Author : Beam
# @File : server.py
# @Software: PyCharm
from socket import *
import time
HOST = '192.168.0.10'
PORT = 9999
s = socket(AF_INET, SOCK_DGRAM)
s.bind((HOST, PORT))
print('...waiting for message..')
n = 100
data, address = s.recvfrom(1024)
print(data.decode('utf-8'), address)
while True:
time.sleep(int(n))
msg = str(n)
s.sendto(bytes(msg.encode('utf8')) ,address)
n +=1
s.close()
Client:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/2/6 14:16
# @Author : Beam
# @File : client.py
# @Software: PyCharm
from socket import *
HOST = '192.168.0.10'
PORT = 9999
s = socket(AF_INET, SOCK_DGRAM)
s.connect((HOST, PORT))
s.sendall(b'HI\r\n')
while True:
data = s.recv(1024)
if data:
print('睡眠'+ str(data.decode('utf-8')) +'秒后,server发送到client还在连接')
s.close()
网友评论