views:

21

answers:

2

This is a conceptual programming question. I'm writing a tcp server/client program in Java.

The client program will be receiving multiple data types, and I'm trying to figure out how to properly determine which data type is being sent (otherwise I might try to convert a number to a custom data type or vice versa).

I was thinking about pre-empting any data transmission with a string character to alert the client program as to which type of data it would be receiving.

Is this a valid method of data transmission? It seems rather clunky.

Is there a better way to transmit different types of data at different times?

+1  A: 

A general way to deal with this is to create a Message type, which includes a header and a payload. The header should include enough info for the receiving end to determine what the payload is, so it knows how to translate it back into an actual object, or dataset, or whatever you're sending. In your case, the string character you mention might be the header.

Andy White
A: 

Oh, there's lots of standard ways. That's the great thing about standards - there's so many to choose from.

For this particular case, you might want to look at DataOutputStream and DataInputStream in the java.io package.

Mike Baranczak