我必须制作执行以下操作的客户端服务器测试系统:服务器将测试(以xml格式)发送给客户端,客户端显示测试并提交答案,然后将答案发送回服务器,服务器在服务器上检查答案并返回向客户显示成绩并将其显示出来。 到目前为止,我已经做了一些事情,但这实际上是我第一次使用秋千做一些事情,并且对套接字的使用经验也很差。我将粘贴到目前为止已完成的操作,并尝试解释遇到的问题。
public class ServerSide {
private static ServerSocket serverSocket = null;
private static Socket socket = null;
private OutputStream outputStream;
private InputStream inputStream;
private File file = new File("GradingSystemTest.xml");
private void startServer() {
try {
//opening socket on *.8080
serverSocket = new ServerSocket(8080);
System.out.println("Server started.");
//listening for connection!
socket = serverSocket.accept();
System.out.println("Client connected.");
//sending xml file to the client
sendFile(file);
} catch (IOException e) {
System.out.println(e);
}
}
public void sendFile(File file) {
try {
byte[] bytes = new byte[16 * 1024];
inputStream = new FileInputStream(file);
outputStream = socket.getOutputStream();
int count;
while((count = inputStream.read(bytes)) > 0){
outputStream.write(bytes, 0, count);
}
} catch (IOException e) {
System.out.println("Re-open application.");
} catch (NullPointerException npe) {
System.out.println("Server socket is closed.");
}
}
public static void main(String[] args) {
ServerSide serverSide = new ServerSide();
serverSide.startServer();
}
}
和客户部分
public GradingClient() { }
private void parseXML(InputStream inputStream) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("question");
System.out.printf("There are %d questions. \n", nList.getLength());
System.out.println("----------------------------");
showXML(nList, doc);
}
public void showXML(NodeList nList, Document doc) {
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
questionTitles.add((i + 1) + "." + doc.getElementsByTagName("title").item(i).getTextContent()
.replaceAll("\\s+", " "));
addLabel(questionTitles.get(i));
buttonGroup = new ButtonGroup();
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
for(int j = 0; j < eElement.getElementsByTagName("answer").getLength(); j++) {
char answerChar = (char) ('A' + j);
String actionName = String.valueOf(answerChar);
answer = (" " + answerChar + "." + eElement.getElementsByTagName("answer").item(j).getTextContent().replaceAll("\\s+", " "));
JRadioButton jradioButton = addRadioButton(answer, actionName);
buttonGroup.add(jradioButton);
jRadioButtons.add(jradioButton);
}
}
}
jButton = new JButton("Submit");
panel.add(jButton);
}
public void connectToServer() {
try {
socket = new Socket("localhost", 8080);
connectionLabel.setText("Connected to server.");
panel.add(connectionLabel);
InputStream in = socket.getInputStream();
parseXML(in);
} catch (IOException e) {
connectionLabel.setText("Server is down.");
} catch (SAXException | ParserConfigurationException e) {
e.printStackTrace();
}
}
public void addLabel(String questionTitle) {
panel.add(new JLabel(questionTitle));
}
public JRadioButton addRadioButton(String answerTitle, String actionName) {
JRadioButton jRadioButton = new JRadioButton(answerTitle);
jRadioButton.setActionCommand(actionName);
panel.add(jRadioButton);
return jRadioButton;
}
public void createGUI() {
JFrame frame = new JFrame("Grading System");
connectToServer();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
frame.add(panel);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setSize(1000, 1000);
frame.setVisible(true);
}
public static void main(String[] args) {
GradingClient gradingClient = new GradingClient();
gradingClient.createGUI();
}
}
所以我遇到了一些问题,它们是以下几种:当我启动服务器和客户端时,我会收到xml,客户端会正确显示它,但是只有在服务器套接字关闭时它才能显示GUI,并且在关闭时立即关闭我打开客户端。所以我的问题是以下如何在我单击答案以将其发送回服务器时使服务器套接字保持打开状态并在其后显示GUI,以便服务器可以对其进行检查并将其发送回客户端,从而显示等级然后服务器套接字关闭?我该怎么办?