此问题已在此处有答案:
java.net.SocketException socket is closed(1个答案)
7年前关闭。
所以我仍然试图解决这个客户端/服务器的概念。我终于有一个有点工作的程序,除了我得到这个错误。我已经在网上看了看,看看这是不是我能自己解决的问题,但是对于我的生活,我不能弄清楚。有没有人可以看看客户端代码,看看我在哪里犯了错误,并指出我在正确的方向。
java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at Contact$IncomingReader.run(Contact.java:104)
at java.lang.Thread.run(Thread.java:745)
客户代码
public class Contact{
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
JPanel mainP;
JScrollPane scroll;
JButton sendB;
public static void main(String[] args){
Contact client = new Contact();
client.go();
}
public void go(){
JFrame frame = new JFrame("Client");
mainP = new JPanel();
incoming = new JTextArea(15,50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(false);
incoming.setEditable(false);
scroll = new JScrollPane(incoming);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
sendB = new JButton("Send");
sendB.addActionListener(new SendButtonListener());
mainP.add(scroll);
mainP.add(outgoing);
mainP.add(sendB);
Networking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.setLocationRelativeTo(null);
frame.getContentPane().add(BorderLayout.CENTER, mainP);
frame.setSize(400,400);
frame.setVisible(true);
}
private void Networking(){
try{
sock = new Socket("127.0.0.1",5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Connection Established");
}catch(IOException ex){
ex.printStackTrace();
}
}
public class SendButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try{
writer.println(outgoing.getText());
writer.close();
}catch(Exception ex){
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
public class IncomingReader implements Runnable{
@Override
public void run() {
String message;
try{
while((message = reader.readLine()) != null){
System.out.println("read " + message);
incoming.append(message + "\n");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
1条答案
按热度按时间6ljaweal1#
这个异常意味着 * 你 * 关闭了套接字,然后继续使用它。
你在这里关闭了它。关闭套接字的输入流或输出流都会关闭套接字。查看Javadoc。