Solved

Byte Encoding doubt

Posted on 2006-10-21
11
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
How to convert from xls to xlsx using java 7 159
batch vs regular insert in spring DAO 2 52
netbeans can not run self signed app 8 38
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

739 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