Python UDP SocketServer无法读取整个p

2024-09-26 22:08:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在发送方,我有以下使用处理语言的代码(部分代码):

udp = new UDP( this, 6002 );  // create a new datagram connection on port 6000
  //udp.log( true );               // <-- printout the connection activity
  udp.listen( true );              // and wait for incoming message 

escribeUDPLog3(1,TRANSMIT,getTime());  //call function

int[] getTime(){
 int year = year();
 int month = month()
 int day = day();
 int hour = hour();
 int minute = minute();
 int second = second();

 int[] time_constructed = {year, month,day,hour,minute,second};
 return time_constructed;
}


void escribeUDPLog3(int pkg_type, int state, int[] time){

short year = (short)(time[0]); //>> 8;
  byte year_msb = byte(year >> 8);
  byte year_lsb = byte(year & 0x00FF);     
  byte month = byte(time[1]);
  byte day = byte(time[2]); 
  byte hour = byte(time[3]); 
  byte minute = byte(time[4]); 
  byte second = byte(time[5]);

  byte[] payload = {byte(pkg_type), byte(state), year_msb, year_lsb, month, day, hour, minute,second};


  try {
    if (UDP5_flag) {udp.send(payload, UDP5_IP, UDP5_PORT);} 
  }  
  catch (Exception e) {
    e.printStackTrace();
  }
}

在接收方,我使用SocketServer python结构设置一个监听udp数据报的服务器,如下所示。在

^{pr2}$

编辑时间: 我在使用timeConstructor(data[2:9])时遇到了问题,因为我访问的是索引外的数据,有时(在打印的帮助下)我无法从数据接收到字节,有一次它使我出索引,因为我没有收到分钟。大多数时候代码都能正常工作,但是这种类型的错误让我很好奇。在

旧版: 问题是在读取有效负载时,有时似乎有些字节没有到达,即使我使用Wireshark捕获了整个有效负载(但是Wireshark没有告诉我这是发送的数据包还是接收的数据包,因为我使用的是环回接口,可能是重复的信息?)。如果数据报有16个字节的有效负载长度,有时我会收到15个字节,因为当从数据中进行解析时,我会出现索引错误。 我认为有一些缓冲问题。不是吗?如何正确配置?我知道我会因为无连接协议而丢失数据包,但我不认为字节会丢失。假设“data”包含来自一个udp数据报的所有有效负载数据。在


Tags: 数据代码字节timebyte数据包yearint

热门问题