Reputation: 101
freshman, both here and in Java i know codes like:
Socket clientSocket=null;
try
{
clientSocket=new Socket(192.168.2.3,1111);
System.out.println("port open");
clientSocket.close();
}
catch(Exception e)
{
System.out.println("port closed");
}
can be used to test whether certain port is open(here is port 1111 on 192.168.2.3), just wandering is there any other way?? not using socket() maybe??
Upvotes: 0
Views: 248
Reputation: 1076
To simplyfy things a bit: yes, establishing a connection is correct way to probe for open port, and if you even find other method, it will be using socket() under the hood.
Complexity of the whole problem comes from the fact that "open port" is a number one suspect in every security book out there. System administrators use a bunch of tricks to fight so called "port scanning". This includes reporting a "false positive" (claiming that port is open while it is not) to any software that behaves like a port scanner (that is: makes a connection and disconnects without sending any data).
So in reality the best way is to establish connection, send request and process response. Every other approach can fail under certain network/host/security configurations, leaving your user puzzled as to why his application doesn't work when it claims there is no problem with connection.
Upvotes: 4