0

What I'm trying to do

A client/server application: The client opens a GUI with a button "Read". Clicking it it will open a new frame with some articles to read. At this stage a short String.

Abstract:

  • After you start both server and client , the Client will see the frame with a button.
  • As a response, I want the server to send me specific/different things depending on what Operation I click.

What I tried

At this current stage of development I'm stuck with this error message:

Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error

This is the server-class:

public class StartServer {


    public static void main(String[] argv) throws Exception {

        @
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("Server started");


        while (true) {

            List<String> receive = new ArrayList<String>();

            // Wait for client to connect
            Socket aNewClientSocket = serverSocket.accept();

            System.out.println("Server-Client connexion established!!");

            ObjectInputStream toReceiveFromClient = new ObjectInputStream(aNewClientSocket.getInputStream());
            receive = (List<String>) toReceiveFromClient.readObject();

            System.out.println(aNewClientSocket.getLocalPort());
            System.out.println(aNewClientSocket.getInetAddress());

            switch (receive.get(0)) {
            case "Reader button click":
                List<Object> toSend = new ArrayList<Object>();
                Article a=new Article();
                a.setTitle("New Title");
                toSend.add(a);
                ObjectOutputStream objectWayToSend = new ObjectOutputStream(aNewClientSocket.getOutputStream());
                objectWayToSend.writeObject(toSend);
                objectWayToSend.flush();


            default:
                throw new IllegalArgumentException("Bad request from client!!");
            }

The clients relevant method:

    public void sendToServer(List<String> toSend) throws UnknownHostException, IOException {
        try {


        Socket socket = new Socket("localhost", 9999);
        ObjectOutputStream objectWayToSend = new ObjectOutputStream(socket.getOutputStream());

        objectWayToSend.writeObject(toSend);
        objectWayToSend.flush();

        objectWayToSend.close();

        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(frame, "The server is offline or connection can't be establish!");
        }
    }

    @SuppressWarnings("unchecked")
    public List<Object> receiveFromServer() throws UnknownHostException, IOException {
        List<Object> receive = new ArrayList<Object>();
        try {

            Socket socket = new Socket("localhost", 9999);
        ObjectInputStream objectToGet = new ObjectInputStream(socket.getInputStream());

        receive=(List<Object>) objectToGet.readObject();



        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(frame, "The server is offline or connection can't be establish!");
        }
        return receive;

The referenced button in the GUI

    btnCiteste = new JButton("Read");
            btnCiteste.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    List<String> toSend = new ArrayList<String>();
                    toSend.add("Read");
                    try {
                        sendToServer(toSend);
                        List<Object> received= receiveFromServer();
                        //new ArticoleReaderFrame ();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
blkpingu
  • 1,556
  • 1
  • 18
  • 41
Tufisi Radu
  • 138
  • 6

1 Answers1

1

You close the OutputStream in the client after you finished sending but before you started receiving. That's the cause of your problem, since closing the OutputStream also closes the underlying Socket.

Referencing Official reasons for “Software caused connection abort: socket write error”

blkpingu
  • 1,556
  • 1
  • 18
  • 41
  • 1
    Thanks a lot ! Now it works but it has a problem. After I press the button the application freeze. Is there something bag at my server side? – Tufisi Radu May 09 '18 at 20:37
  • I'd like you to ask a second question and link it here to keep the content separated. – blkpingu May 09 '18 at 20:49
  • @TufisiRadu feel free to read up on [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – blkpingu May 09 '18 at 20:50