【发布时间】:2019-08-30 18:41:12
【问题描述】:
i 编码扫描服务器和客户端扫描所有 ip 我使用套接字/C 但我不明白如何为结构中的一个 ip 添加扫描端口 我的扫描端口有问题 我有一个错误的显示他没有显示任何打开 问题是什么都没有显示服务器中打开的任何端口。 我找不到任何解决方案 我需要帮助
#include <netdb.h>
#include "stdio.h"
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Driver function
int main()
{
int sockfd, connfd, len ,start , end,err;
struct sockaddr_in servaddr, cli;
struct sockaddr_in addr_remote;
char ips[2][20];
char hostname[30];
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
for(int i=0;i<2;i++){
connfd = accept(sockfd, (SA*)&addr_remote, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
strcpy(ips[i], inet_ntoa(addr_remote.sin_addr));
}
// Display Ip
for(int i=0;i<2;i++){
printf("%s \n",ips[i]);}
// Scan PORT
//Get the hostname to scan
printf("Enter hostname or IP : ");
gets(hostname);
//Get start port number
printf("Enter start port number : ");
//To Handle the Input
int data;
data = scanf("%d",&start);
//Get end port number
printf("Enter end port number : ");
int data2 = scanf("%d" , &end);
int j;
//Start the port scan loop
printf("Starting the portscan loop : \n");
for( j = start ; j <= end ; j++)
{
//Fill in the port number
servaddr.sin_port = htons(j);
//Connect using that socket and sockaddr structure
err =connect(sockfd, (SA*)&servaddr, sizeof(servaddr));
if( err < 0 )
{
//printf("%s %-5d %s\r" , hostname , i, strerror(errno));
fflush(stdout);
}
//connected
else
{
printf("%-5d open\n", j);
}
}
// After chatting close the socket
close(sockfd);
}
【问题讨论】:
-
如果对等方没有应答,
connect()调用会阻塞很长时间,直到达到连接超时。这是你的问题吗? -
是的,他不显示地址打开的端口