java - Passing String through Socket duplicates value -
i creating simple client-server application , facing weird behaviour when passing messages through socket: when client writes server, message passed correctly, when server sends response, whichever value sent through socket seems duplicated...
here sample code of server does:
. . . public void respond(socket socket) { seek { inputstreamreader instream = new inputstreamreader( socket.getinputstream() ); printwriter outstream = new printwriter( new outputstreamwriter( socket.getoutputstream(), "utf-16" ) ); outstream.write("message received\n"); outstream.flush(); . . . } grab (exception e) { /* */ } } . . . server , client running on same machine.
furthermore, encoding seems no issue when writing client server, when writing server client: if specify other (or no) encoding utf-16 outputstreamwriter, client won't able parse message correctly.
does of guys have thought why might be?
the character encoding on each end of conversation needs same: charset used encoding inputstreamreader @ client must match used outputstreamwriter @ server (and vice-versa).
if don't specify one, going utilize jvm's default.
when didn't provided client's code, fact server using default charset read , utf-16 write makes me think there potential mismatch.
java sockets printwriter
No comments:
Post a Comment