Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to check tcp port

Posted on 2009-04-10
15
Medium Priority
?
2,222 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:kapot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 5

Expert Comment

by:ping_it
ID: 24114342
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
0
 

Author Comment

by:kapot
ID: 24114926
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 :(
0
 
LVL 5

Expert Comment

by:ping_it
ID: 24114960
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:kapot
ID: 24115158
Yes, thats what I want to tell you ... the black screen.
0
 
LVL 5

Expert Comment

by:ping_it
ID: 24115304
0
 

Author Comment

by:kapot
ID: 24115491
I just need a simple function.
0
 
LVL 18

Accepted Solution

by:
Johnjces earned 800 total points
ID: 24116311
This uses ClientSocket but could be done with Indy as well.

It also uses the events OnConnect, OnDisconnect and OnError.

OnDisconnect it sets a true boolean value 'Done'.
OnConnect it sets a true boolean value of 'GoodPing'.
OnError it does the same but GoodPing := False.

A bit 'messy' but gets the job done. Now, I cut and pasted this from some code that I do use, so this exact snippet is not tested, but should work just fine. In any event it will point you in the right direction.

John
{Global var}
var
 Done, GoodPing : Boolean.
 
function IsPortOpen(HostIP, Port : String) : Boolean;
begin
 Result := False;
 if (StrToIntDef(Port, -1) > -1) and (StrToIntDef(Port, -1) < 65536) then
  begin
   ClientSocket1.Active := False;
   ClientSocket1.Address := HostIP;
   ClientSocket1.Port := StrToInt(Port);
   ClientSocket1.Open;
   repeat
     Application.ProcessMessages
       until
         Done;
  end;
 Result := GoodPing;
end;
 
procedure ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 GoodPing := True;
 ClientSocket1.Close;
end;
 
procedure ClientSocket1Disconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 Done := True;
end;
 
procedure ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
 ErrorCode := 0; // So no error is displayed
 GoodPing := False;
 ClientSocket1.Close;
 Done := True;
end;

Open in new window

0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24116319
Should have been a bit more clear.

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

John
0
 
LVL 46

Expert Comment

by:aikimark
ID: 24120294
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.


0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24120417
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
0
 
LVL 46

Expert Comment

by:aikimark
ID: 24120721
@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.
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 24120860
@Aikimark,

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

John
0
 
LVL 46

Expert Comment

by:aikimark
ID: 24121363
@John

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

Expert Comment

by:Johnjces
ID: 24132043
@kapot

Did any of this help you?

John
0
 

Author Comment

by:kapot
ID: 24191437
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 :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question