pyhon udp服务器读取字符流
程序如下:
from socket import *
import time
import os
host = ''
port =8899
bufsize = 1024
addr = (host,port)
def recv():
recvdata = socket(AF_INET,SOCK_DGRAM)
recvdata.bind(addr)
print("waiting for connection")
print("开始时间为:",time.strftime('%Y-%m-%d %H:%M:%S'))
f = open('8899UDP.txt','w',encoding = 'UTF-8')
while True:
data = recvdata.recvfrom(bufsize)
print(data,end = ' ')
print("timestamp:",time.strftime('%Y-%m-%d %H:%M:%S'))
data = str(data)
now = str(time.strftime('%Y-%m-%d %H:%M:%S'))
f.write(data)
f.write(now)
f.write("\n")
if __name__ == '__main__':
recv()
客户端采用sprintf()进行数据的封装,
举例:
char senddata[60];
sprintf(senddata,"%dBB,%d,%f,%f,%f",id,as,a[0],a[1],a[2]);
esp8266_sendstring(senddata,strlen(senddata));
pyhon udp服务器读取字节流
程序如下:
还未编写。。。。
客户端采用结构体进行数据的封装(注意:字节对齐)
举例:
typedef struct SendData
{
unsigned int id;
unsigned short as;
char a[3];
}SendData;
SendData goingto;
char godata[8];
goingto.id=12;
goingto.as=72;
goingto.a[0]=0.123124;
goingto.a[1]=-1.232122;
goingto.a[2]=-2.321421;
memset(godata,(char *)&goingto,sizeof(goingto));
esp9266_sendstring(godata,sizeof(godata));
memset(&goingto,0,sizeof(godata));