package socket;
import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import java.io.*;
public class socketmidlet extends midlet implements commandlistener { private static final string server = "server"; private static final string client = "client"; private static final string[] names = { server, client }; private static display display; private form f; private choicegroup cg; private boolean ispaused; private server server; private client client; private command exitcommand = new command("exit", command.exit, 1); private command startcommand = new command("start", command.item, 1);
public socketmidlet() { display = display.getdisplay(this); f = new form("socket demo"); cg = new choicegroup("please select peer", choice.exclusive, names,null); f.append(cg);
f.addcommand(exitcommand); f.addcommand(startcommand); f.setcommandlistener(this);
display.setcurrent(f); }
public boolean ispaused() { return ispaused; }
public void startapp() { ispaused = false; }
public void pauseapp() { ispaused = true; }
public void destroyapp(boolean unconditional) { if (server != null) { server.stop(); } if (client != null) { client.stop(); } }
public void commandaction(command c, displayable s) { if (c == exitcommand) { destroyapp(true); notifydestroyed(); } else if (c == startcommand) { string name = cg.getstring(cg.getselectedindex()); if (name.equals(server)) { server = new server(this); server.start(); } else { client = new client(this); client.start(); } } }
}
package socket;
import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import java.io.*;
public class server implements runnable, commandlistener { private socketmidlet parent; private display display; private form f; private stringitem si; private textfield tf; private boolean stop; private command sendcommand = new command("send", command.item, 1); private command exitcommand = new command("exit", command.exit, 1); inputstream is; outputstream os; socketconnection sc; serversocketconnection scn; sender sender; public server(socketmidlet m) { parent = m; display = display.getdisplay(parent); f = new form("socket server"); si = new stringitem("status:", " "); tf = new textfield("send:", "", 30, textfield.any); f.append(si); f.append(tf); f.addcommand(exitcommand); f.setcommandlistener(this); display.setcurrent(f); }
public void start() { thread t = new thread(this); t.start(); }
public void run() { try { si.settext("waiting for connection"); scn = (serversocketconnection) connector.open("socket://:5009");
// wait for a connection. sc = (socketconnection) scn.acceptandopen(); si.settext("connection accepted"); is = sc.openinputstream(); os = sc.openoutputstream(); sender = new sender(os); // allow sending of messages only after sender is created f.addcommand(sendcommand);
while (true) { stringbuffer sb = new stringbuffer(); int c = 0;
while (((c = is.read()) != '/n') && (c != -1)) { sb.append((char) c); }
if (c == -1) { break; } si.settext("message received - " + sb.tostring()); } stop(); si.settext("connection is closed"); f.removecommand(sendcommand); } catch (ioexception ioe) { if (ioe.getmessage().equals("serversocket open")) { alert a = new alert("server", "port 5000 is already taken.",null, alerttype.error); a.settimeout(alert.forever); a.setcommandlistener(this); display.setcurrent(a); } else { if (!stop) { ioe.printstacktrace(); } } } catch (exception e) { e.printstacktrace(); } }
public void commandaction(command c, displayable s) { if (c == sendcommand && !parent.ispaused()) { sender.send(tf.getstring()); }
if ((c == alert.dismiss_command) || (c == exitcommand)) { parent.notifydestroyed(); parent.destroyapp(true); } }
/** * close all open streams */
public void stop() { try { stop = true;
if (is != null) { is.close(); }
if (os != null) { os.close(); }
if (sc != null) { sc.close(); }
if (scn != null) { scn.close(); } } catch (ioexception ioe) { } } }
package socket;
import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import java.io.*;
public class client implements runnable, commandlistener { private socketmidlet parent; private display display; private form f; private stringitem si; private textfield tf; private boolean stop; private command sendcommand = new command("send", command.item, 1); private command exitcommand = new command("exit", command.exit, 1);
inputstream is; outputstream os; socketconnection sc; sender sender;
public client(socketmidlet m) { parent = m; display = display.getdisplay(parent); f = new form("socket client"); si = new stringitem("status:", " "); tf = new textfield("send:", "", 30, textfield.any); f.append(si); f.append(tf); f.addcommand(exitcommand); f.addcommand(sendcommand); f.setcommandlistener(this); display.setcurrent(f); }
/** * start the client thread */ public void start() { thread t = new thread(this); t.start(); }
public void run() { try { sc = (socketconnection) connector.open("socket://localhost:5009"); si.settext("connected to server"); is = sc.openinputstream(); os = sc.openoutputstream();
// start the thread for sending messages - see sender's main // comment for explanation sender = new sender(os); // loop forever, receiving data while (true) { stringbuffer sb = new stringbuffer(); int c = 0; while (((c = is.read()) != '/n') && (c != -1)) { sb.append((char) c); }
if (c == -1) { break; } // display message to user si.settext("message received - " + sb.tostring()); } stop(); si.settext("connection closed"); f.removecommand(sendcommand); } catch (connectionnotfoundexception cnfe) { alert a = new alert("client", "please run server midlet first",null, alerttype.error); a.settimeout(alert.forever); a.setcommandlistener(this); display.setcurrent(a); } catch (ioexception ioe) { if (!stop) { ioe.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } }
public void commandaction(command c, displayable s) { if (c == sendcommand && !parent.ispaused()) { sender.send(tf.getstring()); }
if ((c == alert.dismiss_command) || (c == exitcommand)) { parent.notifydestroyed(); parent.destroyapp(true); } }
/** * close all open streams */
public void stop() { try { stop = true; if (sender != null) { sender.stop(); }
if (is != null) { is.close(); }
if (os != null) { os.close(); }
if (sc != null) { sc.close(); } } catch (ioexception ioe) { } } }
package socket;
import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import java.io.*;
public class sender extends thread {
private outputstream os; private string message;
public sender(outputstream os) { this.os = os; start(); }
public synchronized void send(string msg) { message = msg; notify(); }
public synchronized void run() { while (true) { // if no client to deal, wait until one connects if (message == null) { try { wait(); } catch (interruptedexception e) { } }
if (message == null) { break; }
try { os.write(message.getbytes()); os.write("/r/n".getbytes()); } catch (ioexception ioe) { ioe.printstacktrace(); }
// completed client handling, return handler to pool and // mark for wait message = null; } }
public synchronized void stop() { message = null; notify(); } } |