Java I/O help !!!!!!!!!!!!

Hallo Everybody,

I have a class xxxxxx to connect to a server through TCPIP protocol. after making a connection i am sending a sequence of commands to the server ( say for example 10 commands) now i have pasted a sample of only one command IDENTIFY.

I write to the dataoutputstream as a byte[]array identify.

after that i receive the reply from the server in the form of bytes.
so i have a value in the reply of the server in the form of string starting from 10th byte to the 44th byte

i need to print in the form of a string itself
but i have it in numbers printed.
how should i do it in order to have in the desired format.


i also want to know whether it is possible for me to use the same instream and outstream for all the commands that i send in a row to the server.

if that is possible do i have any methods to flush the instream and outstream after every command i send.

public class xxxxxx
{
DataInputStream dis;
DataOutputStream dos;
private Socket tcpipSocket;
InputStream instream;
OutputStream outstream;
private tcpipParameters tcpipparameters;

public void OpenConnection() throws ConnectException,UnknownHostException
{
    try
       {
          tcpipSocket = new Socket   (tcpipparameters.getIpAddress(),tcpipparameters.getPortNumber());
        }
try
{
      outstream = tcpipSocket.getOutputStream();
      instream  = tcpipSocket.getInputStream();
      dos = new DataOutputStream(outstream);
      dis = new DataInputStream(instream);
                  
// The IDENTIFY Command

dos.write(identify,0,len);
dos.flush();
int length1 = instream.available();
for (int i=1; i<=length1; i++)
{
      byte reply = (byte)dis.read();
      System.out.println(reply);
}
}
}

With Regards,

Ashok.G
LVL 5
ashok3sepAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

expertmbCommented:
(new Byte(byte)).toString()
0
expertmbCommented:
byte[] b = ...;
new String(b);
0
ashok3sepAuthor Commented:
the command IDENTIFY that i am writing to the stream has to follow a protocol rule of the server

the protocol rule is as follows.

length : - 2 bytes
command_code :- 2 bytes
protocol version number :- 2 bytes

protocol name length :- 2 bytes
protocol name :- 2*n bytes ( n --- total no of characters in the string of the protocol name)

checksum :- 2 bytes

I am sending all these to the server in the form of a byte[]array.


the server responds to me in the same form with an additional data
( i.e) the status :- 2 bytes

now i have to retrieve the protocol name of the server.

it is giving in bytes
i need to do something to convert these bytes to string

hope u understand now clearly
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

expertmbCommented:
plz try this
byte[] b = ...;//bytes read from the server
String protocolName = new String(b);
0
expertmbCommented:
StringBuffer sb = new StringBuffer();
for (int i=1; i<=length1; i++)
{
     byte reply = (byte)dis.read();
     sb.append((char)(reply & 0xff));
}
System.out.println(sb.toString());


0
CEHJCommented:
Don't use in.available - it's not reliable and doesn't do quite what you may think it does. Read the stream and put it into a ByteArrayOutputStream then do

byte[] bytes = baos.toByteArray();
String s = new String(bytes, 9, 43-9); // bytes from correct offset


0
ashok3sepAuthor Commented:
i need to print only the string from bytes 10 to 44
0
ashok3sepAuthor Commented:
if I could not use          in.available
how could i run the loop till it reads all the contents from the server
0
CEHJCommented:
>>i need to print only the string from bytes 10 to 44

See my code above

>>how could i run the loop till it reads all the contents from the server

int bytesRead = -1;
while ((bytesRead = in.read(buffer)) > -1) {
    // read them
}

0
objectsCommented:
your client needs to know the size of the packet, so first read the number of bytes from the packet and then create a buffer of the appropriate size, and read it from stream

// protocol name length :- 2 bytes
int nbytes = dis.readShort();  // or however it is encoded
// protocol name :- 2*n bytes
byte[] buf = new byte[nbytes*2];
dis.readFully(buf);
String s = new String(bytes);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ashok3sepAuthor Commented:
hallo CEHJ,

> while ((bytesRead = in.read(buffer)) > -1)

What is that buffer variable

i am writing a byte[] identify to the dataoutputstream

now my dataoutputstream has all the data for writing to the socket and then to the server.

then my server reads those data and then sends me the reply back.

now i am reading it again through the datainputstream
in bytes.

how can i print the string that the server is sending in the form of bytes.

please explain me clearly.

With regards,

Ashok.G
0
objectsCommented:
see the code i posted above
0
ashok3sepAuthor Commented:
here is a code for the INIT command to the server

byte[] init = {0,6,0,2};
// here i calculate checksum and append to the init array
init = createMessage.calculateCheckSum(init);
init = baos.toByteArray();

baos.write(init);
baos.flush();
int length = baos.size();
System.out.println("length = " + length);
for (int i=0; i<=length; i++)
{
                byte reply = (byte)bais.read();
      System.out.print(reply);            
}

the length is always zero !!!!!!!!!! why !!!!!!!!
how can i correct the code

the answer from the server should be {08 02 00 010}

With regards,

Ashok.G
0
CEHJCommented:
>>hallo CEHJ ...

The answers to your questions (ignored) i gave in your next question ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.