Link to home
Start Free TrialLog in
Avatar of kapot
kapot

asked on

How to check tcp port

Hi,

Anyone know a good function to check if an TCP port is open or not?

Open means that the port is listening.

PS: Indy 9 is preferred.
Avatar of ping_it
ping_it
Flag of Italy image

The port to be in Listening state should have an application that "opens" it.

You can check them with netstat -a or netstat -no

If no program is really opening a port, the port is not on the NETSTAT output.

If you want to check whether a port is really blocked and so, even if an application tries to open a port, this port is not reachable from the outside, you can make some port check from the internet. --- The remote server will try to connect to a range of ports you want and it will check the status of the port.

Please let me know if it is clear enough
Avatar of kapot
kapot

ASKER

I know in theory how to test if a port is open or not ... by using TELNET.

For example to test if port 233 is open or not, I would start a cmd prompt and type:

telnet   ipaddress   233

if it is open then you will not get connection refused :)

But the thing is that I dont know how to code this in Delphi :(
No, if the port is open with telnet you get a "black screen" or something will be written, depending on the listening application.

If the port is not listening there will be: connection failed

The "connection refused" there will be if there is some firewall that refuses the connection.

Firewalls can "drop" or "reject" the connections, in the first case it is connection failed and in the second is refused.
Avatar of kapot

ASKER

Yes, thats what I want to tell you ... the black screen.
Avatar of kapot

ASKER

I just need a simple function.
ASKER CERTIFIED SOLUTION
Avatar of Johnjces
Johnjces
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Should have been a bit more clear.

It also uses the events OnConnect, OnDisconnect and OnError of the TClientSocket component.

John
Avatar of aikimark
I think you actually need to open the port with a TCP/IP connection.  In many corporate environments, ICMP (ping) is blocked.

Even then, there are two potential problems:
* firewalls prevent the opening of the port by the program
* the PC network configuration does not have a particular port in the active list.


aikimark,

Not certain where the author was mentioning an ICMP ping. My code does not use ICMP other than the word GoodPing. The TCLientSocket is a TCP/IP component and attempts to make a successful TCP/IP connection with the selected port number.

However and of course, blocked ports would not allow any kind of a response and would show closed.

John
@John

The general question was about a port being 'open'.  I interpreted some of the comments in this thread as indicating that any testing of the port might suffice.  My comment was not really addressing any specific comment, rather stating that the specific protocol (assumed it would be TCP) would need to be used.  I probably should have mentioned in my earlier comment that while a port might receive UDP packets, it might not accept TCP handshakes/sessions or packets.

IMHO:
I think it is somewhat misleading for you to have lines in your code snippet like:
    GoodPing := False;
since you aren't really 'pinging' the port.  I think the variable might be more self-documenting if it were something like IsPortOpen or PortIsAvailable.  I'll certainly grant you that your code is probably the best solution posted to date and that a single variable name quibble is rather nit-picky on my part.  

I can't say for certain that I thoroughly read your code before posting my comment and that I might have just seen the GoodPing value assignment and thought that you were actually pinging (when you really weren't).  If I did, I appologize -- both your comment(s) and code deserve sufficient attention and consideration.
@Aikimark,

Your thoughts and points are well taken and thanks for the response.

John
@John

If you are a fan of classic comedy albums:
"Sped reddin im-provs com-prenshun wun-der-full-e" :-)
@kapot

Did any of this help you?

John
Avatar of kapot

ASKER

Hi John,

I am sorry for the late reply, but yes ... your solution is what I needed.

Very good and clear for learning purposes.

Thanks :)