在linux中使用C进行广播后获取IP地址

nis*_*ant 2 c linux networking gcc

我在Linux下用C编写了一个简单的客户端服务器程序.我创建了UDP套接字.客户端使用广播消息

sendto(clientsock, buf, 100, 0, (struct sockaddr *)&to, sizeof (to)); 
Run Code Online (Sandbox Code Playgroud)

服务器使用接收广播的消息

recvfrom(serversock, buf, 100, 0, (struct sockaddr *)&rcv,&size);
Run Code Online (Sandbox Code Playgroud)

服务器正在成功接收消息.在服务器端,我需要找到收到的消息的源IP地址.我怎样才能做到这一点 ?

ayu*_*ush 5

对于像这样的电话 -

recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)
Run Code Online (Sandbox Code Playgroud)

使用

printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
Run Code Online (Sandbox Code Playgroud)

  • 为了上帝的缘故,至少使用`inet_ntop`. (2认同)