[英]Getting the ip address of a device in a network
from the topic itself how can i detect device via its ip address. 从主题本身,我如何通过其IP地址检测设备。 When i detected it the device will send a response to the server that it is now connected just like detecting a new device but the device is not connected to the linux machine.
当我检测到它时,设备将向服务器发送响应,表明它现在已连接,就像检测到新设备一样,但该设备未连接到linux计算机。 Here i got a picture..
在这里,我有一张照片。
When the device is now connected I can send data to it via it's ip address so i can control it.. the device will compose of motors, led lights for which linux will send commands to them. 当设备现在连接时,我可以通过它的IP地址向它发送数据,以便我可以控制它。.该设备将由电动机,Linux为其发送命令的LED灯组成。 commands like.. move up, move down, left ,right for the motor and on/off for the led lights.
诸如..电动机的上,下,左,右命令以及led灯的开/关命令。
How can i do it in c? 我如何在C语言中执行此操作?
When your device talks back you will get an IP address. 当您的设备回话时,您将获得一个IP地址。 I assume your detection mechanism is something like broadcasting on UDP.
我认为您的检测机制就像在UDP上广播一样。 If you're using Berkeley sockets you will use either of the following two functions:
如果使用的是Berkeley套接字,则将使用以下两个功能之一:
int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); // TCP method of receiving connection
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); // UDP method of receiving data
In these, the cliaddr
and src_addr
respectively will give you the IP of the computer sending packets. 其中,
cliaddr
和src_addr
分别为您提供发送数据包的计算机的IP。 For instance on linux the sockaddr struct for IPv4 is: 例如,在Linux上,IPv4的sockaddr结构为:
/* Internet address. */
struct in_addr {
__be32 s_addr;
};
struct sockaddr_in {
__kernel_sa_family_t sin_family; /* Address family */
__be16 sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
/* Pad to size of `struct sockaddr'. */
unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) -
sizeof(unsigned short int) - sizeof(struct in_addr)];
};
if you know the device's host name, you can use gethostbyname , which returns you a hostent struct containing the required data, you are looking for (IP address). 如果您知道设备的主机名,你可以使用的gethostbyname ,它返回你一个hostent包含所需的数据结构,你正在寻找(IP地址)。
from there you can proceed establishing a TCP connection or sending UDP packages etc... 从那里您可以继续建立TCP连接或发送UDP包等...
cheers 干杯
I found this to be a useful tutorial: 我发现这是一个有用的教程:
linuxhowtos.org/C_C++/socket.htm linuxhowtos.org/C_C++/socket.htm
It actually covers all you might want to know for getting started with socket programming and is well written. 它实际上涵盖了您可能想了解的有关套接字编程入门的所有知识,并且编写得很好。
There are many ways to get the data about a remote device. 有很多方法可以获取有关远程设备的数据。
Socket programming is the generic way to connect to a remote system and getting the data about it. 套接字编程是连接到远程系统并获取有关其数据的通用方法。
For the protocol SNMP use the following library to work it in programming. 对于协议SNMP,请使用以下库在编程中使用它。
For the nmap tool you can use following link 对于nmap工具,您可以使用以下链接
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.