如何添加扫描仪端口

时间:2019-04-09 14:28:38

标签: c sockets server client port

我用c和显示套接字创建了代码,以在我的服务器中连接地址,现在我需要为其中一个地址中的扫描器端口做一些代码,输入是开始端口和结束端口以及服务器扫描端口,连接到本地地址之一 请我neeeeeeeeeeeed帮助 帮助我的人

#include <netdb.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;
        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);


    // After Display close the socket 
    close(sockfd); 

}

0 个答案:

没有答案