Source Code : Server Socket pada Java ME

This code is presented by JENI (Java Education Network Indonesia)
/********************************************
* Percobaan 5 : Server Socket *
* Tested On : Emulator Only *
* Test Result : Work Properly on Localhost *
********************************************/
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ServerSocketExample extends MIDlet implements Runnable,CommandListener{
 Display display;
 Form formServer;
 Command exitCommand = new Command("Exit", Command.EXIT, 0);
 ServerSocketConnection connection = null;
 SocketConnection conn = null;
 InputStream iStream = null;
 public ServerSocketExample(){
 formServer = new Form("Server Socket");
 formServer.addCommand(exitCommand);
 formServer.setCommandListener(this);
 }
 public void startApp(){
 if (display == null){
 display = Display.getDisplay(this);
 display.setCurrent(formServer);
 }
 Thread t= new Thread(this);
 t.start();
 }
 public void pauseApp(){
 }
 public void destroyApp(boolean d){
 }
 public void commandAction(Command c, Displayable d){
 if (c == exitCommand){
 try{
 if (connection != null)
 connection.close();
 if (conn != null)
 conn.close();
 if (iStream != null)
 iStream.close();
 }catch (Exception e){
 }
 destroyApp(true);
 notifyDestroyed(); // Exit
 }
 }
 public void start(){
 }
 public void run(){
 try {
 connection = (ServerSocketConnection) Connector.open("socket://:3887");
 conn = (SocketConnection) connection.acceptAndOpen();
 conn.setSocketOption(conn.DELAY, 0);
 iStream = conn.openInputStream();
 int c = 0;
 String data = "";
 while((c = iStream.read()) != -1) {
 data += (char)c;
 }    
 formServer.append(data);
 } catch (Exception e){
 formServer.append(e.toString());
 }
 }
 public void stop(){
 }
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: