小能豆

为什么我无法建立Input/OutputStreams?Java服务器套接字

javascript

我有一个 ServerMain 和一个 ClientMain,它们向客户端和服务器线程提供套接字。然后我尝试使用相同的方法创建 ObjectStream,但出现错误。

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)

抽象类

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();
        }
    }
}

客户

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();
    }
}

服务器

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();
    }
}

它想让我写更多。所以是的,这是代码的精简版本,因此您不必阅读所有不相关的内容。


阅读 59

收藏
2023-11-16

共1个答案

小能豆

持有 Socket 的主线程不会等待客户端/服务器退出。它只是启动连接,启动客户端/服务器,然后立即结束 try(){} 块,关闭流。

您应该.join()创建您创建的线程,以使主线程等待客户端/服务器线程完成后再继续,但那时我质疑为什么您甚至会有一个新线程开始。

2023-11-16