我需要找到所有網絡服務器到包含IP地址的文件的程序。 如果他的端口80打開,我發現IP地址是服務器。我寫了這段代碼,但它不起作用。 Аlways說80端口是關閉的,即使我用開放端口80寫IP也是如此(例如194.153.145.104)。我錯在哪裏? 我cheking這裏IP與開放端口:http://www.yougetsignal.com/tools/open-ports/C/UNIX:端口掃描器
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
u_short port=80; /* user specified port number */
short int sock = -1; /* the socket descriptor */
struct hostent *host_info; /* host info structure */
struct sockaddr_in address; /* address structures */
char addr[1023];
char buf[20];
char *filename;
filename=argv[1];
FILE *file = fopen(filename, "r");
while (!feof(file))
{
fscanf(file,"%s",buf);
strncpy(addr, buf, 1023);
bzero((char *)&address, sizeof(address));
address.sin_addr.s_addr = inet_addr(addr);
address.sin_port = htons(port);
address.sin_family=AF_INET;
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) {
fprintf(stderr, "Error: could not assign master socket\n");
exit (1);
}
if(connect(sock,(struct sockaddr *)&address,sizeof(address)) == 0)
printf("%s is a web server\n", addr);
else printf("%s isn't a web server\n", addr);
close(sock);
}
return 0;
}
這裏有問題嗎?什麼不行? – FDinoff
是的。程序總是說80端口是關閉的,即使我用打開的端口80寫IP也是如此(例如194.153.145.104) – cattaneo