Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java I/O problem

Posted on 2004-11-09
6
Medium Priority
?
244 Views
Last Modified: 2010-03-31

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
0
Comment
Question by:ashok3sep
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12531277
use DataOutputStream.
0
 
LVL 3

Expert Comment

by:nesnemis
ID: 12531285
Hi D4Ly,

You can use the BufferedWriter instead

nesnemis
0
 
LVL 35

Expert Comment

by:girionis
ID: 12531300
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/
0
Industry Leaders: 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!

 
LVL 13

Expert Comment

by:petmagdy
ID: 12531302
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();
0
 
LVL 35

Expert Comment

by:girionis
ID: 12531309
Go to the section where it says "Transporting Objects over Sockets"
0
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 12531322
Thsi is the part you are interested in:

ObjectInputStream ois = new ObjectInputStream(tcpipSocket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(tcpipSocket.getOutputStream());

Then to send objects simply do:

oos.writeObject(new Date());
oos.flush();
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

864 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