如何使用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是事實上的標準。 –