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
Solved

Data over TCP/IP socket

Posted on 2004-07-30
5
582 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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