Android NDK(套接字)-recvfrom() 将 sockaddr * 设置为 NULL(无法获取发送者的 IP)
当我使用 Android NDK 调用 recvfrom
时,我的 sockaddr_in
from
返回了 NULL
。这个完全相同的代码在我的桌面环境上运行良好,但在设备上则不然。
int MyClass::ReceiveData(char *buffer, int bufferLength)
{
int numBytes = 0;
struct sockaddr_in from;
socklen_t fromLength = sizeof(struct sockaddr_in);
numBytes = recvfrom(mConnectionSocket,
buffer,
bufferLength,
0,
(struct sockaddr *)&from,
&fromLength);
if (numBytes == -1)
perror("recvfrom");
int fromAddress = ntohl(from.sin_addr.s_addr);
return fromAddress;
}
有人有什么想法吗?
When I call recvfrom
using the Android NDK, I get NULL
returned to my sockaddr_in
from
. This same exact code works fine on my desktop environment, but not on the device.
int MyClass::ReceiveData(char *buffer, int bufferLength)
{
int numBytes = 0;
struct sockaddr_in from;
socklen_t fromLength = sizeof(struct sockaddr_in);
numBytes = recvfrom(mConnectionSocket,
buffer,
bufferLength,
0,
(struct sockaddr *)&from,
&fromLength);
if (numBytes == -1)
perror("recvfrom");
int fromAddress = ntohl(from.sin_addr.s_addr);
return fromAddress;
}
Anybody have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对于该代码是不可能的。
from
不是一个可以变为NULL
的指针。更可能的是,您的由from
命名的 sockaddr_in 结构正在被清零。这是由于在 TCP 套接字上使用它而导致的。如果这是 TCP,您应该在套接字上调用 getpeername() 来获取对等地址。That's not possible with that code.
from
isn't a pointer that can becomeNULL
. More probably your sockaddr_in structure named byfrom
is getting zeroed. Which would be caused by using this on a TCP socket. If this is TCP you should be callinggetpeername()
on the socket to get the peer address.