java - Trying to test connection on localhost ports using Socket and get ConnectException -


A simple test using the socket to test which port is available for TCP service on my local machine Writing java program. Here is my code:

 for  (int i = 0; i & lt; 1024; i ++) {try {// test Availability InetAddress addr = InetAddress.getLocalHost (); Socket s = new socket (adr, i); System.out.println ("port is a server" + "+" localhost. ");} Hold (IOException e) {e.printStackTrace (); breakdown;}}   < p> And when I'm getting:  
  java.net.ConnectException: connect: The address on the local machine is invalid or not valid on the port on the remote machine at java.net.DualStackPlainSocketImpl.connect0 java.net.DualStackPlainSocketImpl.socketConnect (java.net) on java.net.AbstractPlainSocketImpl.doCannect (unknown source) java.net.AbstractPlainSocketImpl.connectToAddress (Unknown Source) java.net.AbstractPlainSocketImpl.connect (unknown source) java.net .java.net (Unknown Source) at PlainSocketImpl.connect .SocksSocketImpl.connect (unknown source) on java.net.Socket.connect (unknown source) Launch Java at .net.Socket.java.net.Socket (unknown source). & Lt; init & gt; (unknown Source) at java.net.Socket. & Lt; init & gt; (unknown source) on main.Portscreen.main (PortScanner.java: 19)   

I'm new to socket And do I need to start a socket server in response to my search? Or what should I really do to scan my ports? Thanks for your reply.

You are trying to establish a client connection from port 0 that is not possible. If you bind the server side socket to port 0, then the system will automatically give an independent port above 1024. Thus a server socket will never listen to port 0. Unfortunately I did not find a document about this. Other operating systems may behave differently.

Your second question: If you are turning off a port you will get an exception.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -