Results 1 to 18 of 18
Thread: Threading TCP Socket Connection
- 03-16-2012, 01:22 PM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-17-2012, 01:39 PM #2
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 02:11 PM. Reason: reworded
- 03-17-2012, 09:55 PM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-17-2012, 10:01 PM #4
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.
- 03-17-2012, 10:04 PM #5
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-17-2012, 10:09 PM #6
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.
- 03-17-2012, 10:14 PM #7
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Threading TCP Socket Connection
Sorry, what variables are you talking about? Could you elaborate?
Thanks
- 03-17-2012, 10:15 PM #8
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.
- 03-17-2012, 10:17 PM #9
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Threading TCP Socket Connection
Oh right. Would that be ok if you could provide some sample code, specifically on thread variables?
Thanks
- 03-17-2012, 10:22 PM #10
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.
- 03-18-2012, 01:27 PM #11
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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"); } } }
- 03-18-2012, 01:42 PM #12
Re: Threading TCP Socket Connection
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.where would the variables be placed in the codeIf you don't understand my response, don't ignore it, ask a question.
- 03-18-2012, 02:09 PM #13
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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"); } } }
- 03-18-2012, 02:19 PM #14
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.
- 03-18-2012, 02:43 PM #15
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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 lineshould be changed toJava Code:for(int port=startPort; port<=endPort; port++)
? Therefore the parameter variables are defined in the method?Java Code:for(int port=sP; port<=eP; port++)
Thanks
- 03-18-2012, 02:48 PM #16
Re: Threading TCP Socket Connection
Look at the API doc for the Thread class. There are examples there.how can I call the run method then?
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.
- 03-18-2012, 02:53 PM #17
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.
- 03-18-2012, 03:03 PM #18
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Similar Threads
-
threading in server connection
By niteangell21 in forum New To JavaReplies: 1Last Post: 10-15-2010, 11:06 PM -
Java.net.socket connection :connection closed
By veeru541 in forum Advanced JavaReplies: 2Last Post: 06-27-2010, 02:14 AM -
socket connection in an applet
By j2me64 in forum Java AppletsReplies: 5Last Post: 04-12-2010, 08:34 PM -
Socket Connection problem
By xpan in forum NetworkingReplies: 4Last Post: 02-14-2010, 04:32 PM -
How To Make Socket Connection
By madhumurundi in forum NetworkingReplies: 5Last Post: 04-21-2008, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks