Solved

Byte Encoding doubt

Posted on 2006-10-21
11
216 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 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

11 Experts available now in Live!

Get 1:1 Help Now