Solved

Data over TCP/IP socket

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Title # Comments Views Activity
eclipse console opening separately 2 50
jsp error 6 72
Setup GlassFish 4 57
Java List 4 76
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

752 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