• Post Reply Bookmark Topic Watch Topic
  • New Topic

Port Scanner. Trying to input.nextInt for ports to scan.  RSS feed

 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a source for a port scanner, and I am trying to ask user for input to which port they would like to see if opened or closed.
My mind is fried from coding all weekend getting ready for a pop up final. Teacher won't tell us beforehand what the final will be, but will require more than 200 lines of code and due in 2 1/2 hours. Crazy huh?
Anyway, here is what I have so far.



So I am trying to get the scanner to only scan what the user wants.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example input format you show is 0-80.
Therefore you need to read the first int (the 0 in your example), then a hyphen (which you discard) and then the second int (the 80 in your example).
If you assign the two int values to different variables you can then use those variables in your for loop instead of 0 and 65535.
 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is what I have so far, and it does not work quite the way I want it to. It only scans a single port from input.nextLine.





 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You split the string into an array with two entries. You use the first entry (parts[0]) -- sort of -- but you don't use the second entry, which would be parts[1].

I said "sort of" back there because you don't use the first entry for what it's supposed to be used for. Think about it -- you asked the user which range of ports you should scan. Now, which is the line of code where you say which ports to scan?
 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now, which is the line of code where you say which ports to scan?



That would be it. I appreciate you calmly helping me. This is not part of my final, but my teacher expects us to know all 16 chapters of the book when we only covered 6 chapters in 14 weeks. Doesn't make sense does it? But there you go.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I'm not making myself clear. Sorry about that. What you posted there is where your code asks the user which ports to scan. What I meant to ask was, which line of your code controls the scanning of those ports?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Carter wrote:That would be it. I appreciate you calmly helping me.

That's our usual aim. :-)

One thing to think about: What if your "user" just enters "0"? Or "35-20"? Or indeed: "@x%-a6"?

Most people can write code that works when everything goes OK; a good programmer writes code that works when they don't.

You might want to read the UserInput page if you have time, and also (hint) think about entering each end of your range separately.

HIH

Winston
 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Stephen Carter wrote:That would be it. I appreciate you calmly helping me.

That's our usual aim. :-)

One thing to think about: What if your "user" just enters "0"? Or "35-20"? Or indeed: "@x%-a6"?

Most people can write code that works when everything goes OK; a good programmer writes code that works when they don't.

You might want to read the UserInput page if you have time, and also (hint) think about entering each end of your range separately.

HIH

Winston


Wow! That got me going on the right track! Here is the revised WORKING code. Thanks!!!

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Carter wrote:Wow! That got me going on the right track! Here is the revised WORKING code. Thanks!!!

Glad we could help. However, I still don't see anything that stops a user from entering "70000-70004" (although you may get an exception when you actually try to scan those ports), and an invalid range of valid numbers (eg, "35-20") will simply result in nothing happening.

If that's fine then your job is done, but it might be worth documenting the behaviour for bad input values.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!