我正在尝试构建一个快速脚本来侦听一个 UDP 数据包,将其存储到文件中,然后退出。
socat来救援!
$ socat UDP-RECVFROM:9999 CREATE:/tmp/results_9999
2016/04/08 11:56:59 socat[1955] E read(6, 0x5220d3d0020, 8192): Bad file descriptor
嗯?
该文件包含收到的数据包:
$ cat /tmp/results_9999
SUCCESS on Port 9999
strace 似乎表明它试图读取它写入的文件:
recvfrom(5, "SUCCESS on Port 9999\n", 8192, 0, {sa_family=AF_INET, sin_port=htons(34032), sin_addr=inet_addr("192.168.56.102")}, [16]) = 10
recvfrom(3, 0x78ea087b3700, 519, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(3, 0x78ea087b3d40, 519, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
write(6, "SUCCESS on Port 9999\n", 10) = 10
recvfrom(3, 0x78ea087b3d40, 519, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
recvfrom(3, 0x78ea087b3840, 519, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
read(6, 0xb675ac8f020, 8192) = -1 EBADF (Bad file descriptor)
recvfrom(3, 0x78ea087b3840, 519, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
open("/etc/localtime", O_RDONLY|O_NONBLOCK|O_CLOEXEC) = 7
fstat(7, {st_mode=S_IFREG|0644, st_size=2223, ...}) = 0
mmap(NULL, 2223, PROT_READ, MAP_SHARED, 7, 0) = 0x6c38b6779000
close(7) = 0
getpid() = 1954
writev(2, [{"", 0}, {"2016/04/08 11:56:41 socat[1954] "..., 84}], 22016/04/08 11:56:41 socat[1954] E read(6, 0xb675ac8f020, 8192): Bad file descriptor
) = 84
exit_group(1) = ?
+++ exited with 1 +++
为什么它需要读取文件,如果是这样,他们为什么要以只写方式打开文件?
有没有解决这个问题的开关还是一个错误?
在这个时间点,socat显然假设双向通信,即使它没有意义。
要覆盖它,请使用-u(单向)开关。
例如
——感谢 Gerhard Rieger 通过电子邮件澄清这一点。