Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java python可以';从网络打印未打包的浮动_Java_Python_Sockets - Fatal编程技术网

Java python可以';从网络打印未打包的浮动

Java python可以';从网络打印未打包的浮动,java,python,sockets,Java,Python,Sockets,我尝试从UDP数据报获取浮点数并打印它们以验证: import socket from struct import * socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) socket.bind( ('127.0.0.1', 2416) ) msg = bytearray( 4*1024 ) f1 = 0.0 f2 = 0.0 f3 = 0.0 while True: nBytes = socket.recv_into(

我尝试从UDP数据报获取浮点数并打印它们以验证:

import socket
from struct import *

socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket.bind( ('127.0.0.1', 2416) )
msg = bytearray( 4*1024 ) 
f1 = 0.0
f2 = 0.0
f3 = 0.0
while True:
    nBytes = socket.recv_into( msg )
    print( '%d bytes received' % nBytes )
    (f1) = unpack_from( '!f', msg, 0 )
    (f2) = unpack_from( '!f', msg, 4 )
    (f3) = unpack_from( '!f', msg, 8 )
    print( '%f, %f, %f received' % ( f1, f2, f3 ))
出现以下错误:

$ python Server.py
12 bytes received
Traceback (most recent call last):
  File "Server.py", line 13, in <module>
    print( '%f, %f, %f received' % ( f1, f2, f3 ))
TypeError: a float is required
有关Java UDP发送方(客户端)的信息:


这条线就是问题所在:

(f1) = unpack_from( '!f', msg, 0 )
试一试

请注意附加的逗号。其他两行也是如此


正如您所提到的,
unpack\u from
返回一个元组
(f1)
不是元组,而是单个值
(f1,)
是一个包含一个元素的元组。

此程序运行良好:

import socket
from struct import *

socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket.bind( ('127.0.0.1', 2416) )
msg = bytearray( 4*1024 )
while True:
    nBytes = socket.recv_into( msg )
    print( '%d bytes received' % nBytes )
    (f1,) = unpack_from( '!f', msg,  0 )
    (f2,) = unpack_from( '!f', msg,  4 )
    (f3,) = unpack_from( '!f', msg,  8 )
    (d1,) = unpack_from( '!d', msg, 12 )
    (d2,) = unpack_from( '!d', msg, 20 )
    (d3,) = unpack_from( '!d', msg, 28 )
    print( f1, f2, f3, d1, d2, d3 )
    print( type(f1), type(d1))
和打印:

$ python Server.py
36 bytes received
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
<class 'float'> <class 'float'>
36 bytes received
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
<class 'float'> <class 'float'>
36 bytes received
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
<class 'float'> <class 'float'>
$python Server.py
接收到36字节
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
接收到36字节
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
接收到36字节
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12

f1、f2和f3的数据类型是什么?这是我第一次使用Python,如何打印数据类型?unpack返回一个元组,我必须使用
[0]
获取第一部分,额外的逗号表示f1是元组的一部分,而不是元组本身?有点像
X
是一个变量<代码>(X)也是一个变量
(X,Y,Z)
是一个大小为3的元组
(X,)
是一个大小为1的元组。我知道,您的解决方案比我的更具表现力,我将更改我的代码。
(f1,) = unpack_from( '!f', msg, 0 )
import socket
from struct import *

socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket.bind( ('127.0.0.1', 2416) )
msg = bytearray( 4*1024 )
while True:
    nBytes = socket.recv_into( msg )
    print( '%d bytes received' % nBytes )
    (f1,) = unpack_from( '!f', msg,  0 )
    (f2,) = unpack_from( '!f', msg,  4 )
    (f3,) = unpack_from( '!f', msg,  8 )
    (d1,) = unpack_from( '!d', msg, 12 )
    (d2,) = unpack_from( '!d', msg, 20 )
    (d3,) = unpack_from( '!d', msg, 28 )
    print( f1, f2, f3, d1, d2, d3 )
    print( type(f1), type(d1))
$ python Server.py
36 bytes received
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
<class 'float'> <class 'float'>
36 bytes received
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
<class 'float'> <class 'float'>
36 bytes received
1.2000000476837158 3.4000000953674316 5.599999904632568 7.8 9.1 11.12
<class 'float'> <class 'float'>