如何使用getOutputStream方法发送字符串。它只能像他们提到的那样发送字节。到目前为止,我可以发送一个字节。但不是字符串值。通过Java中的套接字发送字符串而不是字节
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是事实上的标准。 –