我正在尝试构建一个快速脚本,用于监听一个 UDP 数据包、将其存储到文件中并退出。
索卡特来救援!
$ 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 +++
为什么需要读取该文件,如果是的话,为什么他们要以只写方式打开该文件?
有没有开关可以修复此问题或者这是一个错误?
答案1
此时,索卡特显然假设双向沟通即使它没有任何意义。
要覆盖此设置,请使用-u(单向)开关。
例如
socat -u UDP-RECVFROM:9999 CREATE:/tmp/results_9999
——感谢 Gerhard Rieger 通过电子邮件澄清这一点。