Solved

Byte Encoding doubt

Posted on 2006-10-21
11
223 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

627 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