0
我是新设计客户端和服务器的JAVA。现在我正在尝试编写一个由GUI呈现的客户端来与我的字典服务器进行通信。客户端可以添加,删除或查询单词(三个按钮)。在客户端的代码,如下:writeUTF()方法导致java.net.SocketException:Socket关闭
public class DictionaryClient {
private static String ip;
private static int port;
private DataInputStream input = null;
private DataOutputStream output = null;
public static void main(String[] args) {
// IP and port
ip = args[0];
port = Integer.parseInt(args[1]);
DictionaryClient client = new DictionaryClient();
client.run(ip, port);
}
public void run(String ip, int port){
GUI g = new GUI();
try(Socket socket = new Socket(ip, port);) {
// Output and Input Stream
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
g.start();
JButton c2 = (JButton)g.getComponent(2);
JButton c3 = (JButton)g.getComponent(3);
JButton c4 = (JButton)g.getComponent(4);
c2.addActionListener(new ButtonAdd(g));
c3.addActionListener(new ButtonRemove(g));
c4.addActionListener(new ButtonQuery(g));
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
//Button: Add
public class ButtonAdd implements ActionListener{
GUI g = null;
//Constructor
public ButtonAdd(GUI g){
this.g = g;
}
public void actionPerformed(ActionEvent event) {
JTextField t1 = (JTextField)g.getComponent(1);
JLabel t6 = (JLabel)g.getComponent(6);
String word = t1.getText();
String definition = t6.getText();
JLabel t5 = (JLabel)g.getComponent(5);
try {
output.writeInt(1);
output.writeUTF(word);
output.writeUTF(definition);
output.flush();
String message = input.readUTF();
t5.setText("Status: ");
t6.setText(message);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
//Button: Remove
public class ButtonRemove implements ActionListener{
GUI g = null;
//Constructor
public ButtonRemove(GUI g){
this.g = g;
}
public void actionPerformed(ActionEvent event) {
JTextField t1 = (JTextField)g.getComponent(1);
String word = t1.getText();
JLabel t6 = (JLabel)g.getComponent(6);
JLabel t5 = (JLabel)g.getComponent(5);
try {
output.writeInt(2);
output.writeUTF(word);
output.flush();
t5.setText("Status: ");
String message = input.readUTF();
t6.setText(message);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
//Button: Query
public class ButtonQuery implements ActionListener{
GUI g = null;
//Constructor
public ButtonQuery(GUI g){
this.g = g;
}
public void actionPerformed(ActionEvent event) {
JTextField t1 = (JTextField)g.getComponent(1);
String word = t1.getText();
JLabel t5 = (JLabel)g.getComponent(5);
JLabel t6 = (JLabel)g.getComponent(6);
try {
output.writeInt(3);
output.writeUTF(word);
output.flush();
String message = input.readUTF();
t6.setText(message);
t5.setText("Definition: ");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
然而,当我每次上的三个按钮中的一个点击时,它总是POP操作了异常:java.net.SocketException: Socket closed
就行试图将消息发送到服务器,如output.writeInt(1)
或output.writeUTF(word)
等
我完全不知道怎么回事。为什么套接字关闭?我甚至在我的代码中没有任何close()
。有人有任何想法吗?非常感谢!
那么,你的'run()'方法会退出,所以套接字将被'run()'中的try-with-resources语句自动关闭。 –