×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Regular Contributor
    Join Date
    Jul 2005
    Posts
    64

    Push registry for sockets not working on emulator...

    Hi All,

    I want to implement push registry for socket inbound connections. I tried to make the client and server communicating through sockets and run it on sun emulator. Server will send some message to client using socket connection. Client should wake up on incoming socket data from the server using push registry. Below is the code for client and server. Please see that code and point me where I am going wrong...Thanks in advance..

    //Server Midlet
    import javax.microedition.lcdui.*;
    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.midlet.*;

    public class Server implements CommandListener,Runnable{

    MyNetwork parent;
    Form f;
    Command exit=new Command("Exit",Command.EXIT,1);
    StringItem si;
    InputStream is;
    OutputStream os;
    int noOfConnections=3;
    ClientHandler clients[];
    ServerSocketConnection ss1;
    public Server(MyNetwork parent){
    this.parent=parent;
    f=new Form("Server");
    si=new StringItem("Server Started","");
    f.addCommand(exit);
    f.setCommandListener(this);
    f.append(si);
    Display.getDisplay(parent).setCurrent(f);
    }

    public void start(){
    new Thread(this).start();
    }

    public void run(){
    try{
    ss1=(ServerSocketConnection)Connector.open("socket://:5000");
    }
    catch(Exception e){
    System.out.println("Error opening server socket " + e);
    }

    clients=new ClientHandler[noOfConnections];

    for(int i=0;i<noOfConnections;i++)
    clients[i]=new ClientHandler(ss1);
    }
    void selectClient(String message){

    String clientname=message.substring(0,message.indexOf(":"));
    message=message.substring(message.indexOf(":")+1);
    for(int i=0;i<noOfConnections;i++){
    if(clients[i].id.trim().equals(clientname)){
    clients[i].sendMessage(message);
    }
    }

    }

    public void commandAction(Command cmd,Displayable d){
    if(cmd==exit){
    try{
    parent.destroyApp(false);
    }
    catch(Exception e){
    }
    parent.notifyDestroyed();
    }
    }
    void stop(){
    for(int i=0;i<noOfConnections;i++)
    clients[i].stop();
    }

    class ClientHandler implements Runnable{
    ServerSocketConnection ss;
    SocketConnection socket;
    OutputStream os;
    InputStream is;
    String message;
    String id="";
    public ClientHandler(ServerSocketConnection ss){
    this.ss=ss;
    new Thread(this).start();
    }

    public void run(){
    try{

    socket=(SocketConnection) ss.acceptAndOpen();
    os=socket.openOutputStream();
    is=socket.openInputStream();
    while(true){
    StringBuffer buffer=new StringBuffer();
    int c=0;
    while((c=is.read())!='\n' && c!=-1){
    buffer.append((char)c);
    }
    if(c==-1){
    break;

    }
    message=buffer.toString();

    if(message!=null && message.length()>0){
    if(message.indexOf("id")>=0){
    id=message.substring(message.indexOf(":")+1);
    }
    else{
    selectClient(message);
    }
    }
    Thread.sleep(100);

    message=null;
    }


    }
    catch(Exception e){
    System.out.println("Exception thrown in run of client handler " + e);
    }

    }


    void stop(){
    try{
    if(os!=null)
    os.close();
    if(is!=null)
    is.close();
    if(ss!=null)
    ss.close();
    if(socket!=null)
    socket.close();
    }
    catch(Exception e){

    }
    }


    void sendMessage(String msg){
    try{
    os.write(msg.getBytes());
    os.write("\r\n".getBytes());
    }
    catch(Exception e){

    }
    }
    }
    }


    //Client Midlet.....
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.io.*;
    import javax.microedition.io.*;

    public class ClientPush extends MIDlet implements Runnable,CommandListener{

    String url="socket://localhost:5000";
    Form form=new Form("Client");
    Display display;
    TextField tf=new TextField("Message","",200,TextField.ANY);
    InputStream is;
    OutputStream os;
    SocketConnection sc;
    Command exit=new Command("Exit",Command.EXIT,1);
    public ClientPush(){
    form.append(tf);
    form.addCommand(exit);
    form.setCommandListener(this);
    new Thread(this).start();
    }


    public void startApp() throws MIDletStateChangeException{
    display=Display.getDisplay(this);
    display.setCurrent(form);
    }

    public void destroyApp(boolean unconditional) throws MIDletStateChangeException{
    try{
    os.close();
    is.close();
    sc.close();
    }
    catch(Exception e){
    System.out.println("Exception thrown in destroy app " + e);
    }
    }

    public void pauseApp(){

    }


    public void run(){
    try{

    System.out.println("in client 11");
    sc=(SocketConnection)Connector.open(url);
    try{

    String cn = this.getClass().getName();
    // Get the current time by calling Date.getTime()
    PushRegistry.registerConnection("socket://:5000",cn,"*");
    }
    catch(Exception e){
    System.out.println("Exception in scheduleMIDlet " + e);
    }
    System.out.println("in client 22");
    is=sc.openInputStream();
    System.out.println("in client 33");
    os=sc.openOutputStream();
    System.out.println("in client 44");
    while(true){
    System.out.println("in loop============================");
    StringBuffer sb=new StringBuffer();
    int c=0;

    while((c=is.read())!=-1){// && c!=-1){
    System.out.println("c is " + c + "," + sb.toString());
    sb.append((char) c);
    if(!sb.toString().equals(""))
    append(sb.toString());
    }
    System.out.println("after inner loop");
    // if(!sb.toString().equals(""))
    // append(sb.toString());
    // si.setText("Message recieved : " + sb.toString());
    Thread.sleep(100);
    }
    }
    catch(Exception e){
    System.out.println("Exception thrown in run " + e);
    }
    }

    void append(String s){
    System.out.println("in append method " + s);
    tf.setString(s);
    }

    public void commandAction(Command cmd,Displayable d){
    if(cmd==exit){
    try{
    destroyApp(true);
    notifyDestroyed();
    }
    catch(Exception e){
    System.out.println("exception in exit " + e);
    }
    }
    }
    }
    Regards,
    Pankaj Hotwani.

  2. #2
    Nokia Developer Champion
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,192

    Re: Push registry for sockets not working on emulator...

    Please do not crosspost, same question in this thread.
    http://discussion.forum.nokia.com/fo...ad.php?t=77252

    Hartti

Similar Threads

  1. Push Registry Problem..SMS Demo
    By dhamodharan in forum Mobile Java General
    Replies: 2
    Last Post: 2005-11-24, 00:32
  2. Series 60 SDK_2_1_Beta emulator not working in WIN XP SP2
    By kakoskin in forum Mobile Java Tools & SDKs
    Replies: 3
    Last Post: 2004-11-18, 08:12
  3. Sending SMS to test Push with Emulator from Series 60 SDK 2.0
    By mkleijer in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2004-04-15, 14:26
  4. Since 3650 supports WMA, and it supports Push Registry as well ?
    By akokchai in forum Mobile Java General
    Replies: 0
    Last Post: 2003-04-23, 07:49
  5. cannot get WAP Push working on Nokia 6310
    By Nokia_Archive in forum Mobile Web Site Development
    Replies: 1
    Last Post: 2002-05-17, 15:01

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •