2
A
回答
1
Here's an example寫入和讀取回應服務器。
簡化摘錄:
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
1
不知道,我想這應該工作:
try (DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream())) {
outToClient.write(stringMessage.getBytes("US-ASCII"));
} catch (IOException e) {}
1
在原來的問題沒有說明是ASCII控制代碼是否有處理。
雖然接受的答案適用於打印 ASCII字符我有問題(在Windows 7企業版SP1)使用它包含ASCII控制代碼,尤其是含有任何的java的換行「字符」字符串的字符串,例如VT,CR,LF等。解決方法是將字符串作爲字節發送並將其轉換回遠端的字符串。
查看我對這個問題的回答,瞭解如何處理這種情況。
Reading Lines and byte[] from input stream
和我密切相關的問題,它是公認的答案:
Need TCPIP client that blocks until a specific character sequence is received
相關問題
- 1. Java TCP套接字
- 2. TCP套接字 - InvalidOperationException
- 3. 從TCP套接字
- 4. 套接字和TCP套接字
- 5. TCP套接字連接
- 6. TCP套接字連接
- 7. TCP套接字和連接
- 8. 通過TCP套接字的Android發送/接收字節[]通過TCP套接字
- 9. TCP套接字和Node.js的
- 10. wlan上的TCP套接字
- 11. Iphone上的TCP套接字
- 12. Linux TCP套接字崩潰
- 13. TCP套接字在Android 4.0.3
- 14. Netty WebSockets和TCP套接字
- 15. Android TCP套接字超時
- 16. TCP套接字測試
- 17. PHP插座/ TCP套接字
- 18. TCP套接字管道
- 19. Silverlight 4和TCP套接字
- 20. Node.js TCP套接字會話
- 21. Windows Phone 8.1 TCP套接字
- 22. AS3 TCP套接字丟包
- 23. Java TCP套接字塊readLine
- 24. 從TCP套接字讀取
- 25. C++ Linux TCP套接字fd
- 26. TCP套接字編程
- 27. TCP套接字通信
- 28. 寫入TCP套接字
- 29. 套接字在c tcp
- 30. .NET中的TCP/IP套接字連接
它幾乎是不可思議的,你找不到*在WWW任何*例如:http://下載。 oracle.com/javase/tutorial/networking/index.html –