OBEX: problems with get()

Hi, I'm coding a server-client app. The server is on a PC, and the client on a mobile phone. I want to transfer some files from the PC to the mobile phone, using obex get function. This is the code that I have:

Server:

publicvoid run(){

startButton.setEnabled(false);

updateStatus(local.getFriendlyName());

UUID uuid =new UUID("8841",true);

String url ="btgoep://localhost:" + uuid +";name=reproductor_video;authenticate=false;master=false;encrypt=false";

try{

//updateStatus("epa");

sn = (SessionNotifier)Connector.open(url);

updateStatus("[server:] Now waiting for a client to connect");

}catch (Exception e){

updateStatus("Error: "+e.getCause());

}

try{

sn.acceptAndOpen(this);

updateStatus("[server:] A client is now connected");

}catch (Exception ex){

updateStatus("Error: "+ex.getCause());

}

}

publicint onConnect(HeaderSet request, HeaderSet reply){

updateStatus("[server:] The client has created an OBEX session.");

return ResponseCodes.OBEX_HTTP_OK;

}

publicint onGet(Operation op){

try{

hs = op.getReceivedHeaders();

String path ="C:/UPL/"+hs.getHeader(HeaderSet.NAME).toString();

File f =new File(path);

InputStream is =new FileInputStream(f);

byte filebytes [] =newbyte [is.available()];

updateStatus(path+"\nTama駉: "+is.available());

is.read(filebytes);

is.close();

OutputStream os = op.openOutputStream();

os.write(filebytes);

os.close();

op.close();

}catch (IOException ex){

updateStatus(ex.toString());

ex.printStackTrace();

}

return ResponseCodes.OBEX_HTTP_OK;

}

publicvoid onDisconnect(HeaderSet req, HeaderSet resp){

updateStatus("[server:] The client has disconnected the OBEX session");

}

And the client:

publicvoid run(){

servicio.append("conectado1\n");

try{

cs = (ClientSession) Connector.open(connectionURL);

}catch (IOException ex){

servicio.append(ex.toString());

ex.printStackTrace();

}

servicio.append("conectado2\n");

HeaderSet hs;

hs = cs.createHeaderSet();

hs.setHeader(HeaderSet.NAME, name);

servicio.append("conectado3\n");

try{

cs.connect(hs);

}catch (IOException ex){

servicio.append(ex.toString());

ex.printStackTrace();

}

servicio.append("conectado4\n");

try{

putop = cs.get(hs);

InputStream is = putop.openInputStream();

path = System.getProperty("fileconn.dir.memorycard");

path=path+"upl/";

filecon = (FileConnection) Connector.open(path, Connector.READ_WRITE);

if (!filecon.exists())

filecon.mkdir();

filecon = (FileConnection) Connector.open(path+name, Connector.READ_WRITE);

if (!filecon.exists())

filecon.create();

OutputStream os = filecon.openOutputStream();

int len;

while (is.available()>=0 && (len =is.read(data))>0){

os.write(data, 0, len);

}

is.close();

os.close();

}catch (IOException ex){

servicio.append(ex.toString());

ex.printStackTrace();

}

try{

cs.disconnect(null);

}catch (IOException ex){

ex.printStackTrace();

}

synchronized (this){

try{

wait (5000);

}catch (InterruptedException ex){

ex.printStackTrace();

}

}

conectar("playlist.txt");

}

the first time the file is downloaded correctly, but the second time, the app gets blocked in cs.connect(hs);

Can anybody help me? Thanks

[6862 byte] By [ouendana] at [2007-11-27 8:50:46]
# 1

I don't know anything about the framework you're using but that server looks like it only deals with one client at a time. It should be starting a thread somewhere for each accepted socket.

Also don't use available() to guess the length of a file; don't use available() to guess when you'vre read all the data from a socket; and don't read entire files into memory at once, especially to or from a mobile phone. There is no necessity for any of this.

ejpa at 2007-7-12 21:02:33 > top of Java-index,Core,Core APIs...
# 2
Ok, thanks for answering.
ouendana at 2007-7-12 21:02:33 > top of Java-index,Core,Core APIs...
# 3

Well, now I have another little problem.

When the first file is downloaded, I close the connection, with sn.close(); , and run another thread, which contains an sn.acceptAndOpen(this); function.

The problem is that the URI changes each time I do acceptAndOpen:

btgoep://<BD_ADDR>:<SRV_CH_ID>[;<param>=<value>;...;<param>=<value>]

SRV_CH_ID is changing, so I should search for services each time I want to download a file. Is ther any way not to change SRV_CH_ID, so I can do more than one get(); with a unique URI?

Thanks.

ouendana at 2007-7-12 21:02:33 > top of Java-index,Core,Core APIs...