Results 1 to 18 of 18
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Threading TCP Socket Connection

    Hi,

    When you create a socket connection to scan all (65535) TCP ports to ensure if they are open or not, the connection to all ports is really time consuming (at least taking hours to complete, if even it may stop). I was thinking threading some of the socket connections in groups (dividing 65535 into a separate groups) to increase the speed of scanning all ports. How can I get around doing this?

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    Use multiple threads could help if the work was taking place off the PC, IE on the internet. The waiting for off PC work to complete could be done in parallel with multiple threads. I say could because I don't know for sure. It would be worth a test.
    Last edited by Norm; 03-17-2012 at 03:11 PM. Reason: reworded

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    Oh yeah I understand. Sorry I was suppose to say that I'm scanning ports on the local host (my computer) for TCP ports. See I understand the best possible option is threading, but have no idea how to divide 65535 into many groups of threads, in order to scan all.
    Thanks

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    If all the work is being done on your PC, I'm not sure where the overlap of work between threads would be.
    Try a test using a few threads and divide the number of threads into the total number of ports to get the number of ports each thread is to scan.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    Ok. Would I override the run() method, and have threads using this method for initiating all threads at once? Or Am I missing the point?
    Thanks

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    If the method doesn't change any class variables it could be called by all the threads. Each thread would have its own local variables.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    Sorry, what variables are you talking about? Could you elaborate?
    Thanks

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    Variables that are defined outside of the method you want to call in the threads.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    Oh right. Would that be ok if you could provide some sample code, specifically on thread variables?
    Thanks

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    Can you explain what you mean by "thread variables"?
    All code executes on some thread. If you create some extra threads that execute methods that try to update a class variable at the same time there will be a problem. Hence my warning about not trying to update any class variables from a method that is being executed on more than one thread at the same time.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    Ok. I've provided the method to scan all TCP ports below. So where would the variables be placed in the code, in order to run separate threads? Or am i getting the wrong concept?
    Thanks

    Java Code:
    public void testTCPPorts()
    	{
    		boolean anyPortsOpen = false;
    		//for loop to loop through all possible ports and..
    		for(int port=1; port<=65535; port++)
    		{	
    			//try to..
    			try
    			{
    				//Create a socket to connect to that port 
    				//on the host name privided
    				connecter = new Socket(hostName, port);
    				//Print out the opened port
    				System.out.println("Port "+port+" is opened");
    				//close the port
             	connecter.close();
    			}
             catch(IOException ex) 
    			{
               	//System.out.println("This port closed");
    			}
    		}
    	}

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    where would the variables be placed in the code
    Define all of the variables used by the method inside of the method. Pass the range of ports the method is to scan to the method as a parameter.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    Ok. Now I've provided two parameters, which then are assigned into the two instance fields (class variables). Now, where do I go about running this method into multiple threads?
    Thanks

    Java Code:
    //Scans for a range of ports determined from the user
    	public void testTCPPorts(int sP, int eP)
    	{
    		//Assign the two parameters to the two instance fields
    		startPort = sP;
    		endPort = eP;
    		
    		//For loop through the range of chose ports and..
    		for(int port=startPort; port<=endPort; port++)
    		{ 
    			try
    			{
    				//Create a socket to connect to each port 
    				//seperatly to the host name
    				connecter = new Socket(hostName, port);
    				//Print out each opened port
    				System.out.println("Port "+port+" is opened");
    				//Close the port
    				connecter.close();
    				goodInput = true;
    
    			}				
                            //Catch the exception when trying to connect to a closed port
    			catch(IOException ex)
    			{
    				//System.out.println("This port closed");
    			}
    	
    		}
    	}

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    Create a thread and call the method from the thread's run method.

    The method is using four variables that are not defined in the method: startPort, endPort, connector and goodInput
    All variables that are changed need to be defined in the method.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    But the method is called testTCPPorts(int, int), so how can I call the run method then?
    Sorry, that 'goodInput = true;' should not be there.
    So the line
    Java Code:
    for(int port=startPort; port<=endPort; port++)
    should be changed to
    Java Code:
    for(int port=sP; port<=eP; port++)
    ? Therefore the parameter variables are defined in the method?
    Thanks

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    how can I call the run method then?
    Look at the API doc for the Thread class. There are examples there.
    Also look at the tutorial: Thread Objects (The Java™ Tutorials > Essential Classes > Concurrency)
    The thread calls the run method which then should call your method.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Threading TCP Socket Connection

    A suggestion: Write a small simple program that creates some threads, starts each thread and have the thread's run() method call a method in the class that prints a message.
    This will show you how to write and use threads.
    When you get this testing program to work, then you can use the techniques you learned with the port scanning program.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Threading TCP Socket Connection

    Ok thanks, I'l try to some sample thread testing and see from there. If I have any other queries I'll post them here.
    Thanks

Similar Threads

  1. threading in server connection
    By niteangell21 in forum New To Java
    Replies: 1
    Last Post: 10-16-2010, 12:06 AM
  2. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 03:14 AM
  3. socket connection in an applet
    By j2me64 in forum Java Applets
    Replies: 5
    Last Post: 04-12-2010, 09:34 PM
  4. Socket Connection problem
    By xpan in forum Networking
    Replies: 4
    Last Post: 02-14-2010, 05:32 PM
  5. How To Make Socket Connection
    By madhumurundi in forum Networking
    Replies: 5
    Last Post: 04-21-2008, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •