Stream types

is it possible to use object streams, and if it is, is there a way to send primitives through the stream and determine on the other side if its an object or a primitive, or will i need an object string and a primitive stream through different ports?
[256 byte] By [sosleepya] at [2007-10-3 10:22:25]
# 1

Primitives are written with DataOutputStream. Objects are written with ObjectOutputStream. ObjectOutputStream extends DataOutputStream and therefore has all the methods to write primitives. No need for two streams.

As always, the other end has to know the protocol, or the protocol has to be self-describing.

ejpa at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 2

so i should do something like write/read a line first that will look something like this:

"Object, Object, Line, Line, Object, int, double"

then do a loop that will take apart the string from left to write till the string size is 0, removing from the left with a if else chain in my loop that will read specific types?

... why does it take a smart person to make me realize im stupid? the system scares me...

Message was edited by:

sosleepy

but you can define an object output and input stream to send through a socket right

would i need a complicated strand of stream conversions or will something as simple of this work:ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());

ObjectInputStream in = new ObjectInputStream(client.getInputStream());

sosleepya at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 3

I don't understand your first paragraph.

If you are going to write a sequence of Objects, Strings, lines, and primitives at one end with the various writeXXX methods, the code at the other end has to read using the corresponding readXXX methods in the same sequence, e.g.:

out.writeObject(o1);

out.writeObject(o2);

out.writeInt(i1);

// ...

o1 = in.readObject();

o2 = in.readObject();

i1 = in.readInt();

Your last piece of code is correct.

ejpa at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 4

what i was trying to say in my first paragarph is if something like this works//from the client

out.writeChars("String, Object, int,");

out.writeChars(string1);

out.writeObject(obj1);

out.writeInt(int1);

//severside

String reads = in.readLine();

while(reads.length > 0 && !reads.equals(","))

{

String read = reads.substring(reads.indexOf(","));

if(read.equals("Object"))

performObjectMethod(in.readObject());

else if(read.equals("String"))

performStringMethod(in.readLine());

...

if(reads.length > 1)

reads = reads.substring(reads.indexOf(",")+1,reads.length-1);

}

or something like that

sosleepya at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 5

But why is the reader so decoupled from the writer? Why can't you omit the first write and just write this at the reader:

string1 = in.readChars();

obj1 = in.readObject();

int1 = in.readInt();

? otherwise what on earth is the reader going to do next, after it's got all these bits and pieces that might or might not be there?

Why isn't an application protocol defined?

ejpa at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 6
because objects are only sent through the stream when they are modified and text is only sent through when it is sent by the user
sosleepya at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 7

Ok so I would do something like this:

enum MessageType

{

Object, Integer, String, ...

}

out.writeObject(MessageType.Object);

out.writeObject(obj1)

out.writeObject(MessageType.Integer);

out.writeInt(int1);

//

etc, and on the reading end:

MessageType mt = (MessageType)in.readObject();

switch (mt)

{

case Object:

obj = in.readObject();

break;

case Integer:

int1 = in.readInt();

// ...

}

and so on

ejpa at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 8
enums are new to me... i looked at the documentation and im not exactly understand what your doing with it? or even what it is entirely
sosleepya at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 9
Doesn't matter, could be final ints, the point is to tag each write with what it's going to be so that the reader knows which read method to call. Better than your String syntax which needs a parser within the parser.
ejpa at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 10

so basicly the enum tells you how you should cast a read object? or should it be used in the switch like you used, and is it just defined like this:enum type{

Object , String, int ,double;

}

or is it more complicated?

sosleepya at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 11

The enum tells you whatever you need to know when deciding what kind of read you need to do next. What that might be is up to you and your application protocol. You certainly need an enum for every primitive type you're going to transmit, and maybe one for every Object type as well. How far you go with that is up to you.

Your suggestion won't compile because of the reserved words.

ejpa at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...
# 12
so i could basicly use objects i define and strings? or do the lighter colored blue words also mean they are reserved?
sosleepya at 2007-7-15 5:44:01 > top of Java-index,Archived Forums,Socket Programming...