我是用JAVA设计客户端和服务器的新手。现在,我正在尝试编写一个由GUI呈现的客户机,以便与我的字典服务器进行通信。客户端可以添加、删除或查询一个单词(三个按钮)。客户端代码如下:
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();
}
}
}
}然而,当我每次点击三个按钮中的一个时,它总是弹出一个异常:尝试向服务器发送消息的行上的java.net.SocketException: Socket closed,如output.writeInt(1)或output.writeUTF(word)等。
我完全不知道哪里出了问题。为什么插座关闭?我的代码中甚至没有任何close()。有谁知道这件事吗?非常感谢!
发布于 2017-08-26 22:54:15
我想你可能想看看你的服务器端代码。在连接之后,插座很可能会在那一侧关闭。
https://stackoverflow.com/questions/45896565
复制相似问题