Solved

Data over TCP/IP socket

Posted on 2004-07-30
5
552 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
Comment Utility
Use DataInputStream/DataOutputStream to handle passing various primitives.
ObjectInputStrea.ObjectOutputStream for passing objects.
0
 
LVL 7

Expert Comment

by:JugglerW
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
thanks for responses
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
nestparen challenge 4 55
Which is the latest version of Apache Tomcatl 3 35
Java and GPO 11 45
Java / Linux and Regular Expressions 11 43
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 …
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 …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now