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
Posts: 25
Registered: ‎11-19-2009
My Device: Not Specified

problems communication protocol with external server

it's a it difficult to describe this problem but i'll try

 

i'm trying to create a socket connection with an external server that comunicates with some packets with a defined structure. First to this server i send an array of 8 bytes to start the comunication, i receive an ack then i send the real packet with date, hour and some gps coordinates.

 

the problem is that i don't know what the socket sends.

 

i'll explain with a little example.

 

i have to send a string of 8 chars that i transform in bytes (just a simple type cast is needed) i put these bytes in the byte array then i send it to the socket but the server doesn't send the ack (the server is ok it sends the ack to other application but these applications aren't java's applications) so the problem is what i really send to the server.

 

i've tried to create a file to look what i send and i found that the bytes are transformed to decimals and this is fine for me but when i tried to write the bytes on the BB's screen i saw that the "string" is  completely different of what i expect.

 

as socket i've used one that i've created some time ago it worked fine but that time it used a local server

 

i hope that somebody understands what i've said..

 

p.s. i have to use a byte array and i can't use just strings

Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: problems communication protocol with external server

Best would be to use a sniffer tool to check what is sent to the server. I recommend wireshark (formerly known as ethereal).

Just run it on the BES or better test on simulator and let it run locally.

 

How are you writing to the socket? Can you post the code?

 

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 25
Registered: ‎11-19-2009
My Device: Not Specified

Re: problems communication protocol with external server

we were trying to see what  i'm sending to the server but we aren't lucky...now i'll try with the sniffer

 

the socket is a basic one beacause for the moment is a kind of test

 

         public void run()
        {
            byte[] buf = new byte[1500];
            byte[] bufshort = new byte[2];
            byte[] bufint = new byte [4];
            byte[] bufhello = new byte[8];
            short size;
            int counter = 0;
            int orario;
            int latitude;
            int longitude;
            int data;
            char ack;
                StreamConnection connection = null;
               
                try
                {
                    connection = (StreamConnection)Connector.open(_url);
                    _in = new DataInputStream(connection.openDataInputStream());
                    _out = new DataOutputStream(connection.openDataOutputStream());
                   
                 for(int i = 0; i<8; i++){
                  
                  bufhello[i] = (byte)HELLO.charAt(i);
                  counter ++;
                 }
                    // Send the HELLO string.
                 _out.write(bufhello, 0, 8); // 8 bytes to start the connection
                    ack = (char)_in.read(); // wait for the ack  the socket stops here

                     Coor coor = (Coor) CoorVect.firstElement();
                     for(int i = 0; i<8; i++){
                      
                      buf[i] = (byte)packet.charAt(i); //header of the packet
                      counter ++;
                     }
                        size =(short) (14 + 36 * CoorVect.size());
                     bufshort = FromShortToByte(size);
                     buf[8] = bufshort[0];
                     buf[9] = bufshort[1];
                     counter = counter + 2;
                     data = coor.anno + coor.mese * 10000 + coor.mese * 1000000;
                     bufint = FromIntToByte(data);
                     
                     for(int i = 0; i<4; i++ ){
                      
                      buf[counter] = bufint[i];
                      counter ++;
                     }
                     
                     // Execute further data exchange here...
                     while (!CoorVect.isEmpty()){
                     
                      coor = (Coor) CoorVect.firstElement();
                      orario = coor.secondi + coor.minuti * 100 + coor.ora * 10000;
                      bufint = FromIntToByte(orario);
                      for(int i = 0; i < 4; i++){
                       
                       buf[counter] = bufint[i];
                       counter ++;
                      }
                      latitude = (int)coor.latitude * 100000;
                      longitude = (int)coor.longitude * 100000;
                      bufint = FromIntToByte(latitude);
                      for(int i = 0; i < 4; i++){
                       
                       buf[counter] = bufint[i];
                       counter ++;
                      }
                      bufint = FromIntToByte(longitude);
                      for(int i = 0; i < 4; i++){
                       
                       buf[counter] = bufint[i];
                       counter ++;
                      }
                      for(int i = 0; i<23; i++){
                       buf[counter]= 0;
                       counter ++;
                      }
                      buf[counter]  = 4;
                      counter ++;
                      for(int i=0; i < 4; i++){
                       buf[counter] = 0;
                      }
                     
                      CoorVect.removeElementAt(0);
                     }
                     
                     _out.write(buf, 0, buf.length);
                   
                    // Debug
                    System.out.println("Debug: exchange(): received acknowledgement char:" + ack);
                    _delay = DEFAULT; // Reset the backoff delay .
                     
                 
                }
                catch (IOException e)
                {
                    _delay = increaseDelay(_delay);
                    GPSDemo.this.updateLocationScreen(e.toString());
                   
                    // Push this data back on the stack, it's still pending.
                    synchronized (this)
                    {
                        _data.insertElementAt(buf, 0);
                    }
                }
                finally
                {
                    try
                    {
                        if(_in!=null) _in.close();
                        if(_out!=null) _out.close();
                        if(connection!=null) connection.close();
                    }
                    catch (IOException ioe)
                    {
                       
                    }
                }
            }
Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: problems communication protocol with external server

Try using a flush after writing to the outputstream.

 

Just a comment: you can only send ascii characters this way. Better would be to use one of getBytes methods from the string instead of casting chars to bytes

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 25
Registered: ‎11-19-2009
My Device: Not Specified

Re: problems communication protocol with external server

the flush doesn't help

 

i know i can use the getbytes, but i can use it only for the first packet after that i can't use it because the next array of bytes hasn't only chars but also int and short.

 

By "you can only send ascii characters this way" you mean that the outputstream sends to the server ascii characters?

 

if yes, how can i send to the server non-ascii characters?

Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: problems communication protocol with external server

> By "you can only send ascii characters this way" you mean that the outputstream sends to the server ascii characters?

No I was just refering to the cast from characters to bytes which only works for ascii characters.

 

 

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 25
Registered: ‎11-19-2009
My Device: Not Specified

Re: problems communication protocol with external server

ok...but it's not the problem

 

by the way the socket is defined right? and the output/input streams are ok? (maybe there's an error that i can't find...)

Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: problems communication protocol with external server

As long as your url starts with "socket://" everything looks fine to me.

Any updates on the data that was sent?

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: problems communication protocol with external server

I think you need to split this into two different problems:

 

a) Getting the data correct

 

I suggest that you create and test independently a routine that will convert your Vector entry in a byte array.  I'm not convinced about some of this code and you write the entire length of the buffer regardless of how much is actually used.  You can do this yourself, it is easy to write a test and use the debugger to check this.  I don't think you need our help with this. 

 

b) Sending the information on the socket.

 

Rewrite the above to just send bytes that you specify.  For example one easy thing to do is

byte [] testSend = "0123456789ABCDEF".getBytes();

 

Then you can test your socket code independently.

 

Once you have rewritten this and have a much smaller code snippet, if you are still having problems can you repost the smaller section of code.  That will make it easier for us to understand and test ourselves if we want to.

 

 

In addition, there seems to be to be a flaw in your design.  I would recommend that you send one entry from your Vector, and only delete it when you have received an acknowledgement that it has been successfully received.  The code at present will process all entries (which might overflow your buffer) and may not even send them, meaning you have lost your co-ordinates. 

 

 

Developer
Posts: 25
Registered: ‎11-19-2009
My Device: Not Specified

Re: problems communication protocol with external server

yes the url stars with "socket://"

 

i've found that there was some connection problems but trying with the socket of the SocketDemo a i can connect to the server but it says that the comunication protocol il wrong...i have to send UB000001 as an "hello" to the server, but the servers log says that i send all zeros...so i can't send the packet with the gps' coordinates....