Solved

Byte Encoding doubt

Posted on 2006-10-21
11
219 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 43
solarwind tftp server 2 45
Checkbox and ListView in Android Layout 4 56
ForLoop Example 3 40
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now