Google

Wednesday, February 11, 2009

IP Scanner 002 - using ExecutorService (concurrent GUI)



/*
* GUI version of IPScanner
* Scans IP networks using ping
* Concurrent GUI update
*/
package ipscannergui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.concurrent.*;
import javax.swing.*;

public class IPScannerGui extends JFrame {

private JButton btnStart;
private JTextField tfIP;
private JLabel lbIP;
private JTextArea taOutput;
private JScrollPane jsp;

public static void main(String[] args) {
IPScannerGui sg = new IPScannerGui();
sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sg.setSize(500, 400);
sg.setVisible(true);

}

public IPScannerGui() {
super("IP Scanner 002");
setLayout(new FlowLayout()); // set frame layout
lbIP = new JLabel("Enter network (eg 118.100.118)");
tfIP = new JTextField("118.100.118", 20);
btnStart = new JButton("Start");
taOutput = new JTextArea(20, 40);
jsp = new JScrollPane(taOutput);
add(lbIP);
add(tfIP);
add(btnStart);
add(jsp);
ButtonHandler handler = new ButtonHandler();
btnStart.addActionListener(handler);

}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStart) {
IPScanner[] ips = new IPScanner[254];

ExecutorService runner = Executors.newFixedThreadPool(254);
for (int i = 1; i < 20="=" ip =" ipadd;" p =" Runtime.getRuntime().exec(" input =" new" line =" input.readLine())"> -1) {
//System.out.println(line);
final String r = line + "\n";
SwingUtilities.invokeLater(new Runnable() {

public void run() {
taOutput.append(r);
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}

}
}
}

Monday, February 9, 2009

IP Scanner 001 - Multi-threading but delayed GUI update



/*
* GUI version of IPScanner
* Scans IP networks using ping
* by Paul Chin, Feb 9, 2009
*/
package ipscannergui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;

public class IPScannerGui extends JFrame {

private JButton btnStart;
private JTextField tfIP;
private JLabel lbIP;
private JTextArea taOutput;
private JScrollPane jsp;

public static void main(String[] args) {
IPScannerGui sg = new IPScannerGui();
sg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sg.setSize(500, 400);
sg.setVisible(true);

}

public IPScannerGui() {
super("IP Scanner 001");
setLayout(new FlowLayout()); // set frame layout
lbIP = new JLabel("Enter network (eg 118.100.118)");
tfIP = new JTextField("118.100.118",20);
btnStart = new JButton("Start");
taOutput = new JTextArea(20, 40);
jsp = new JScrollPane(taOutput);
add(lbIP);
add(tfIP);
add(btnStart);
add(jsp);
ButtonHandler handler = new ButtonHandler();
btnStart.addActionListener(handler);
}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStart) {
try {

doScan();
} catch (Exception x) {
x.printStackTrace();
}
}
}
}

private void doScan() throws InterruptedException {
Thread[] t = new Thread[255];
MyScanner[] ms = new MyScanner[255];
String net= tfIP.getText() + "."; //Construct net addr eg 192.168.1.
for (int i = 1; i < ip =" ip;" taresult =" (JTextArea)" p =" Runtime.getRuntime().exec(" input =" new" line =" input.readLine())"> -1) {
System.out.println(line);
taResult.append(line + "\n");
}
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

IP Scanner

/*
* Scans IP networks using ping
* Substitute your target net address in String net
* by Paul Chin, Feb 9, 2009
*/
package ipscanner;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class IPScanner {

public static void main(String[] args) throws InterruptedException {
IPScanner ips = new IPScanner();
ips.doScan();
}

private void doScan() throws InterruptedException {
Thread[] t = new Thread[255];
MyScanner[] ms = new MyScanner[255];
String net = "118.100.118.";
for (int i = 1; i < 255; i++) {
ms[i] = new MyScanner(net + i);
t[i] = new Thread(ms[i]);
t[i].start();
Thread.sleep(100);
}
}
}

class MyScanner implements Runnable {

String ip;

public MyScanner(String ip) {
this.ip = ip;
}

public void run() {
try {
String line;
Process p = Runtime.getRuntime().exec("cmd /c ping " + ip + " -n 1");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
if (line.indexOf("Reply") > -1) {
System.out.println(line);
}
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Bluetooth J2ME Server, J2SE Server and J2ME Client - How to Use

In the previous 3 posts, I put up the sources for the following:

1. J2ME Server
2. J2ME Client
3. J2SE Server

How to Use:

Every client communicates with a server.
Server should always listen first, then, only start the
Client.

Therefore, you can use the following combinations:

A. J2ME Server <--> J2ME Client (mobile phone to mobile phone)
B. J2SE Server <--> J2ME Client (PC to mobile phone, or vice versa)

Scenario A can be run on PC in emulator mode for both Server and Client, or,
both Server and Client run on Mobile Phone.Both (Server and Client) must be run
on PC Emultor, or, both transferred to Mobile phone and run from two mobile phones.
One Mobile Phone will run the Server whilst the second Mobile Phone run the client.

Scenario B, Server must be run on PC whilst Client must be run on real Mobile phone.
In order to run J2SE server you must install the Bluecove library first. This library enables
you to access your PC's Bluetooth hardware. As such when you run Scenario B, you are
actually using the PC's real Bluetooth hardware to communcate with a real mobile Phone.

Summary:
To communicate between two mobile phones, use Scenario A
To communicate between PC and mobile phone (or vice versa), use Scenario B

Bluetooth J2SE Server

Use this server with the J2ME Client

/*
* J2SE Server - to start listening from for connections from
* Mobile Phones and receive a string and display it
*/
package j2seserver;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class J2SEServer {

public static void main(String[] ags) throws BluetoothStateException, IOException {
new J2SEServer().startServer();
}

private void startServer() throws BluetoothStateException, IOException {
LocalDevice local = LocalDevice.getLocalDevice();
if (!local.setDiscoverable(DiscoveryAgent.GIAC)) {
System.out.println("Failed to change to the " + "discoverable mode");
}

// Create a server connection object to accept
// a connection from a client
StreamConnectionNotifier notifier =
(StreamConnectionNotifier) Connector.open("btspp://localhost:" +
"86b4d249fb8844d6a756ec265dd1f6a3");

// Accept a connection from the client
StreamConnection conn = notifier.acceptAndOpen();

// Open the input to read data from
InputStream in = conn.openInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();

// Read the data sent from the client until
// the end of stream
int data;
while ((data = in.read()) != -1) {
out.write(data);
}

System.out.println(out.toString());
}
}

Bluetooth J2ME Client

Use this client with either J2SE Server OR J2ME Server

/*
* J2ME Client - to sent a string to the J2SE Server
*/
package j2meclient;

import java.io.OutputStream;
import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class J2MEClientMidlet extends MIDlet implements CommandListener, Runnable {

Display d;
Command cmExit, cmConnect;
Form f;
Thread t;
String connString;

public J2MEClientMidlet() {
f = new Form("Client");
cmExit = new Command("Exit", Command.EXIT, 1);
cmConnect = new Command("Connect", Command.ITEM, 2);

f.addCommand(cmExit);
f.addCommand(cmConnect);
f.setCommandListener(this);
}

public void startApp() {
if (d == null) {
d = Display.getDisplay(this);
d.setCurrent(f);
t = new Thread(this);
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
if (c == cmExit) {
destroyApp(false);
notifyDestroyed();
}
if (c == cmConnect) {
t.start();
}
}

public void run() {
try {
// Retrieve the connection string to connect to
// the server
LocalDevice local =
LocalDevice.getLocalDevice();
DiscoveryAgent agent = local.getDiscoveryAgent();
connString = agent.selectService(
new UUID("86b4d249fb8844d6a756ec265dd1f6a3", false),
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
} catch (Exception e) {
}

if (connString != null) {

try {
// Connect to the server and send 'Hello, World'
StreamConnection conn = (StreamConnection) Connector.open(connString);
OutputStream out = conn.openOutputStream();
Thread.sleep(2000); //2 secs delay necessary to wait for
//Nokia 3120 to open connection
out.write("Hello, World".getBytes());
out.close();
conn.close();
f.append("Message sent correctly");

} catch (Exception ex) {
f.append("IOException: ");
f.append(ex.getMessage());
}
}
else{
f.append("Unable to locate service");
}
}
}