Solved

Data over TCP/IP socket

Posted on 2004-07-30
5
600 Views
Last Modified: 2012-08-13
I'm looking out how to get data back and forth over TCP/IP.
I can see how to do char (see snippet below), but I don't know how to do other primitives (int, float) or how I would xfer an Object.
I guess I have to use something other than InputStreamReader/OutputStreamWriter.
What would I use?

InputStreamReader isr = null;
OutputStreamWriter osw = null;
isr = new InputStreamReader(socket.getInputStream());
osw = new OutputStreamWriter(socket.getOutputStream());

// read
char[] cbuf=null; // character buffer to get chars from stream
isr.read(cbuf,  0, 100);
valid = true;
string = cbuf.toString();

// write
osw.write(string, 0, string.length());
0
Comment
Question by:omom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 11682069
Use DataInputStream/DataOutputStream to handle passing various primitives.
ObjectInputStrea.ObjectOutputStream for passing objects.
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11683235
ObjectInputStream and ObjectOutputStream are ok if you have Java on both sides of your communication line otherwise you have to do the marshalling yourself. e.g. to send a String you may use:

String toSend = "ABC";
OutputStream out;

byte[] bytes = toSend.getBytes();
out.write( out );

but you also must send the length of the string or null terminate or send a special delimiter character (which must be escaped if in contained in the string) to know it on the other side how many bytes to fetch from line.


0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11687618
If you use a buffered stream or writer, then make sure you also call flush (). BTW, don't use readers/ writers for primitives (you can use them for Strings). Stick to some InputStream/ OutputStream like suggested.
0
 

Author Comment

by:omom
ID: 11728784
thanks for responses
0
 
LVL 92

Expert Comment

by:objects
ID: 11731404
0

Featured Post

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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 video teaches viewers about errors in exception handling.
Suggested Courses

635 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