aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes how to check for open ports of remote computer in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "how to check for open ports of remote computer in java" Watch "how to check for open ports of remote computer in java" New topic
Author

how to check for open ports of remote computer in java

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
i m trying to search for open ports in my computer here is the code:



but now i want to search for open ports on any other remote computer, how can i do this??
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Be aware that port scans on networks you don't own -or are otherwise allowed to probe- are generally considered an attack, and might have consequences.

What particular difficulty have had when you tried adapting the code to access some other IP address? What did you try?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2

Be aware that port scans on networks you don't own -or are otherwise allowed to probe- are generally considered an attack, and might have consequences.


i will try on my friends computer.


Socket ServerSok = new Socket("127.0.0.1",i);
i just changed the ipaddress in here, but it not work, showing me error:

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine


Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
do i need to create one client class also that will accept the connection??
but if i create, that time i have to wait until and unless client accept the connection.
but i only want to check for open ports don't want to wait until client accept the connections.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Punit Jain wrote:

What particular difficulty have had when you tried adapting the code to access some other IP address? What did you try?

Socket ServerSok = new Socket("127.0.0.1",i);
i just changed the ipaddress in here, but it not work, showing me error:

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine


Not sure of the point that you are making here. You are looking for open ports, ports where there isn't any application using them. In that regard, shouldn't you be expecting connection exceptions?

Or do you have a different definition of an open port?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
yes i m looking for the same, open ports for remote computer.
but how do i do that, i mean do i simply need to change the ip address (that i did in the code), but that's an exception.
i want is that i run this program on my computer, and if change the ip address (say, i put ip address of my friend machine), that time it should show me, how many ports are opened and close in that machine...?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:i m trying to search for open ports in my computer


Why?

What are you actually trying to accomplish?

And what exactly do you mean by "open"? There are two definitions I can think of, and they are polar opposites. One definition would be "available," as in "not currently in use." The other would be "some application has opened a server socket on that port and is listening for connections."
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2

Why?

What are you actually trying to accomplish?


not any specific reason, i m doing this only as a learning exercise.
and by open i mean by this:

One definition would be "available," as in "not currently in use."
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:

Why?

What are you actually trying to accomplish?


not any specific reason, i m doing this only as a learning exercise.
and by open i mean by this:

One definition would be "available," as in "not currently in use."


You can detect if a port on a remote machine doesn't have a TCP socket listening on it by trying to connect. If it fails, no socket. You can't tell whether there's a UDP listener there or not. However, there's not really much use in doing so, since, as already pointed out, this is generally considered a hostile act.

This isn't a very useful learning exercise. What do you think you'll learn by doing it that you can't learn by writing a more realistic program?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2

You can detect if a port on a remote machine doesn't have a TCP socket listening on it by trying to connect. If it fails, no socket. You can't tell whether there's a UDP listener there or not.

but how to do this??

This isn't a very useful learning exercise. What do you think you'll learn by doing it that you can't learn by writing a more realistic program?

well i am just trying to write an application, and if i install that application i will be able to check all the open and close ports of any machine, just like a port scanner.
it would be great if you suggest anything else??
thank you
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:
You can detect if a port on a remote machine doesn't have a TCP socket listening on it by trying to connect. If it fails, no socket. You can't tell whether there's a UDP listener there or not.

but how to do this??

What do you mean? I know that you know how to establish a connection to a socket, as you have done this in code in a previous question you posted.



This isn't a very useful learning exercise. What do you think you'll learn by doing it that you can't learn by writing a more realistic program?

well i am just trying to write an application, and if i install that application i will be able to check all the open and close ports of any machine, just like a port scanner.


That doesn't actually answer my question. You're saying you want to write a port scanner so you can write a port scanner.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2


This works fine.
but when i am running and searching for open ports on localhost, it works good, but when i give ip of remote machine, the speed of searching ports gets very much slow???
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Creating 64K socket connections to a remote machine sequentially? And you're seriously wondering why it takes quite a while? Are you familiar with computer and network architectures so you can appreciate the difference between a local and a remote connection?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2

Creating 64K socket connections to a remote machine sequentially?

don't know what you mean by this.


Are you familiar with computer and network architectures so you can appreciate the difference between a local and a remote connection?

No.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:

Creating 64K socket connections to a remote machine sequentially?

don't know what you mean by this.


He means this:



Are you familiar with computer and network architectures so you can appreciate the difference between a local and a remote connection?

No.

Well, then just apply a little common sense. Are you surprised that it takes longer to make 64k connection requests to a remote computer than to localhost? Even in general you shouldn't be surprised. Now add something that you may not have known: Going to local host can bypass large parts of the TCP/IP stack--it doesn't have to put anything physically on a wire, and can basically just copy bytes from one region of memory to another.

Also consider that most of those ports will be closed, so most of your 64k connection attempts will have to time out. For a localhost attempt, it may be able to bypass that and just ask its own network stack directly if that port is even open.

As we've been trying to tell you all along: This is not a good app to be writing.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
okay..got it..i am establishing connection each time, i should to create connection once and use that only..?

As we've been trying to tell you all along: This is not a good app to be writing.

okay, stopped writing for this..

Thank you...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:okay..got it..i am establishing connection each time, i should to create connection once and use that only..?


No.

Please stop and think for a minute. What do you need to do to connect to a port? You need to establish a connection to that port, using new Socket(). You know that you can't just reuse a connection for multiple ports. Nobody was telling you to try to reuse a connection. Nothing even close to that. We merely pointed out that the behavior you saw is not surprising, and gave you a couple of ideas why.

Please understand, I mean this in the nicest way possible, as constructive criticism to help you be more productive in you learning and coding: You need to stop and think more. You need to consider beyond the first idea that pops into your mind, think about what your goal is, where you are now, and what the steps might be to get from here to there, and if that particular path is actually viable. Right now you seem to just have one vague idea of "I want to learn about networking, so I'll write a port scanner," and then you rush off to type the first code that pops into your head. Slow down, take a step back, and do more thinking, planning, and designing before you start coding. Good luck!
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 995
    
    2
okay..
thank you.....
 
 
subject: how to check for open ports of remote computer in java