Link to home
Start Free TrialLog in
Avatar of ashok3sep
ashok3sep

asked on

Java I/O problem


this is my program that i had written.
            
outstream = tcpipSocket.getOutputStream();
instream  = tcpipSocket.getInputStream();

//Identify Command Sequence.
            
int protocol_version = 513;
int check_sum = 543;
String protocol_name = "PRS";
byte[] identify = {10,20,(byte)protocol_version,0,(byte)check_sum};

BufferedOutputStream bufferoutput1 = new BufferedOutputStream(outstream);

bufferoutput1.write(identify,0,len);
bufferoutput1.flush();

length = instream.available();
BufferedInputStream bufferinput1 = new BufferedInputStream(instream);

for (int i=1; i<=length; i++)
{
      byte response = (byte)bufferinput1.read();
      System.out.print(response);
}

I dont want to have a byte array  to add all the codes in side the byte array.

Instead it should be possible for me to add strings, integers, and bytes to the output stream.

how could i do it.
please help me out to reach the solution

With regards,
Ashok.G
Avatar of Mick Barry
Mick Barry
Flag of Australia image

use DataOutputStream.
Avatar of nesnemis
nesnemis

Hi D4Ly,

You can use the BufferedWriter instead

nesnemis
Use ObjectInputStream and ObjectOutputStream to send the objects you want. You will need to pass to the ObjectInput/OutputStream constructors the input/output stream of the socket. For more info: http://java.sun.com/developer/technicalArticles/ALT/sockets/
easy:

if u have String for example u can simply:
String strVal = "hello i will become a byte array now";
byte[] byteValue = strVal.getBytes();

for int u can do this
byte[] byteValue = String.valueOf(12).getBytes();
Go to the section where it says "Transporting Objects over Sockets"
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial