+1  A: 

I'm not quite sure what you're really trying to achieve here. You can't determine what method the remote end is using, since everything will resolve to sending a stream of bytes.

So at your end you will receive a stream of bytes, and then you have to identify what that will represent. What is the set of formats you're looking to identify ? Can you enumerate these ? Can you control how the remote end sends them ? If so, I would implement some form of header, such that (say) the first byte identifies the type (e.g. a string, an array of numbers etc.) and then your client can switch modes appropriately.

If you can use Java object serialisation and the appropriate object streams, then you can resolve a Java Object, and then use instanceof e.g.

ObjectInputStream ois = new ObjectInputStream(is);
Object obj = ois.readObject();

if (obj instanceof String) {
   // do string-related stuff
}

etc. This presupposes that you can change your remote end (not sure about that though!)

Brian Agnew
Hey Brian, Thanks for the quick reply. The issue is, I am running code on server, which is listening to multiple clients having different formats to communicate with server. The common formats used to write by clients are writeUTF and write(byte[]). I also tried to implement a way in which the client could send first few bytes so that I could know the format through read; but then even if assigned to corresp format to read, it couldn't read. please could you help me.
Perhaps the easiest thing to do is to get all processes to write to the socket consistently. i.e. use writeUTF() or write(byte[]). Note also that writeUTF writes 2 bytes for the string length, followed by the string in byte format. So you're never really going to identify one method v. another :-(
Brian Agnew
A: 

I would suggest to take a look at the apache mina framework for network communication. Most (if not all) low level network functionality is handled for you from mina.

Oliver Michels
A: 

Instead of DataInputStream, you can use ObjectOutputStream to write and ObjectInputStream to read.

You can write like this:

 ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
 objectOutputStream.writeObject("xyz");

And read like this:

 ObjectInputStream objectInputStream = new ObjectInputStream(in);
 Object obj = objectInputStream.readObject();
Emre
Hi Emre, Thanks for your suggestion, while initiating ObjectInputStream I am getting an java.io.StreamCorruptedException: invalid stream header. I am trying to google it. Could you please let me know how to resolve this. I have to use DataInputStream.writeUTF(String) to write data.
ObjectInput/OutputStream only work in combination. They won't work with any plain DataInput/OutputStream.
Peter Lawrey
A: 

The problem you will have is that anything you can writeUTF() you can also do with write() and visa versa. This is no way to tell the difference without make some assumptions about the type of data which might be sent. This can be made to work for some specific cases but won't work in a general case.

There is no way for the reader to reliably know the length of a write(byte[]) write. You may find that read() will read the same length over loopback most of the time, but it won't work over a real network.

So the the sender performs write(byte[]) followed by writeUTF(String). There is no way to know how long the first write was, so there is no way to determine when writeUTF starts (again unless you make assumptions about the type of data sent)

A solution to your problem is to send the length of a piece of data, followed by its data type. e.g.

write(bytes) is replaced with writeInt(bytes.length), write((byte) 1 /* bytes */), write(bytes)

writeUTF(bytes) is replaced with byte[] bytes = text.getBytes("UTF-8"); writeInt(bytes.length), write((byte) 2 /* UTF */), write(bytes)

This way the receiver knows the length of the message and its type.

Peter Lawrey