我一直在努力尝试修改我的代码以成功等待外部类的套接字响应,但未能完成,希望您能帮助我实现我想要的目标。
让我们开始吧:
这是我的IPCSocket类,它通过自己的套接字连接到c ++服务器
package com.socket;
import java.io.*;
import java.net.*;
import java.util.concurrent.ConcurrentLinkedQueue;
public class IPCSocket implements Runnable {
private Socket m_socket;
private static ConcurrentLinkedQueue<String> send_queue = new ConcurrentLinkedQueue<String>();
private static ConcurrentLinkedQueue<String> rcv_queue = new ConcurrentLinkedQueue<String>();
PrintWriter out;
BufferedReader in;
public void run() {
try {
m_socket = new Socket("127.0.0.1", 7070);
System.out.println("The client is running.");
out = new PrintWriter(m_socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
while(true) {
//System.out.println("Sending:");
send(out);
//System.out.println("Receiving:");
receive(in);
}
} catch (IOException | InterruptedException e) {
System.out.println("IOException on java client : " + e.getMessage());
if(e.getMessage().contains("Connection refused")) {
System.out.print("impossibile comunicare con l'urna\n");
}
}
finally {
try {
out.close();
in.close();
m_socket.close();
} catch (IOException e) {
System.out.println("IOException on java client: " + e.getMessage());
}
}
}
public synchronized void send(PrintWriter out) throws IOException, InterruptedException {
while(send_queue.isEmpty()) {
System.out.println("Queue is empty, waiting for it to be filled");
Thread.sleep(5000);
}
String message = send_queue.poll();
System.out.println("Sending >:" + message);
out.println(message);
}
public synchronized void receive(BufferedReader in) throws IOException, InterruptedException {
String str;
while((str = in.readLine()) == null)
Thread.sleep(500);
System.out.println("Receiving <:" + str);
rcv_queue.add(str);
}
public void pushInSendQueue(String message) {
send_queue.add(message);
}
public String pullFromRcvQueue() throws InterruptedException {
return rcv_queue.poll();
}
public String peekFromRcvQueue() {
return rcv_queue.peek();
}
}
在同一个Java项目中也有一个Web服务器,该服务器实例化IPCSocket类以从c ++服务器检索数据并将其显示在网页上
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="com.eVotingOnline.*" %>
<%@ page import="com.socket.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/index.js"></script>
<title>eVoting Online</title>
</head>
<body>
<%
IPCSocket conn = new IPCSocket();
new Thread(conn).start();
//invio token
String token = (String) request.getParameter("token");
conn.pushInSendQueue("1:"+token);
String fileScheda = conn.pullFromRcvQueue();
System.out.println("result:" + fileScheda);
.... // other stuff
问题在于,因为rcv_queue尚未正确填充,pullFromRcvQueue返回了空字符串,但是在pushInSendQueue和pullFromrcvQueue之间添加延迟显然无法解决问题。 我读到有关FutureTask和Callable接口(我认为应该实现)的信息,但是当我尝试在index.jsp中使用Future对象时,即使我正确导入了Java程序包,也会提示“ Future无法解析为类型”。 util.Concurrent。*
您能帮我找到实现我想要的目标的适当方法吗?非常感谢