首页 > 解决方案 > printf 不会在屏幕上返回任何内容

问题描述

我在用 C 语言编程时遇到问题。我编译了一个程序,扫描用户给定的 IP 上的开放端口,当端口打开时,它必须显示在屏幕上。问题是,它不打印任何东西!程序运行,但屏幕上没有显示任何内容。

#include <stdio.h>          // printf(), perror()
#include <sys/types.h>      // AF_INET, SOCK_STREAM
#include <sys/socket.h>     // socket(), connect()
#include <arpa/inet.h>      // htons(), inet_addr()
#include <unistd.h>         // close()

int main(int argc, char *argv[])
{
    int meusocket;
    int conecta;

    int port;
    int inicio = 0;
    int final = 65535;
    char * destino;
    destino = argv[1];

    struct sockaddr_in alvo;

    if(argv[1] == "")
    {
        printf("Informe o IP de acordo com o exemplo: \n");
        printf("./portscan 192.168.100.1");
        close(meusocket);
        return 0;
    }else{

    for(port=inicio;port<final;port++)
    {

    meusocket = socket(AF_INET, SOCK_STREAM, 0);
        alvo.sin_family = AF_INET;
        alvo.sin_port = htons(port);
        alvo.sin_addr.s_addr = inet_addr(destino);

        conecta = connect(meusocket, (struct sockaddr *)&alvo, sizeof alvo);

        if(conecta == 0)
        {
            printf("Porta %i [ABERTA] \n",port);
            close(meusocket);
            return 0;
        }else{
            close(meusocket);
            return 0;
        }

    }
    }
}    

标签: printf

解决方案


推荐阅读