CLDC and MIDP - TC65 TCP connexion

Hi, i use a TCP client on my TC65 which connect to a PC server. My code :

publicboolean initConnexionTCP(){

String openParm ="socket://" + destHost +":" + destPort+";" + connProfile;

System.out.println("Tentative de connection: " + destHost +"@" + destPort);

try{

sc = (SocketConnection) Connector.open(openParm);

System.out.println("Connection OK...");

is = sc.openInputStream();

os = sc.openOutputStream();

}catch(ConnectionNotFoundException e)

{

System.out.println("Connection Not Found");

returnfalse;

}catch (Exception e)

{

System.out.println("Connexion : " + e.getMessage() + e.getClass());

returnfalse;

}

returntrue;

}

And when i read data from server i use

is.available()

to know if data is in my buffer. TC65 example uses

while(ch != -1){

ch = is.read();

}

. But when i m waiting ch == -1 it s never appended. is.available() works but sometimes my client is waiting data from server and it waits too.

My reception function on TC65 (client) is :

public String readTCP(InputStream is)throws IOException{

int ch=0;

StringBuffer str=new StringBuffer();

while(is.available()==0);

while (is.available() > 0){

ch=is.read();

str.append((char)ch);

}

//System.out.println(str);

return str.toString();

}

Sometimes communications are ok, sometimes there is a trouble during transfer,...?

Someone knows something about that ?

Thanks !

[3194 byte] By [AdrienDa] at [2007-11-26 23:30:37]
# 1
search for TC65 in this forum, they are plenty of messages with THIS device!!
suparenoa at 2007-7-10 14:41:52 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

The right way to read all the data is to wait until

is.read()!=-1;

To achieve it please make sure, that you have closed the stream from server side after sending all data.

is.available()

can only tell you when you need to start reading data (not to block on is.read() ) , but not when to finish it.

Tigra321a at 2007-7-10 14:41:52 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

If you want to read larger blocks of data, a single read is very, very slow

It much better to read multiple bytes at ones. How many? Well: as many as available() gives you.

Shoule be easy to implement.

Do not make a endless loop with the available() call. Firstly, let the Thread sleep for about 10ms after each call to available() . You might also want a timeout mechanism. Should also be simple to implement.

About bad and irregular data transfer: what else does the application do, and what connection are you using? There are a few issues with this, but those are fairly particular problems.

deepspacea at 2007-7-10 14:41:52 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

Ok for the end less loop.

However, for bad data transfer :

My client sends a message like "DATA?", and the server responds with 50 short messages like "DATA1" , "DATA2", ... without close output stream.

Example :

output/input stream opened

CLIENTSERVER

DATA?-->

<-DATA1


OK>

<-DATA2

-OK-->

...

output/input stream closed

Why does it work sometimes only ? Because of my end less loop ? Have I to resend message after a sleep time ?

thanks

AdrienDa at 2007-7-10 14:41:52 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5
well... What particular problem occurs? The client can not read all the data?for example it reads somethign like "DAT" instead of "DATA1" or something else?in any case try to flush the OutputStream after sending a message (Both from client & server side.)
Tigra321a at 2007-7-10 14:41:52 > top of Java-index,Java Mobility Forums,Java ME Technologies...