0

So I am still trying to tackle this client/server concept. I finally have a somewhat working program except I get this error. I have looked around the net to see if this something I can try to resolve myself, but for the life of me I can't figure it out. Can someone take a look at the client code and see where I made a mistake and point me in the right direction.

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)

Client Code

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();
        }
        
    }
    
}
}
Kirby
  • 15,127
  • 10
  • 89
  • 104

1 Answers1

1

This exception means that you closed the socket and then continued to use it.

writer.close();

You closed it here. Closing either the input stream or the output stream of a socket closes the socket. See the Javadoc.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • When I comment out the writer.close(); and run 2 clients, now the message is not being sent to the server. I have the system read out what is sent to see if its being sent and its not. I will look over the Javadoc on oracles site and see what I can make of it. Thank you EJP for looking at this. – Joshua Wilford Nov 22 '15 at 04:14
  • call writer.flush() since you are using PrintWriter. – Ravindra babu Nov 23 '15 at 11:50