11.2.1շSMSıʾ
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class WMAServer extends MIDlet
               implements CommandListener, MessageListener  {
  private Command exitCommand;
  private Command getMessageCommand;
  private Display display;
  Form displayForm;
  String MessageReceived;
  MessageConnection serverConn;
  public WMAExample() {
    display = Display.getDisplay(this);
    exitCommand =
    new Command("Exit", Command.SCREEN, 1);
    getMessageCommand =
    new Command("Get", Command.SCREEN, 2);
 }
  public void startApp() {
     displayForm = new Form("Get Message");
     displayForm.addCommand(exitCommand);
     displayForm.addCommand(getMessageCommand);
     displayForm.setCommandListener(this);
     displayForm.setItemStateListener(this);
     display.setCurrent(displayForm);
     try
     {
         serverConn = (MessageConnection)Connector.open
         ("sms://:5000");
         serverConn.setMessageListener(this);
     }
     catch (IOException e)
     {
         System.out.println("Server connection could
         not be obtained");
         e.printStackTrace();
     }
  }
  public void notifyIncomingMessage(MessageConnection
                   conn) {
        Message msg = null;
        try {
            msg = conn.receive();
        }
        catch (Exception e) {
            System.out.println("processMessage.receive "
            + e);
        }
        if (msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage)msg;
            MessageReceived = tmsg.getPayloadText();
        }
        else
        {
            if (msg instanceof BinaryMessage) {
                BinaryMessage bmsg = (BinaryMessage)msg;
                byte[] data = bmsg.getPayloadData();
                MessageReceived = data.toString();
        }
}
  public void pauseApp() { }
  public void destroyApp(boolean unconditional) {
  try {
    if (serverConn != null) {
        serverConn.setMessageListener(null);
        serverConn.close();
    }
}
catch (IOException e) {
    e.printStacktrace();
}
}
  public void commandAction(
  Command c, Displayable s) {
    if (c == exitCommand) {
        destroyApp(false);
        notifyDestroyed();
    }
    if (c == getMsgCommand) {
        try
        {
            displayForm.append(msgReceived);
            display.setCurrent(displayForm);
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
  }
 } 

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class WMAClient extends MIDlet
               implements CommandListener, MessageListener  {
  private Command exitCommand;
  private Command sendMsgCommand;
  private Display display;
  Form displayForm;
  String msgToSend="Hello World!";
  MessageConnection clientConn;
  public WMAClient() {
    display = Display.getDisplay(this);
    exitCommand =
    new Command("Exit", Command.SCREEN, 1);
    sendMsgCommand =
    new Command("Send", Command.SCREEN, 2);
 }
public void notifyIncomingMessage(MessageConnection conn)
 {
 }
  public void startApp() {
     displayForm = new Form("Send Message");
     displayForm.addCommand(exitCommand);
     displayForm.addCommand(sendMsgCommand);
     displayForm.setCommandListener(this);
     displayForm.setItemStateListener(this);
     display.setCurrent(displayForm);
     try
     {
     clientConn = (MessageConnection)Connector.open("
     sms://13880610888:5000");
     }
     catch (IOException e)
     {
        System.out.println("Client connection could
        not be obtained");
        e.printStackTrace();
     }
  }
  public void pauseApp() { 

}
  
  public void destroyApp(boolean unconditional) { 

}
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
        destroyApp(false);
        notifyDestroyed();
    }
    if (c == sendMsgCommand) {
        try
        {
            TextMessage tmsg =
            (TextMessage)clientConn.newMessage
              (MessageConnection.TEXT_MESSAGE);
            tmsg.setAddress("sms://13880610888:5000");
            tmsg.setPayloadText(msgToSend);
            clientConn.send(tmsg);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
  }
 }


//11.2.2  CBSıϢʾ
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
public class CbsReceivers extends MIDlet
               implements CommandListener, MessageListener  {
  private Command exitCommand;
  private Command getMsgCommand;
  private Display display;
  Form displayForm ;
  StringItem messageFiled;
  String msgReceived;
  MessageConnection serverConn;
  MessageReader msgReader;
  public CbsReceivers() {
    display = Display.getDisplay(this);
    displayForm = new Form("");
    messageFiled =new StringItem("Ϣ","");
    exitCommand =
    new Command("Exit", Command.EXIT, 1);
    getMsgCommand =
    new Command("read", Command.SCREEN, 2);
 }
  
  public void startApp() {
     displayForm.addCommand(exitCommand);
     displayForm.addCommand(getMsgCommand);
     displayForm.append(messageFiled);
     displayForm.setCommandListener(this);
     display.setCurrent(displayForm);
     try
     {
     serverConn = (MessageConnection)Connector.open
     ("sms://:5000");
     serverConn.setMessageListener(this);
     }
     catch (IOException ioExc)
     {
     ioExc.printStackTrace();
     }
  }
  public void notifyIncomingMessage(MessageConnection
  conn) {
        if(conn == serverConn){
        try {
            msgReader = new MessageReader();
            new Thread(msgReader).start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        }
       
}

  public void pauseApp() { }
  
  public void destroyApp(boolean unconditional) {
  try {
    if (serverConn != null) {
        serverConn.setMessageListener(null);
        serverConn.close();
    }
}
catch (IOException e) {
    
}
}
  public void commandAction(
  Command c, Displayable s) {
    if (c == exitCommand) {
        destroyApp(false);
        notifyDestroyed();
    }
    if (c == getMsgCommand) {
        
            messageFiled.setText(msgReceived);
            msgReceived = "";
        
    }
  }
  class MessageReader implements Runnable{
	  TextMessage message = null;
	  
	  public void run(){
		  try{
			  Message msg = serverConn.receive();
			  if(msg instanceof TextMessage){
				  message = (TextMessage)msg;
				  msgReceived = message.getPayloadText();
			  }
		  }catch(Exception e){
			e.printStackTrace();  
		  }
	  }
	  	 
	  }
  }
