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
New Contributor
Posts: 2
Registered: ‎02-19-2013
My Device: 9800
My Carrier: indosat Im3

Direct TCP serverside didn't receiving data until client timeout

Hi,

I have a problem with direct tcp connection. i'm using client from socket demo in blackberry simulator 9800 and testing it with my server. Connection successfully made but when client sending data to the server, server didn't notice at all. After 2 minutes later client was timeout and server receive the data and know the client was disconnected.

 

I did modify socket demo code in this part:

 

String url = "socket://" + _screen.getHostFieldText() + ":6050" + (_screen.isDirectTCP() ? ";deviceside=true" : "");                                    
connection = (StreamConnection)Connector.open(url, Connector.READ_WRITE, true);

 

The rest is same

 

The serverside is working perfectly cause i have another client in android and testing with it.

 

what should i do? please help me..

 

I'm sorry cause my english is bad..

 

thanks..

Developer
Posts: 16,676
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Direct TCP serverside didn't receiving data until client timeout

welcome to the support forums.
i would suggest to use http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/transport/ConnectionFactory...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,628
Registered: ‎07-14-2008
My Device: Not Specified

Re: Direct TCP serverside didn't receiving data until client timeout

[ Edited ]

Does the socket demo work correctly when testing with the supplied code and the standard Server component?  In other words, put the code back as it was and test with the supplied server program - does that handshake properly? 

 

I'm using this test to decide if the problem is related to your configuration, specifically something your Server is expecting. 

New Contributor
Posts: 2
Registered: ‎02-19-2013
My Device: 9800
My Carrier: indosat Im3

Re: Direct TCP serverside didn't receiving data until client timeout

handshake is working properly.

i put the code back and still got the problem. Serverside never received client's message so they waiting each other. i presume the outputstreamwriter not direcly send the message. i try flush() but it didn't make any difference.

public class ConnectThread extends Thread
{   
    private InputStream _in;
    private OutputStreamWriter _out;        
    private SocketDemoScreen _screen;   
    
    public ConnectThread()
    {
        _screen = ((SocketDemo)UiApplication.getUiApplication()).getScreen();
    }
    
    private void exchange(String data) throws IOException
    {
        int length = data.length();
        char[] input = new char[length];
        _out.write(data, 0, length);      <------------ didn't arrive at serverside
    _out.flush();              <------------ i try this

        for (int i = 0; i < length; ++i)
        {
            input[i] = (char)_in.read();  <------------ stop here until the connection timeout.
        }        

        StringBuffer s = new StringBuffer();
        s.append("Received: ") ;
        s.append(input, 0, length);
        _screen.updateDisplay(s.toString());
    }

    public void run()
    {        
        StreamConnection connection = null;       
        try
        {
            _screen.updateDisplay("Opening Connection...");
            String url = "socket://" + _screen.getHostFieldText() + ":44444" + (_screen.isDirectTCP() ? ";deviceside=true" : "");                                    
            connection = (StreamConnection)Connector.open(url);
            _screen.updateDisplay("Connection open");
            _in = connection.openInputStream();
            _out = new OutputStreamWriter(connection.openOutputStream());            
            exchange("Hello");
            exchange("Goodbye and farewell");           
            _screen.updateDisplay("Done!");
        }
        catch(IOException e)
        {
            System.err.println(e.toString());
        }
        finally
        {              
            _screen.setThreadRunning(false);
            
            try
            {               
                _in.close();             
            }
            catch(IOException ioe)
            {                
            }
            try
            {       
                _out.close();             
            }
            catch(IOException ioe)
            {               
            }
            try
            {               
                connection.close();
            }
            catch(IOException ioe)
            {                
            }
        }
    }
}

i modify outputstreamwriter with printstream. The connection working properly and message delivered to serverside. i don't understand why outputstreamwriter didn't work. Am i missing something?

the code like this :

public class ConnectThread extends Thread
{   
    private InputStream _in;
    private PrintStream _out;        
    private SocketDemoScreen _screen;   
    
    public ConnectThread()
    {
        _screen = ((SocketDemo)UiApplication.getUiApplication()).getScreen();
    }

    private void exchange(String data) throws IOException
    {
        int length = data.length();
        
        char[] input = new char[length];
        _out.println(data);

        for (int i = 0; i < length; ++i)
        {
            input[i] = (char)_in.read();
        }        

        StringBuffer s = new StringBuffer();
        s.append("Received: ") ;
        s.append(input, 0, length);
        _screen.updateDisplay(s.toString());
    }

    public void run()
    {        
        StreamConnection connection = null;       
        
        try
        {
            _screen.updateDisplay("Opening Connection...");
            String url = "socket://" + _screen.getHostFieldText() + ":44444" + (_screen.isDirectTCP() ? ";deviceside=true" : "");                                    
            connection = (StreamConnection)Connector.open(url);
            _screen.updateDisplay("Connection open");
            _in = connection.openInputStream();
            _out = new PrintStream(connection.openOutputStream());            
            exchange("Hello");
            exchange("Goodbye and farewell");           
            _screen.updateDisplay("Done!");
        }
        catch(IOException e)
        {
            System.err.println(e.toString());
        }
        finally
        {              
            _screen.setThreadRunning(false);
            
            try
            {               
                _in.close();             
            }
            catch(IOException ioe)
            {                
            }    
            _out.close();             
            try
            {               
                connection.close();
            }
            catch(IOException ioe)
            {                
            }
        }
    }
}
thanks.

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

Re: Direct TCP serverside didn't receiving data until client timeout

That is interesting.  Here is what I think you have explained:

 

The sample uses OutputStreamWriter and it doesn't work. 

But if you change the sample to use a PrintStream it does work. 

 

Is that what you were saying?

 

Were you connecting to the supplied Server code running on your own PC or to your real Server code?

 

The only thing I can see that is different is that println will do a flush automatically.  But as you pointed out, adding your own flush didn't help.  So that is confusing. 

 

Can I just say that for me, the use of OutputStreamWriter here is lazy, and the sample should just have used OutputStream.  This would have meant extra code, specifically it would have to convert the String to bytes, send the bytes and then do a flush.  But that is what I think you need to do for SocketConnections where you really need to know what bytes you are sending.  So can you test this?  I mean can you test using OutputStream, rather than OutputStreamWriter or PrintStream.  You will need to convert the String to bytes and flush in the exchange() method.  Let us know how you get on.