?
Solved

Data over TCP/IP socket

Posted on 2004-07-30
5
Medium Priority
?
604 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 200 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

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!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

718 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