我有一个char类型的IP地址像char ip ="192.123.34.134"我想增加最后一个值(134).有人应该怎么做?我想,我应该将它转换为整数,然后再回来,但不幸的是我不知道怎么做?:(我正在使用C++.
请帮我!
谢谢,kampi
我正在编写一个端口扫描器,目的是循环遍历整个子网,以查看端口 445 是否在任何主机上打开,作为学习 C 编程的一种方式。该程序当前的设置方式是,需要手动输入一个 IP 地址,然后扫描仪就会启动。它工作完美(终于)。
我当前面临的问题:我不确定如何使该程序扫描整个子网,而不仅仅是单个主机。研究完这个问题后,我相信最好的方法是去掉 IP 地址的最后一个八位字节,并让它从 1-254 循环。然而,我并不完全确定如何开始解决这个问题。这是我当前的代码(为了澄清而大量注释):
...
void portScan() {
struct hostent *host;
int err, i , sock ,start , end;
char hostname[100];
struct sockaddr_in sa;
//Get the hostname to scan
printf("Enter hostname or IP to scan: ");
gets(hostname);
//Get start port number
start = 445;
//Get end port number
end = 445;
//Initialise the sockaddr_in structure
strncpy((char*)&sa , "" , sizeof sa);
sa.sin_family = AF_INET;
//direct ip address, use it
if(isdigit(hostname[0])) {
sa.sin_addr.s_addr …Run Code Online (Sandbox Code Playgroud)