当前,我们正在研究套接字的基本知识...对于套接字我是完全陌生的。我的问题是,我总是将套接字源地址设为0.0.0.0。任何人都可以帮助我解决问题。下面是代码段
int main(int argc, char argv[])
{
struct sockaddr_in si_me, si_other;
int s, i, slen = sizeof(struct sockaddr_in) , recv_len;
unsigned char buf[BUFLEN];
char cmd[2048];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
perror("Failed to open socket:");
return -1;
}
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if( bind(s, (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
perror("Failed to bind socket:");
close(s);
return -1;
}
while(1)
{
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr )&si_other, &slen)) == -1)
{
perror("Failed to recv:");
close(s);
return -1;
}
/ print details of the client/peer and the data received /
printf( "Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
inet_ntoa(si_other.sin_addr)--->始终返回0.0.0.0
预先感谢
答案 0 :(得分:0)
警告不可忽略!
无关,但这是不正确的:split
。 b %>%
split(b[["id"]]) %>%
lapply(bind_rows, a) %>%
lapply(select, -"id") %>%
bind_rows(.id = "id")
# # A tibble: 10 x 3
# id time value
# <chr> <dbl> <dbl>
# 1 a 1 1
# 2 a 2 2
# 3 a 3 3
# 4 a -1 100
# 5 a 0 200
# 6 b 1 4
# 7 b 2 5
# 8 b 3 6
# 9 b -1 100
# 10 b 0 200
的第二个参数应该是int main(int argc, char argv[])
,而不是main
。你应该写
char **
或者简单地
char *
如果您不打算处理命令行。
这与您的问题无关,只是问题是由类似的错误引起的:
int main(int argc, char *argv[])
当期望的是指针 int main()
时,您错误地将recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr )&si_other, &slen))
强制转换为&si_other
。我刚刚将该行改写为:
struct sockaddr
然后我立即获得了原始地址。
也不建议使用BTW强制转换指针,因此您应该让struct sockaddr *
是 if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)) == -1)
(或至少是slen
)而不是socklen_t
。