문제

I've got a Client and a Server, both running on the same Computer (with Windows 7 OS). Programming language is java.

to send a message I use:

public static void sendMessage(Socket socket,String message) throws IOException{
    byte[] mBytes = message.getBytes();
    output.write(mBytes, 0, mBytes.length);
    output.flush();
}

and to receive I use:

 byte read = (byte) input.read();

several times (the method is quite long, but the details I think arent that important) and store the result in a byte[] messageBytes. To retain the message I use

String message = new String(messageBytes);

This worked for me quite well. But I am wondering about the encodings used here. Will it be ok to use getBytes() on the one host and new String(byte[] bytes) on the other host? Is it good style to act like that?

도움이 되었습니까?

해결책

No, it is not ok without specifying the same encoding on conversion from and to bytes. This is because Java will use the default encoding, and those may be different on different servers.

So, say on the sender side you have default cp1252 (i.e. Windows host), but on the server site you have UTF-8 (Linux). Then you'll get garbage as soon as you have non-ASCII characters.

This is maybe hard to detect. For one, all your servers could have the same default encoding (like, if you run the client and the server on the same host). Second, make sure your test data contain some non-ASCII characters, like the € sign, Umlauts or even chinese characters.

Remember, even if presently client/server do run on the same host: Do it robust and correct from the beginning, so as to remove the restriction that your SW will only run correctly when client and server are on similar platforms.

다른 팁

I've got a Client and a Server, both running on the same Computer

Since both the client and the server run Java on the same platform, it is OK to send String's byte[]s over the wire. The encoding used by this scheme is the implicitly agreed upon default encoding used by the platform on which your client and server run. Documentation of getBytes() says that

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

If you need your client and your server to be on different platforms, use a specific encoding for your data. For example, you could use message.getBytes("UTF8"), or work with the CharsetEncoder APIs.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top