javasocketsinputstreamserversocketobjectinputstream

Why can I not establish the Input/OutputStreams? Java ServerSocket


I have a ServerMain and a ClientMain that give a Socket to a Client and a Server Thread. I then try to make ObjectStreams using the same method but I get an error.

error for ServerMain:

java.net.SocketException: An established connection was aborted by the software in your host machine
    at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:325)
    at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
    at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
    at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966)
    at java.base/java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2875)
    at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2891)
    at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3388)
    at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:959)
    at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:397)
    at abstractClass.initializeStreams(abstractClass.java:17)
    at MulServerThread.run(MulServerThread.java:12)

abstractClass

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public abstract class abstractClass extends Thread {
    protected Socket socket;
    protected ObjectInputStream in;
    protected ObjectOutputStream out;
    protected abstractClass(Socket socket) {
       this.socket = socket;
    }

    protected void initializeStreams() {
        try {
            in = new ObjectInputStream(socket.getInputStream());
            out = new ObjectOutputStream(socket.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }
}

Client

import java.io.IOException;
import java.net.Socket;

public class ClientMain {

    public static void main(String[] args) {

        try (Socket server = new Socket("localhost", 3141)) {
            Client client = new Client(server);
            client.start();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
import java.net.Socket;

class Client extends abstractClass {
    Client(Socket socket) {
        super(socket);
    }

    @Override
    public void run() {
        initializeStreams();
    }
}

Server

import java.io.IOException;
import java.net.ServerSocket;

public class ServerMain {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(3141);
            MulServerThread mulThread = new MulServerThread(serverSocket.accept());
            mulThread.start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
import java.net.Socket;

class MulServerThread extends abstractClass {

    public MulServerThread(Socket socket) {
        super(socket);
    }

    @Override
    public void run() {
        initializeStreams();
    }
}

It wants me to write more. So yes, this is a stripped down version of the code so you don't have to read all the irrelevant stuff.


Solution

  • Your main thread holding the Socket doesn't wait for the client / server to exit. It just starts the connection, starts the client / server and then immediately follows up by ending the try(){} block, closing the stream.

    You should .join() the thread you created, to make the main thread wait for the client / server thread to finish before proceeding, tho at that point I question why you would even have a new thread to begin with.