【发布时间】:2013-06-30 17:14:22
【问题描述】:
如何使用 getOutputStream 方法发送字符串。它只能像他们提到的那样发送字节。到目前为止,我可以发送一个字节。但不是字符串值。
public void sendToPort() throws IOException {
Socket socket = null;
try {
socket = new Socket("ip address", 4014);
socket.getOutputStream().write(2); // have to insert the string
} catch (UnknownHostException e) {
System.err.print(e);
} finally {
socket.close();
}
}
提前致谢
【问题讨论】:
-
要发送一个字符串,您必须首先使用某种编码方案将其转换为字节。 UTF-8 是当今事实上的标准。