• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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
0
ashok3sep
Asked:
ashok3sep
  • 5
  • 4
  • 3
  • +1
3 Solutions
 
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
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!

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now