java - Server/Client errors when too many messages are sent -
i have problem in server/client tcp multiplayer game whenever seek send many messages in short time (usually on 20 messages in arc of 20 millis). after while messages start arrive corrupted reason (like integers in place of strings me numberformatexception).
i send informations strings using dataoutputstream , read them scanner.
inputstream = socket.getinputstream(); outputstream = socket.getoutputstream(); in = new scanner(inputstream); out = new dataoutputstream (outputstream);
my questions are: should utilize different dataoutputstream/scanner combination? there faster combination? should turn strings bytes before sending them?
the strings send composed both integers , strings, "m 2 215 123" or "ep 2".
expanding on @ejp's answer, corruption experiencing result of application programming error of kind.
if using dataoutputstream
write data, should utilize datainputstream
read it. , create sure sequence of write calls matches sequence of read calls.
if want read using scanner, need format info text, , utilize author write it. (make sure utilize same character encoding scheme @ both ends, , avoid doing nasty things mapping binary info text via string(byte[])
... 'cos tend break.)
if either reading or writing info using multiple threads read / write single stream, need utilize kind of locking ensure messages interleave correctly / cleanly. streams typically not thread-safe.
as whether json give improve performance, need experiments sure. among other things, depend on complexity of info , way chose encode in non-json case. (but i'd expect dataoutputstream / datainputstream fastest if chose appropriate encoding.)
java tcp java.util.scanner numberformatexception dataoutputstream
No comments:
Post a Comment