服务器:

public Server() throws IOException {
    socket = new ServerSocket(PORT);
    while (true) {
        Socket client = socket.accept();
        Spooker.get().getWindow().append(client.getInetAddress().getHostAddress() + " has connected.");
        if (inetAddrFound(client.getInetAddress().getHostName())) {
            System.out.println("Address found.");
            Thread thread = new Thread(new SpookerClient(client));
            threads.put(client, thread);
            thread.start();
        }

    }

}

private class SpookerClient implements Runnable {

    Socket sock;
    ObjectInputStream ois;
    ObjectOutputStream oos;

    public SpookerClient(Socket socket) {
        System.out.println("Creating Spooker client.");
        this.sock = socket;
        try {
            this.oos = new ObjectOutputStream(socket.getOutputStream());
            oos.flush();
            this.ois = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        Client client;
        try {
            Spooker.get().getWindow().append("Finding object.");
            System.out.println("Finding object.");
            while ((client = (Client) ois.readObject()) != null && !Thread.currentThread().isInterrupted()) {
                System.out.println("Object found.");
                Spooker.get().getWindow().append("Object found.");
                if (keyFound(client.getKEY()) && idFound(client.getID())) {
                    Spooker.get().getWindow().append("Writing object.");
                    System.out.println("Writing object.");
                    oos = new ObjectOutputStream(sock.getOutputStream());
                    oos.writeObject(Spooker.get().getConfiguration());
                    oos.flush();
                    threads.remove(sock);
                    Thread.currentThread().interrupt();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


客户端:

public ClientConnection() {
    try {
        socket = new Socket(decode(ADDR), Integer.parseInt(decode(PORT)));
        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());
        oos.writeObject(new Client());
        oos.flush();
        oos.close();
        Thread thread = new Thread(new IncomingSpookerConfiguration());
        thread.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private class IncomingSpookerConfiguration implements Runnable {

    SpookerConfiguration configuration;

    @Override
    public void run() {
        System.out.println("Trying..");
        try {
            while ((configuration = (SpookerConfiguration) ois.readObject()) != null && !Thread.currentThread().isInterrupted() && Spooker.get().getConfiguration() == null) {
                System.out.println("Configuration loaded.");
                Spooker.get().setConfiguration((SpookerConfiguration) ois.readObject());
                Thread.currentThread().interrupt();
                System.out.println(Spooker.get().getConfiguration().getX());
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


我得到的错误是套接字已关闭。我不明白为什么。

我想做的就是这个;

客户端向服务器发送客户端类,其中包含唯一的标识符(CPUID,也为found here)。服务器检查客户端是否已注册-例如如果他们的cpuid在数据库中。

如果cpuid在数据库中,则服务器会发回“ SpookerConfiguration”类,该类实际上是一个包含客户端将用于创建GUI的值(例如,大小,标题,默认close op等)的类。

我究竟做错了什么?

编辑:

    Client error:

Trying..
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:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.net.SocketInputStream.read(SocketInputStream.java:224)
at java.io.ObjectInputStream$PeekInputStream.peek
(ObjectInputStream.java:2584)
at java.io.ObjectInputStream$BlockDataInputStream.peek
(ObjectInputStream.java:2891)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte
(ObjectInputStream.java:2901)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1502)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at com.spooker.connection.ClientConnection$IncomingSpookerConfiguration.run
(ClientConnection.java:50)
at java.lang.Thread.run(Thread.java:745)


服务器错误:

java.net.SocketException:
Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at  java.io.ObjectOutputStream$BlockDataOutputStream.drain
(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode
(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.writeFatalException
(ObjectOutputStream.java:1580)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351)
at com.spooker.server.Server$SpookerClient.run(Server.java:65)
at java.lang.Thread.run(Thread.java:745)

最佳答案

socket closed表示您已关闭插座,然后继续使用它。解决方案:不。
readObject()在流的末尾不返回null。抛出EOFException。您的读取循环不正确。
在套接字的生存期内,必须使用相同的对象流。

10-07 19:13