Socket Programming - Port Scanner. I Get Connection Timed Out, Why?


 
Thread Tools Search this Thread
Top Forums Programming Socket Programming - Port Scanner. I Get Connection Timed Out, Why?
# 1  
Old 10-25-2008
Socket Programming - Port Scanner. I Get Connection Timed Out, Why?

When i put the target IP as 127.0.1.1, the program is working fine, can catch blocked & open ports. But when i try to scan remotely, i get connection timed out! Can you tell me why? Smilie

Here is my code - Look at between where i put astriks - at the bottom:

Code:
#include<iostream>
#include<string>
#include<sstream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<errno.h>
using namespace std;


struct addrinfo hints;          //fill your host info - hints is input
struct addrinfo *results;        //gets your host info - results is output
char *remoteIP = new char[40];  //holds inserted remote IP
string tempPort;                //holds ports temporarly
int startingPort;               //stores the starting range of port number
int status;                     //receives the status of your pc address
int currentPort;                //holds current port

//program description
void progDesc()
{
     cout<<"This is a simple port scanner, scan range of\n";
     cout<<"ports on your local machine...\n"<<endl;

}


//getaddrinfe locates your machine, more specific
//details of your host address is returned to results. 
void getAddrIn()
{
    status = getaddrinfo(remoteIP, NULL , &hints, &results);

    if(status != 0)
    {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }
}


//Run the program
int main()
{
    int endingPort;                 //stores the ending rage of port number

    system("clear");

    //tell user what program does
    progDesc();
    
    //set size of hints to zero
    memset(&hints, 0, sizeof hints);

    //fill some of your host address info
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    //ask for remote IP
    cout<<"Please enter your target IP: ";
    cin>>remoteIP;

    //call get addrinfo()
    getAddrIn();

    //ask port range from user
    cout<<"Enter Starting Port: ";
    cin>>startingPort;

    cout<<"Enter Ending Port: ";
    cin>>endingPort;

    cout<<endl;

    cout<<"Start Scanning: "<<endl;

    //check the status
    while(startingPort <= endingPort)
    {
        //call getaddrinfo()
        getAddrIn();

        //create a socket.
        int socketfd;
        socketfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);

        if(socketfd == -1 )
        {
            cout<<"Error: failed to create a socket.\n";
            return 2;
        }

        //********************************************************************
        //add a specific port, you want to connect to, for the remote host
        struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;
        specifyPort->sin_port = htons(startingPort);

        //make connection to host IP/Port specified
        int connectStatus;
        connectStatus = connect(socketfd, results->ai_addr, results->ai_addrlen);
    
        if(connectStatus == -1 )
        {        
            if(errno == ECONNREFUSED)
                cout<<"Port "<<startingPort<<" is Closed or Blocked.\n";
            else if(errno == ETIMEDOUT)
                cout<<"The attempt to connect TIMED OUT before a connection was made."<<endl;
            else
                cout<<"Error: "<<strerror(errno)<<endl;
        }else{
            cout<<"Port "<<startingPort<<" is OPEN.\n";
        }
        //**********************************************************************

        close(socketfd);
    
        //move to the next port in the specified range
        startingPort++;

    }

    //deallocate memory
    delete[] remoteIP;

    //free linkedlist of struct addrinfo *results 
    freeaddrinfo(results);

    return 0;
}

This is my second project, i should finish it as soon as possible.
# 2  
Old 10-28-2008
Well, perhaps there's a firewall (or at least IP packet filter) dropping the packets.
# 3  
Old 10-28-2008
Oh i already solved them....

Thanks anyway
# 4  
Old 10-28-2008
Quote:
Well, perhaps there's a firewall (or at least IP packet filter) dropping the packets.
You are right. This port scanner is made so simple, not stealthy & does not pass any firewall. Otherwise, it should scan normally.

It works best if you use it in your network internally, or use against your machine.

I opened a port from terminal by doing

sudo nc -l -p AnyPortNumber

Then tested the program, It worked!

Note - Instead of doing this

struct sockaddr_in *specifyPort = (struct sockaddr_in *)results->ai_addr;
specifyPort->sin_port = htons(startingPort);

Do this,

status = getaddrinfo(remoteIP, startingPort , &hints, &results);


Better & easier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Ssh connection timed out

Hello, I have two AIX6.1 machines that can communicate with each other through ssh. The problem is that one of them somehow closes the connection after some time and i don't know the reason of that. For example today i send through rsync command 7 files from one server to the other. It send... (7 Replies)
Discussion started by: omonoiatis9
7 Replies

2. Programming

socket programming using UDP connection

I want to send packets through single socket() but using two different port numbers in UDP. Anybody give some idea on this. Thanks in advance.:) (2 Replies)
Discussion started by: naresh046
2 Replies

3. Solaris

Solaris 10 ftp connection problem (connection refused, connection timed out)

Hi everyone, I am hoping anyone of you could help me in this weird problem we have in 1 of our Solaris 10 servers. Lately, we have been having some ftp problems in this server. Though it can ping any server within the network, it seems that it can only ftp to a select few. For most servers, the... (4 Replies)
Discussion started by: labdakos
4 Replies

4. Programming

Cloning a socket connection, using other port numbers

Hello everybody, I've coded a multi-client server based on internet sockets using the scheme listen on port X-accept-fork, exactly like beej's guide At some point I would like to establish a secondary connection between a client and the server-child serving him. I was considering the... (4 Replies)
Discussion started by: jonas.gabriel
4 Replies

5. UNIX for Dummies Questions & Answers

Socket programming:One server two port

I want my server socket to listen on two ports in my machine. How do i achieve it? I will have two clients one connecting to 1 port and another to a different port. So my server needs to listen to both. Thanks. (1 Reply)
Discussion started by: abc.working
1 Replies

6. HP-UX

connection timed out

I am trying to connect with my hp machine using "dialup networking." It times out after 30 seconds. Is there a way to adjust this time. Would it have anything to do with rexec? thanks (0 Replies)
Discussion started by: paschal
0 Replies

7. Linux Benchmarks

Connection Timed out problem - EM64T

Hai All, I have problem during HTTP benchmarking with polygraph tool on EM64T machine. Benchmarking results are getting problem bcas of connection timed out system errors. Machine log information is as, /var/log/messages.3:Feb 18 16:17:44 proxy64 network: Bringing up interface eth0: ... (0 Replies)
Discussion started by: muthukumar
0 Replies

8. UNIX for Advanced & Expert Users

TCP port scanner for remote or for local

I am unable to find any TCP scanner for data captruing for a Remote or local server. Can anybody please help. i need it to read TCP port and capture the incoming/outgoing data , (3 Replies)
Discussion started by: fahadsiddiqui
3 Replies

9. UNIX for Advanced & Expert Users

deferred: connection timed out with NT

We recently installed a new release of SCO UNIX (5.0.6) and when I try to relay e-mail from the UNIX box to my NT server (the mail server) I get the following message from sendmail. Deferred: Connection timed out with nt I have nt set up as my relay server in sendmail.cf and the mail seems to... (8 Replies)
Discussion started by: jmossman
8 Replies

10. UNIX for Advanced & Expert Users

Connection Timed out

I connect to a Sun Box through telnet but it timed out in couple of minutes. Advance thanks for any idea...help... (2 Replies)
Discussion started by: s_aamir
2 Replies
Login or Register to Ask a Question