Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Socket not working well

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Socket not working well

    Hi everyone,

    I'm trying to do a PortScanner that really works, I've searched some tutorials about a PortScanner in java at google and I noted that the basic ideia that people use is:

    1-Create a Socket object with host and port;
    2-If the port is open you close the socket, else the IOException will catch it.

    The big deal is that I tryed this code and it didnt works, it says that are ports opened in my machine when they werent opened. So I decide a different way as this:

    PHP Code:

    try {  
      
            
    Socket clientSocket = new Socket("127.0.0.1"2020);  
            
    clientSocket.setSoTimeout(3000);  
      
            
    DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());  
      
            
    // Issue is here :(  
            
    System.out.println(""+inFromServer.readInt());  
      
            
    clientSocket.close();  
      
      
        } catch (
    SocketTimeoutException ex) {  
            
    System.out.println("Timeout exceed");  
        } catch (
    ConnectException ex) {  
            
    System.out.println("Port closed");  
        } catch (
    IOException e) {  
            
    System.out.println("Doesnt connect");  
        }  
      

    But it's not working well too. If the host doesnt have a port open at 2020 it works fine, the method readInt() return me -1, but when the port is opened, the method readInt(); return nothing to me and my code get freeze because it's waiting for a response of the host.

    Does anybody know how can i fix this? Or an other better way to code a Port Scanner?

    Thanks everybody!
    Last edited by tunai; September 26th, 2011 at 05:28 PM.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Socket not working well

    It doesn't timeout?

    I doubt there's anything in the Sockety documentation which says "you can use this method to test for open ports". There are a lot of methods in the Socket API which test for all sorts of other things. I would say if it's possible to accomplish this task at all, it's a matter of exhaustively testing all the likely-looking methods until you find a set of methods which together allow you to give the right output (closed or open) for a training set.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Socket not working well

    Quote Originally Posted by Sean4u View Post
    It doesn't timeout?

    I doubt there's anything in the Sockety documentation which says "you can use this method to test for open ports". There are a lot of methods in the Socket API which test for all sorts of other things. I would say if it's possible to accomplish this task at all, it's a matter of exhaustively testing all the likely-looking methods until you find a set of methods which together allow you to give the right output (closed or open) for a training set.
    Yes, it does the timeout but the problem is, if the socket connect in the server and the server doesn't send a message the timeout limit exceed.

    As I said I just found a lot fo tutorials about port scanner and all of them use socket to test. Maybe there is a better way to do it, I would like to know it.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Socket not working well

    you might want to try this:
    for(int port=1;port<=65535;port++){
     try{
      Socket s = new Socket("127.0.0.1",port);
     } catch(IOException e){
        System.out.println("port " + port + " is being used.");
       }
    }

  5. #5

    Default Re: Socket not working well

    You should be using another method to test for port availability. Try 123099's method but include a DatagramSocket to chekc for UDP availability as well.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Socket NIO connect not working under Solaris 10
    By trojanfoe in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 4th, 2011, 02:48 AM
  2. socket programming
    By advjava in forum Member Introductions
    Replies: 3
    Last Post: April 5th, 2011, 11:21 PM
  3. windowsize TCP socket
    By ra65ma in forum Java Networking
    Replies: 0
    Last Post: January 29th, 2011, 11:58 AM
  4. Socket C
    By ighor10 in forum Java Networking
    Replies: 0
    Last Post: June 22nd, 2010, 06:56 AM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM