0

Server code:

while (true) {                                  
    Socket sock = serv.accept(); 
    try {
        new ClientSession(sock, outQueue, activeSessions);
        System.out.println("CS");
    } catch (IOException e) {
        System.out.println("Sock error");
        sock.close();
    }
}

ClientSession:

class ClientSession extends Thread {
private Socket socket;
private OutboundMessages outQueue;
private ActiveSessions activeSessions;
private ObjectInputStream netIn;
private ObjectOutputStream netOut;
int n = 0;
boolean inGame = false;
boolean ready = false;
Player p;

public ClientSession(Socket s, OutboundMessages out, ActiveSessions as)
        throws IOException {
    socket = s;
    outQueue = out;
    activeSessions = as;
    netOut = new ObjectOutputStream(socket.getOutputStream());
    netOut.flush();
    netIn = new ObjectInputStream(socket.getInputStream());
    System.out.println("ClientSession " + this + " starts...");
    while (true) {
        Object nameMsg = null;
        try {
            nameMsg = netIn.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (nameMsg instanceof NameMessage) {
            this.setName(((NameMessage) nameMsg).name);
            break;
        }
    }
    start();
}

public void run() {
    try {
        activeSessions.addSession(this);
        while (true) {
            Object inMsg = null;
            try {
                try {
                    inMsg = netIn.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (inMsg instanceof ReadyMessage) {
                ready = true;
            } else if (inMsg instanceof DirMessage) {
                p.setDir(((DirMessage)inMsg).dir);
            }

        }
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
        }
    }
}

public void sendMessage(Message msg) {
    try {
        if (!socket.isClosed()) {
            netOut.writeObject(msg);
            netOut.flush();
        } else {
            throw new IOException();
        }
    } catch (IOException eee) {
        try {
            socket.close();
        } catch (IOException ee) {
        }
    }
}

Creating input and output on client side:

public void connect() {
    try {
        InetAddress serverAddr = InetAddress.getByName(serverName);
        try {
            System.out.println("Connecting with "
                    + serverAddr.getHostName() + ":" + port);
            socket = new Socket(serverAddr, port);

            try {
                System.out.println("Connected to "
                        + serverAddr.getHostName());
                netOut = new ObjectOutputStream(socket.getOutputStream());
                netOut.flush();
                netIn = new ObjectInputStream(socket.getInputStream());
                netOut.writeObject(new NameMessage(name));
                netOut.flush();
            } finally {

            }

        } catch (ConnectException e) {
            System.out.println("Cannot connect to server");

        } catch (IOException e) {
            System.out.println("Input error");
        }

    } catch (UnknownHostException e) {
        System.out.println("Unknown server: " + e.getMessage());
    }
}

receiver on client end:

public void run() {
    while (true) {
        Object a = null;

            try {
                a = netIn.readObject();
                netIn.reset();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        if (a != null && a instanceof CoordMessage) {
            setXY((CoordMessage)a);
        }
    }
}

Stacktrace:

java.io.StreamCorruptedException: invalid type code: 00
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at twoPlayerClient.Receiver.run(Receiver.java:28)

After creating input and output I keep passing them on to other classes and not creating new ones.

I have read other similar questions but can't find an answer to why this keeps happening.

4

1 回答 1

-1
 new ClientSession(sock, outQueue, activeSessions);

我认为,每个客户端都有一个新会话,因此您不能使用具有全局范围的流变量。因为它也被其他会话线程使用。

于 2012-05-14T09:51:56.723 回答