简体   繁体   中英

bluetooth communication server client stuck j2me

How can I use the same Stream to read/write from server to client or from client to server more than once?

I am making a turn based game over bluetooth. Any ideas on how to achieve this in j2me?

I am using RfCOM protocol.

The client code is

public void serviceSearchCompleted(int transID, int respCode) {
    try {
        StreamConnection SC = (StreamConnection) Connector.open(connectionURL);
        input = SC.openDataInputStream();
        output = SC.openDataOutputStream();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    while (true) {
        f.setCommandListener(new CommandListener() {

            public void commandAction(Command c, Displayable d) {
                if (c.getLabel().toString().equalsIgnoreCase("send")) {
                    try {
                        output.writeUTF("Hey server");
                        output.flush();
                        String msg = input.readUTF();
                        System.out.println(msg);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                        System.out.println("am here now " + ex);
                    }
                }
            }
        });
        synchronized (lock) {
            lock.notify();
        }

    }
}

Server code:

while (true) {
                StreamConnection sc = scn.acceptAndOpen();

                RemoteDevice rd = RemoteDevice.getRemoteDevice(sc);
                DataInputStream input = sc.openDataInputStream();
                DataOutputStream output = sc.openDataOutputStream();
                String inMsg = input.readUTF();
                System.out.println(inMsg + " recived at " + new Date().toString());

                output.writeUTF("Hey client Sent at " + new Date().toString());
                output.flush();
            }

The stream works only once, then nothing happens when I click send again

Processing CONN_INIT 4
Processing CONN_OPEN 4
Processing CONN_SEND 4
Processing CONN_RECEIVE 4
Hey client Sent at Sun Jul 22 19:47:15 GMT+02:00 2012
Processing CONN_SEND 4
Processing CONN_RECEIVE 4

L2CAPConnectionNotifier.acceptAndOpen will block the loop and wait a new connection.

Move your code from the while body to a new thread.

while (true) {
    StreamConnection sc = scn.acceptAndOpen();
    final RemoteDevice rd = RemoteDevice.getRemoteDevice(sc);
    new Thread() {
        public void run() {
            treatConnection(rd);
        }
    }.start();
}

private void treatConnection(RemoteDevice rd) {
    DataInputStream input = sc.openDataInputStream();
    DataOutputStream output = sc.openDataOutputStream();
    String inMsg = input.readUTF();

    while (inMsg != null) { // not sure about this stop condition...
        System.out.println(inMsg + " recived at " + new Date().toString());
        output.writeUTF("Hey client Sent at " + new Date().toString());
        output.flush();

        inMsg = input.readUTF();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM