A
Abraham Khalil
The program tries to find a valid host connection. Each host
connection thread is handle by the inner class DetectSocketConnection.
When you run the program
it has a problem of the wait statement (main thread - SocketSearch)
trying to proceed even when the inner class issues a notify call.
Can someone see what I'am doing wrong here? Need jdk1.4 to run with
java.nio.channels package
Thanks
Abraham Khalil
==============================================================================
import java.io.*;
import java.util.*;
import java.net.*;
import java.nio.channels.*;
/**
* SocketSearch class to scan for an open socket from an array of
sockets passed. <br>
* Inner class DetectSocketConnection tries to open the socket and
assign hostname
* a value if successful. <br>
* The SocketSearch thread class is waiting to find the hostname or
active DetectSocketConnection
* threads to be zero - i.e No hosts found
*/
public class SocketSearch extends Thread {
private Object syncObject = new Object();
private String hostname = null;
private ThreadGroup threadGroup = new
ThreadGroup("SOCKETS_CONNECT");
private ArrayList sockets = null;
/**
* Inner class DetectSocketConnection to find an open socket. <br>
* It will assign the hostname a value if successful
*/
private class DetectSocketConnection extends Thread {
private SocketChannel sc = null;
private String host = null;
private int port = -1;
public DetectSocketConnection(String host, int port) {
super(threadGroup, host);
this.host = host;
this.port = port;
System.out.println("DetectSocketConnection::syncObject = " +
syncObject);
}
public void run() {
System.out.println("Scanning " + host + " at port number " +
port);
synchronized(syncObject) {
try {
InetSocketAddress isa = new
InetSocketAddress(InetAddress.getByName(host), port);
// Connect
sc = SocketChannel.open();
sc.connect(isa);
System.out.println("Found hostname: " + host +
"!!!!!!!!!!!!!!!!!!!");
hostname = host;
}
catch (IOException e) {
System.err.println("DetectSocketConnection: " +
e.toString());
}
finally {
// Make sure we close the channel (and hence the socket)
close();
System.out.println("DetectSocketSonnectio:
notify()...");
syncObject.notify();
}
}
}
public void close() {
try {
if (sc != null) sc.close();
}
catch (IOException e) {
}
}
}
public SocketSearch() {
sockets = new ArrayList();
}
/**
* Add socket to the sockets ArrayList to prepare to start the
Socket Search
*
* @param host Socket hostname
* @param port Socket port number
*/
public void addSocket(String host, int port) {
DetectSocketConnection detectSocket = new
DetectSocketConnection(host, port);
sockets.add(detectSocket);
}
/**
* SocketSearch start method to fire up the sockets threads to
search for
*/
public void start() {
super.start();
if (sockets != null) {
DetectSocketConnection[] arrSockets =
(DetectSocketConnection[]) sockets.toArray(new
DetectSocketConnection[0]);
for (int i = 0 ; i < arrSockets.length ; i++) {
arrSockets.start();
}
}
}
/**
* Main code to do the socket search
*/
public void run() {
try {
boolean loop = (sockets.size() > 0);
while (loop) {
synchronized(syncObject) {
System.out.println("SocketSearch.wait() => syncObject =
" + syncObject);
syncObject.wait();
System.out.println("SocketSearch.wait() => syncObject =
" + syncObject + ", finished...");
if (hostname != null) {
// use the hostname
// you could interrupt the threads now - its your
choice
loop = false;
}
else {
System.out.println("Invalid hostname...");
ThreadGroup currentGroup =
Thread.currentThread().getThreadGroup();
int numThreads = currentGroup.activeCount();
Thread[] listOfThreads = new Thread[numThreads];
currentGroup.enumerate(listOfThreads);
int activeThreads = 0;
for (int i = 0 ; i < numThreads ; i++) {
if (listOfThreads instanceof
DetectSocketConnection) {
activeThreads++;
}
}
System.out.println("activeThreads: " +
activeThreads);
if (activeThreads == 0) {
// host name is NULL and active threads are
finished
throw new UnknownHostException("Host not
found.");
}
}
}
}
if (hostname != null) {
System.out.println("Found hostname: " + hostname);
// Do something here, use callback to maybe dispose
ProgressDialog etc...
}
}
catch (UnknownHostException e) {
// Do something here, use callback to maybe dispose
ProgressDialog etc...
System.err.println(e.toString());
}
catch (InterruptedException e) {
// Do something here, use callback to maybe dispose
ProgressDialog etc...
System.err.println(e.toString());
}
}
/**
* Test to find a socket from an array of test socket hosts
*/
public static void main(String args[]) {
String[] yahooPOP3PossibleSettings = new String[] {
"yahoo.com",
"mail.yahoo.com",
"pop.yahoo.com",
"pop.mail.yahoo.com",
"pop3.yahoo.com"
};
int PORT_NUMBER = 110;
SocketSearch socketSearch = new SocketSearch();
for (int i = 0 ; i < yahooPOP3PossibleSettings.length ; i++) {
socketSearch.addSocket(yahooPOP3PossibleSettings,
PORT_NUMBER);
}
socketSearch.start();
}
}
connection thread is handle by the inner class DetectSocketConnection.
When you run the program
it has a problem of the wait statement (main thread - SocketSearch)
trying to proceed even when the inner class issues a notify call.
Can someone see what I'am doing wrong here? Need jdk1.4 to run with
java.nio.channels package
Thanks
Abraham Khalil
==============================================================================
import java.io.*;
import java.util.*;
import java.net.*;
import java.nio.channels.*;
/**
* SocketSearch class to scan for an open socket from an array of
sockets passed. <br>
* Inner class DetectSocketConnection tries to open the socket and
assign hostname
* a value if successful. <br>
* The SocketSearch thread class is waiting to find the hostname or
active DetectSocketConnection
* threads to be zero - i.e No hosts found
*/
public class SocketSearch extends Thread {
private Object syncObject = new Object();
private String hostname = null;
private ThreadGroup threadGroup = new
ThreadGroup("SOCKETS_CONNECT");
private ArrayList sockets = null;
/**
* Inner class DetectSocketConnection to find an open socket. <br>
* It will assign the hostname a value if successful
*/
private class DetectSocketConnection extends Thread {
private SocketChannel sc = null;
private String host = null;
private int port = -1;
public DetectSocketConnection(String host, int port) {
super(threadGroup, host);
this.host = host;
this.port = port;
System.out.println("DetectSocketConnection::syncObject = " +
syncObject);
}
public void run() {
System.out.println("Scanning " + host + " at port number " +
port);
synchronized(syncObject) {
try {
InetSocketAddress isa = new
InetSocketAddress(InetAddress.getByName(host), port);
// Connect
sc = SocketChannel.open();
sc.connect(isa);
System.out.println("Found hostname: " + host +
"!!!!!!!!!!!!!!!!!!!");
hostname = host;
}
catch (IOException e) {
System.err.println("DetectSocketConnection: " +
e.toString());
}
finally {
// Make sure we close the channel (and hence the socket)
close();
System.out.println("DetectSocketSonnectio:
notify()...");
syncObject.notify();
}
}
}
public void close() {
try {
if (sc != null) sc.close();
}
catch (IOException e) {
}
}
}
public SocketSearch() {
sockets = new ArrayList();
}
/**
* Add socket to the sockets ArrayList to prepare to start the
Socket Search
*
* @param host Socket hostname
* @param port Socket port number
*/
public void addSocket(String host, int port) {
DetectSocketConnection detectSocket = new
DetectSocketConnection(host, port);
sockets.add(detectSocket);
}
/**
* SocketSearch start method to fire up the sockets threads to
search for
*/
public void start() {
super.start();
if (sockets != null) {
DetectSocketConnection[] arrSockets =
(DetectSocketConnection[]) sockets.toArray(new
DetectSocketConnection[0]);
for (int i = 0 ; i < arrSockets.length ; i++) {
arrSockets.start();
}
}
}
/**
* Main code to do the socket search
*/
public void run() {
try {
boolean loop = (sockets.size() > 0);
while (loop) {
synchronized(syncObject) {
System.out.println("SocketSearch.wait() => syncObject =
" + syncObject);
syncObject.wait();
System.out.println("SocketSearch.wait() => syncObject =
" + syncObject + ", finished...");
if (hostname != null) {
// use the hostname
// you could interrupt the threads now - its your
choice
loop = false;
}
else {
System.out.println("Invalid hostname...");
ThreadGroup currentGroup =
Thread.currentThread().getThreadGroup();
int numThreads = currentGroup.activeCount();
Thread[] listOfThreads = new Thread[numThreads];
currentGroup.enumerate(listOfThreads);
int activeThreads = 0;
for (int i = 0 ; i < numThreads ; i++) {
if (listOfThreads instanceof
DetectSocketConnection) {
activeThreads++;
}
}
System.out.println("activeThreads: " +
activeThreads);
if (activeThreads == 0) {
// host name is NULL and active threads are
finished
throw new UnknownHostException("Host not
found.");
}
}
}
}
if (hostname != null) {
System.out.println("Found hostname: " + hostname);
// Do something here, use callback to maybe dispose
ProgressDialog etc...
}
}
catch (UnknownHostException e) {
// Do something here, use callback to maybe dispose
ProgressDialog etc...
System.err.println(e.toString());
}
catch (InterruptedException e) {
// Do something here, use callback to maybe dispose
ProgressDialog etc...
System.err.println(e.toString());
}
}
/**
* Test to find a socket from an array of test socket hosts
*/
public static void main(String args[]) {
String[] yahooPOP3PossibleSettings = new String[] {
"yahoo.com",
"mail.yahoo.com",
"pop.yahoo.com",
"pop.mail.yahoo.com",
"pop3.yahoo.com"
};
int PORT_NUMBER = 110;
SocketSearch socketSearch = new SocketSearch();
for (int i = 0 ; i < yahooPOP3PossibleSettings.length ; i++) {
socketSearch.addSocket(yahooPOP3PossibleSettings,
PORT_NUMBER);
}
socketSearch.start();
}
}