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.
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());
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.
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
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?
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
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?
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.