对于Java / Swing over Java套接字中的聊天应用程序,这是否足以确保文本被正确编码/显示,同时避免特定于平台的编码? (客户端可以在Windows,Linux,Mac上运行)
//sending
bytes chatMsgAsBytes = textField.getText().getBytes("UTF-8");
socketOutputStream.write(chatMsgAsBytes);
//receiving
byte[] bytes = ...
socketInputStream.read(bytes);
textField.setText( new String(bytes,"UTF-8"));
我检查了this,但在通过网络发送字节时,是否还有其他重要的事项需要考虑以避免平台特定编码的问题?
答案 0 :(得分:1)
我已经使用了ObjectOutputStream和ObjectInputStream来实现这个目标。
插口:
Socket cliente = new Socket(host, port);
ObjectOutputStream oos = new ObjectOutputStream(cliente.getOutputStream());
ObjectInputStream ois= new ObjectInputStream(cliente.getInputStream());
发送:
String message="Hi";
oos.writeObject(m);
oos.flush();
收到:
String msg = (String)ois.readObject();