Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
dbigham
Posts: 555
Registered: ‎04-01-2009
My Device: Z10, PlayBook
Accepted Solution

How to send data via TCP

Hi all,

I'm trying to write a very simple application that will transfer some data via TCP to another computer running a TCP server. (About 3K of data)

Although I have followed the code example given in the 4.6 Java Development Guide, page 101, about using socket connections, (http://na.blackberry.com/eng/deliverables/3802/development.pdf) I have been running into issues whereby the data transfer freezes after an indeterminate number of bytes.

 

Does anyone have some sample code for opening up a TCP connection and sending data? This seems like an extremely common thing to do, so I'm not sure what my issue is.

 

Thanks,
Daniel

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to send data via TCP

The socketdemo sample, provided with the JDE, should help you with this.
Developer
dbigham
Posts: 555
Registered: ‎04-01-2009
My Device: Z10, PlayBook

Re: How to send data via TCP

Hi Peter,

 

Thanks for pointing me to the demo folder, I hadn't seen that!

 

I gave the socket demo a quick look over and it appears I am doing the same thing... using an OutputStreamWriter and calling "out.Write". Here's a little more detail about the behavior I'm seeing:

 

1. When the transfer freezes, the application hangs for perhap a minute or two before I can use the menu to close it. 

 

2. If I call out.Write several times, breaking the data up into smaller chunks, it is less often that the transfer freezes. The smaller I make the chunk, the less likely it is to freeze. (I update the screen each time I send a chunk of data, which makes the transfer take longer when using small chunks, so that might be part of the cause/effect of it freezing less frequently)

 

3. The transfer freezes more often on slower connections. For instance, if I send 512 bytes via WiFi, it won't typically freeze, but if I send 512 bytes via a 3G network, it always freezes after sending a couple hundred of the bytes (I see the bytes coming in from the TCP server side).

 

4. While monitoring the TCP server, I've found it curious that it usually receives 1 byte at a time. ie. It fires a "read" event after almost every byte that is received, even though I'm calling out.Write with several hundred bytes from my device. If I send data from another application on my PC to the TCP server, the data arrives as a complete chunk, not broken down into single bytes. I find this a bit odd.

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to send data via TCP

I'm only going to answer your first question, I think that we need to resolve this issue before looking at anything else (and I suspect they are all related).

 

I would almost guarantee that you are running your socket send and receive on the Event Thread.  The Event Thread is what processes your Menu interaction, trackball movement etc.  If you run a long running or blocking operation on this Thread, then your device will freeze.

 

Have a look at the Socket demo and move your network processing to a separate Thread like the sample does.

 

To confirm that your socket processing is running on the Event Thread, you could add this code, run it in the Simulator and watch it in the debugger output window, or put a break point on the System.out line..

 

if ( Application.getApplication(),isEventThread() ) {

System.out.println("Running in Event Thread and shouldn't be");

}

Developer
dbigham
Posts: 555
Registered: ‎04-01-2009
My Device: Z10, PlayBook

Re: How to send data via TCP

Hi Peter,

 

Thanks for the thread advice. I had my socket code in its own thread but I wasn't starting the thread appropriately and so, as you suspected, the socket code was running in the event thread... that that was good to clean up. (Thanks!)

 

Unfortunately, the same behavior results regardless of whether the socket code runs in the event thread or its own thread. (I used the code snippet you provided to verify that, after fixing the code, it is running in its own thread)

 

That being said, I seem to have figured out how to work around the program by writing bytes to the stream rather than a string to the stream. I am baffled as to why this has prevented the transfer from freezing, but it does.

 

Here's an example of problematic code:

 

StreamConnection conn = (StreamConnection)Connector.open(url, Connector.READ_WRITE, true);

OutputStreamWriter out = new OutputStreamWriter(conn.openOutputStream());

out.write(new String(_data), 0, _data.length);

 

And here's an example of code that works:

 

StreamConnection conn = (StreamConnection)Connector.open(url, Connector.READ_WRITE, true);

OutputStream out = conn.openOutputStream(); 

out.write(new String(_data), 0, _data.length); 

 

From the perspective of the TCP server, it now receives the entire transmission at once, rather than a byte or two at a time.

 

Very bizarre... like I said, not sure why writing a string to the stream doesn't work for me.

 

The next challenge is to figure out how to make the code work when I'm not on WiFi... I think I may need to configure an APN?  I'll do some more work and create a new post if I have trouble.

 

Thanks for your help Peter, I appreciate it. 

Developer
dbigham
Posts: 555
Registered: ‎04-01-2009
My Device: Z10, PlayBook

Re: How to send data via TCP

Thanks again for your help with this. Here's a video of my app up and running incase you're interested:

 

http://www.platoai.com/bb.htm 

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to send data via TCP

Very impressive, thanks for sharing that with us.