Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Byte Encoding doubt

Posted on 2006-10-21
11
Medium Priority
?
227 Views
Last Modified: 2010-03-31
I got one small doubt more..would appreciate if anybody would help..
So,I created a class to send the data,but i have some problems receiving the data back from the server.This is my class:

import java.net.*;
import java.io.*;
 
public class SocketCat {
 
  public static void main(String[] args) {
 
   
                                          String parameter = "Tester                              ";
            byte[] buf = new byte[40];
            try{
            buf = parameter.getBytes("ASCII");
            }catch(Exception e){
            e.printStackTrace();
            }
 
      try{
            // get a datagram socket
        DatagramSocket socket = new DatagramSocket();
 
            // send request
 
        InetAddress address = InetAddress.getByName("localhost");
            
        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 3590);
        socket.send(packet);
   
         byte[] buffer = new byte[2000];
        // get response
        packet = new DatagramPacket(buffer, buffer.length);
        socket.receive(packet);
 
        // display response
        String received = new String(packet.getData(),"ASCII");
        System.out.println("Data Recd: " + received);
               socket.close();
        }catch(Exception e){
            e.printStackTrace();
            }
  }
 
}
 
 



I would like to change this line:

byte[] buffer = new byte[2000];


to be initialised to the same size as the size of incoming data...is there any way to do so???

And I am getting some strange characters I don't know why,am i doin anything wrong here??

thanks
0
Comment
Question by:harmeek_80
  • 6
  • 4
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17782880
>>to be initialised to the same size as the size of incoming data...is there any way to do so???

That seems academic - how would you know how to decode it if the length can vary?
0
 
LVL 92

Expert Comment

by:objects
ID: 17782932
first send a packet containing the size of the data.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17782937
The length would be

packet.getData().length

but my question stands, i think...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:harmeek_80
ID: 17783821
OK..leave that thing about the length.It would be nice if you can just tell me why  I am I getting some strange characters in the response message back???

Like I am getting  a reponse back as:
[strange character like heart]abcdedbbd    eerssddd[strange character like ?] assss aaeedd   aaaa [strange character like heart]

Could it be,that I am viewing the out in command prompt and thats why I am seeing those characters???Because it is supposed to be a webapp,and finally i need to display the response in a jsp page.....could that be the problem..any clues???
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17783831
The characters are probably not representable at the command prompt. If you print the numeric codes we can investigate further
0
 

Author Comment

by:harmeek_80
ID: 17783856
OK,I am copy-pasting the exact ASCII codes and then the strange characters,there are 4 arrays with length 1 byte,4 byte,30 bytes and 1 byte:



arr[0]:0
Data Recd:



arr[0]:39
arr[1]:-21
arr[2]:-106
arr[3]:0
Data Recd: '??



arr[0]:84
arr[1]:101
arr[2]:115
arr[3]:116
arr[4]:32
arr[5]:85
arr[6]:115
arr[7]:101
arr[8]:114
arr[9]:32
arr[10]:32
arr[11]:32
arr[12]:32
arr[13]:32
arr[14]:32
arr[15]:32
arr[16]:32
arr[17]:32
arr[18]:32
arr[19]:32
arr[20]:32
arr[21]:32
arr[22]:32
arr[23]:32
arr[24]:32
arr[25]:32
arr[26]:32
arr[27]:32
arr[28]:32
arr[29]:32
Data Recd: Test User



arr[0]:4
Data Recd: ♦


Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17783872
OK - which ones are 'weird'?
0
 

Author Comment

by:harmeek_80
ID: 17784010
arr[0]:0
Data Recd:


arr[0]:39
arr[1]:-21
arr[2]:-106
arr[3]:0
Data Recd: '??


arr[0]:4
Data Recd: ♦
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 17784064
arr[0]:0
That's not printable in any encoding

arr[0]:39
Is an apostrophe in ISO8859-1

arr[1]:-21 is
ë

arr[2]:-106
Doesn't exist in ISO8859-1

arr[3]:0
See above

but before we go further - are you sure the encoding is 'ASCII'?

0
 

Author Comment

by:harmeek_80
ID: 17784322
No,I got it..those were actually numbers,and I was encoding them to ASCII...thanks for your great help CEHJ as always!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17784326
No problem

:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question