我在Eclipse中创建了一个java应用程序,通过Socket连接Plant Simulation。我的第一步是发送和接收消息" Hello"工厂模拟(PS)和我的java应用程序之间。它奏效了,但我认为这很奇怪。
Java应用程序是服务器。 PS是客户。
我运行" ServerJava"第一次。当我活跃" MyClientSocket"在PS中,消息" Hallo Plant Simulation"收到PS。然后我运行方法" m_sendMessage"在PS中发送消息" Hallo Java"到ServerJava。它被发送到ServerJava,但它没有打印在控制台中。如果我deactive" MyClientSocket"断开连接,打印在控制台中。
当我运行方法时,如何立即打印消息" m_sendMessage"在PS?
我的代码:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerJava {
public static void main(String[] args) {
ServerSocket listener = null;
String line = null;
Socket clientSocket = null;
// ServerSocket with Port 30005
try {
listener = new ServerSocket(30005);
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
try {
System.out.println("Server is waiting to accept user...");
clientSocket = listener.accept();
System.out.println("Accept a client!");
BufferedReader is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedWriter os = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
while (true) {
// Message to client: Hello Plant Simulation
os.write("Hello Plant Simulation");
os.flush();
//Message from client: Hello Java
line = is.readLine();
System.out.println(line);
os.close();
}
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
System.out.println("Sever stopped!");
}
}
先谢谢你的帮助。