我有一个Web应用程序JSF,它充当客户端,另一个Java应用程序充当服务器。现在我有一个startEngine()函数,它启动服务器上的应用程序,并且工作正常。我还有另一个名为addWebSite的函数,它假设将一个网站添加到之前打开的同一个应用程序中。我对服务器的第一次调用有效,但第二次调用没有,我想在服务器上调用更多函数。
这是我的startEngine函数和addWebSite函数。注意:这是一个管理的大豆会话,但这不会影响任何事情
public void startEgineNow() throws UnknownHostException, IOException {
Session session = HibernateUtil.getSession();
try {
session.beginTransaction();
// get student from database
IPAddress myAddress = session.get(IPAddress.class, sendToSocketId);
session.getTransaction().commit();
if (myAddress.getIpAddress() != null) {
clientSocket = new Socket(myAddress.getIpAddress(), myAddress.getPortNumber());
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
out.println("startEngine");
out.flush();
out.close();
}
} finally {
if (session != null) {
session.close();
}
}
}
public void addWebsite() throws UnknownHostException, IOException{
Session session = HibernateUtil.getSession();
System.out.println(sendToSocketId);
try {
session.beginTransaction();
// get student from database
IPAddress myAddress = session.get(IPAddress.class, sendToSocketId);
Sites sites = new Sites(addSite);
sites.setAddress(myAddress);
session.persist(sites);
session.getTransaction().commit();
if (myAddress.getIpAddress() != null) {
clientSocket = new Socket(myAddress.getIpAddress(), myAddress.getPortNumber());
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
out.println("addWebSite");
out.flush();
out.close();
}
} finally {
if (session != null) {
session.close();
}
}
}
这是我的服务器端代码
public static void main(String[] args) throws InterruptedException, NumberFormatException, IOException {
MainListener myListener = new MainListener();
ServerSocket serverSocket =
new ServerSocket(5000);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.equals("startEngine")) {
myListener.runDesktop();
}else if(inputLine.equals("addWebSite")){
myListener.addWebsite("https://www.buzzfeed.com/");
}
}
当我使用startEngine方法启动引擎时,我希望服务器对其他调用保持打开状态。只有当我从客户端发送close()函数时才关闭,请问我在这里做错了什么