我有一个连接到服务器的客户端.服务器和客户端以字符串格式交换数据.问题是,服务器在消息末尾没有'\n'字符,因此客户端在readLine()方法中被阻塞.不幸的是,服务器端无法更改.怎么能从流中读出那种最后没有'\n'的消息呢?
我的客户代码:
public class json
{
private static Socket socket;
public static void main(String args[])
{
String sendMessage = "";
Gson gson = new Gson();
JSON_package authentication = new JSON_package();
authentication.setType("Identifying");
authentication.setSource("exampleClient");
Package_Parser pp = new Package_Parser();
sendMessage = gson.toJson(authentication);
sendMessage = authentication.buildPackage(sendMessage);
try
{
String host = "host_address";
int port = port_number;
InetAddress address = InetAddress.getByName(host);
System.out.println("Connecting.");
socket = new Socket(address, port);
System.out.println("Connected.");
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new …Run Code Online (Sandbox Code Playgroud) 我有一个简单的JavaFX应用程序,它有一个TextArea.我可以使用start()方法中的以下代码更新textArea的内容:
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 2000; i++) {
Platform.runLater(new Runnable() {
public void run() {
txtarea.appendText("text\n");
}
});
}
}
}).start();
Run Code Online (Sandbox Code Playgroud)
代码只是将text字符串写入TextArea 2000次.我想从一个在start()方法之外实现的函数更新此textArea.
public void appendText(String p){
txtarea.appendText(p);
}
Run Code Online (Sandbox Code Playgroud)
可以从使用JavaFX应用程序更新TextArea的任意程序调用此函数.我怎样才能在appendText函数中执行此操作?