Problem with Winsock. connect() returning -1(error)

This is a discussion on Problem with Winsock. connect() returning -1(error) within the Networking/Device Communication forums, part of the General Programming Boards category; Hello, Im trying to developing a simple portscan, but I m getting an error in the connect() function. connect() is ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Location
    /home
    Posts
    2

    Problem with Winsock. connect() returning -1(error)

    Hello,

    Im trying to developing a simple portscan, but I m getting an error in the connect() function. connect() is returning -1 and I have no idea why and how to solve.

    Error SS:

    Name:  scanningports2.jpg
Views: 558
Size:  50.7 KB



    Before the connect() function, I have debugged and it is ok.

    The code:

    Code:
    #include <stdlib.h>   // atoi()
    #include <stdio.h>    // I/O
    #include <winsock2.h> // winsock 2.0
    #include <ws2tcpip.h>
    
    
    #define INITIAL_PORT 1
    #define FINAL_PORT 65535
    
    
    int main(int argc, char* argv[])
    {
       int ports,           // represents the analised port
           initialPort,     // initial port of scanning
           finalPort,       // final port of scanning
           mySocket,        // socket used for analysis of ports
           returnConnect;
    
       WSADATA wsaData;
       struct sockaddr_in target;    
       struct hostent *host;
       struct servent *service;
    
       WSAStartup(MAKEWORD(2,2),&wsaData);
    
       if(argc == 1)
       {
          printf("\n       ********************************************************************\n");      
          printf("       **           ScanPort v1.0 (English Version)                      **\n");    
          printf("       **                 Developed by Insurgente                        **\n");    
          printf("       **  USE  <%s> <target> <initial-port> <final-port>         **\n",argv[0]);    
          printf("       ********************************************************************\n");     
    
          return 0;
       }
    
       if(argc == 2)
       {
           initialPort = INITIAL_PORT;
           finalPort = FINAL_PORT;
       }
    
       if(argc > 2)
       {
          initialPort = atoi(argv[2]);
          finalPort = atoi(argv[3]);
       }
    
       printf("\n Scanning Ports from <%d> to <%d> in <%s>:\n",initialPort,finalPort,argv[1]);
    
       for(ports = initialPort; ports < finalPort; ports++)
       {
          host = gethostbyname(argv[1]);
          if (host == NULL)
          {
             printf("\nCant find Host\n");
             return 1;        
          }
    
          // Created to analyze the socket port
          mySocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    
          if(mySocket < 0)
          {
             printf("\n\nError creating socket\n\n");
             return 1;
          }
    
          target.sin_family = host->h_addrtype;       // address type returned
          target.sin_port = htons(ports);            // receive the analysed port
          target.sin_addr = *((struct in_addr*)host->h_addr); //inet_addr("l0.0.2.15");
          memset(&target.sin_zero,0,sizeof(target.sin_zero));
    
          returnConnect = connect(mySocket,(struct sockaddr*)&target,sizeof(target));
    
          if(returnConnect == 0) 
          {
             printf("\nIF CONNECT()\n");
             service = getservbyport(htons(ports),"TCP");
             printf("\nPort [%d] is open. Service: [%s]",ports,(service == NULL?"Unknown":service->s_name));
             closesocket(mySocket);
          }
          else // if returnConnect == -1  then error
          {
             printf("\nELSE CONNECT()\n Error: %d,\n WSAGetLastError: %d",returnConnect,WSAGetLastError());
             closesocket(mySocket);  // if dont can to connect in port, closes the socket
          }
       }
    
    }

    If someone can help me, I thank.






    Sorry for my bad english

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well did you get as far as looking up what error code 10061 means?
    Windows Sockets Error Codes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    /home
    Posts
    2
    Quote Originally Posted by Salem View Post
    Well did you get as far as looking up what error code 10061 means?
    Windows Sockets Error Codes
    Thanks Salem.

    Yes.

    WSAECONNREFUSED10061 Connection refused. No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.
    But...this code is based in a source-code for linux, and for linux, this code runs good, showing the services running in the ports like http(80). Windows works different?






    Sorry for my english

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    But does YOUR windows (aka localhost) have a web server running on port 80?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Code:
    for(ports = initialPort; ports < finalPort; ports++)
    {
          host = gethostbyname(argv[1]);
          if (host == NULL)
          {
             printf("\nCant find Host\n");
             return 1;        
          }
    host isn't changing, so why the heck would you want to do a DNS lookup every time through the loop?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Insur View Post
    Thanks Salem.
    WSAECONNREFUSED10061 Connection refused. No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.
    But...this code is based in a source-code for linux, and for linux, this code runs good, showing the services running in the ports like http(80). Windows works different?
    Way different!

    There is another reason why ports will refuse a connection ... It's called a firewall. Even if the service is running behind the firewall, you won't get a connection to it without creating a firewall exception ... *even on your own machine *.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (C++) Winsock connect and listen program. Need Help!
    By azjherben in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-05-2009, 07:44 PM
  2. Sombody PLEASE help!! Connect returning -1
    By ddoum9999 in forum C Programming
    Replies: 13
    Last Post: 01-03-2007, 02:27 PM
  3. socket connect returning odd
    By WaterNut in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2005, 08:49 PM
  4. WinSock: Won't connect
    By johnc in forum C Programming
    Replies: 5
    Last Post: 02-24-2003, 10:25 AM
  5. Winsock working only for 127.0.0.1 Cant connect to other PCs
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-03-2002, 04:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21