You need to use some kind wire protocol. In simplest case a triple (int length, int/enum
for tag/type, byte data). Then you will be able to differentiate between command "ping" and a sending a file with content "ping". Also look into ProtoBuf or ObjectInputStream - it is futile to try parsing objects from toString() output.