What's wrong with this server-client example?
I have this simple server and client pair that I can't get speaking to each other and I wonder why.
The commandAction method:
}elseif (cmd == simpleSrvrCommand){
tbox.insert("Starting simple server...", tbox.size());
t =new MyThread();
t.start();
}elseif (cmd == simpleClntCommand){
tbox.insert("Trying to connect to simple server...("+PROTOCOL +
serverDeviceToBeFoundNokia6600 +":" + serviceNumberServer+")",
tbox.size());
try{
L2CAPConnection cConn = (L2CAPConnection) Connector.open(PROTOCOL +
serverDeviceToBeFoundNokia6600 +":" + serviceNumberServer);
String str1 ="Hej!";
String str2 ="Stop Server";
int maxOs = cConn.getTransmitMTU();
int maxIs = cConn.getReceiveMTU();
tbox.insert("Sending 'Hej!'", tbox.size());
cConn.send(str1.getBytes());
byte[] inbuf =newbyte[maxIs];
cConn.receive(inbuf);
tbox.insert("Rec '" + inbuf +"'", tbox.size());
tbox.insert("Send 'Stop Server'", tbox.size());
cConn.send(str2.getBytes());
byte[] inbuf2 =newbyte[maxIs];
cConn.receive(inbuf2);
tbox.insert("Rec '" + inbuf2 +"'", tbox.size());
}catch (IOException e){
tbox.insert("IOExc:" + e.getMessage() +"'", tbox.size());
}
The server thread:
class MyThreadextends Thread{
L2CAPConnectionNotifier server;
LocalDevice local;
L2CAPConnection conn;
publicvoid kill(){
try{ conn.close();}catch (IOException e){ e.printStackTrace();}
try{ server.close();}catch (IOException e){ e.printStackTrace();}
}
publicvoid run(){
server =null;
String message ="";
byte[] data =null;
int length;
try{
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
}catch (BluetoothStateException e){
tbox.insert("Failed to start service", tbox.size());
tbox.insert("BluetoothStateException: " + e.getMessage(), tbox.size());
return;
}
try{
server = (L2CAPConnectionNotifier) Connector.open(
PROTOCOL +"localhost:" + serviceNumberServer);
tbox.insert(local.getBluetoothAddress()+":"+serviceNumberServer, tbox.size());
}catch (IOException e){
tbox.insert("Failed to start service", tbox.size());
tbox.insert("IOException: " + e.getMessage(), tbox.size());
return;
}
tbox.insert("Starting " + PROTOCOL +" Print Server", tbox.size());
while (!(message.equals("Stop Server"))){
message ="";
conn =null;
try{
conn = server.acceptAndOpen();
tbox.insert("Receiving...", tbox.size());
length = conn.getReceiveMTU();
data =newbyte[length];
length = conn.receive(data);
while (length != -1){
message +=new String(data, 0, length);
tbox.insert("Rec (part) '" +new String(data, 0, length), tbox.size());
try{
length = conn.receive(data);
tbox.insert("RECEIVED!", tbox.size());
}catch (IOException e){
tbox.insert("EXCEPTION!"+e.getMessage(), tbox.size());
break;
}
}
tbox.insert("Received '" + message +"'. Sending it back...", tbox.size());
int maxOs = conn.getTransmitMTU();
conn.send(message.getBytes());
}catch (IOException e){
tbox.insert("IOException: " + e.getMessage(), tbox.size());
}finally{
if (conn !=null)
try{ conn.close();}catch (IOException e){}
}
}
try{ server.close();}catch (IOException e){}
tbox.insert("Server stopped", tbox.size());
}
};

