0

我正在创建简单的点对点游戏。目前我正在服务器/客户端(节点)端工作。我发生了奇怪的问题,我可以ObjectOutputStream从套接字创建,但程序创建ObjectInputStream. 为了存储套接字和创建流,我使用了我自己的类(下面的代码)。我做错了什么?

public void run() {
    try {
        while (this.listen) {

            temp = serverSocket.accept();
            sockList.addSocket(temp);
            sockList.addObjOutStrm(temp);
            sockList.addObjInStrm(temp); // <------------------------ program stops


            setChanged();
            notifyObservers(temp);

            System.out.println("SERVER: Dodalem uzytkownika, oto pelna lista:\n\n"
                            + sockList + "\n");

            synchronized (sockList) {
                for (ObjectOutputStream oos : sockList.getOOSList()) {
                    oos.writeObject(sockList.extractToString());
                }
            }
        }
    } catch (IOException e) {
        System.err.println("SERVER: Błąd I/O serwera podczas nasluchu");
        //e.printStackTrace();
    }
}

我自己的类来存储套接字和流:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SocketList {
    private List<ObjectOutputStream> obOutStrList;
    private List<ObjectInputStream> obInStrList;
    private List<Socket> sockList;

    private int maxUsers;
    private int currentUsers;

    public SocketList() {
        obOutStrList = Collections
                .synchronizedList(new ArrayList<ObjectOutputStream>());
        obInStrList = Collections
                .synchronizedList(new ArrayList<ObjectInputStream>());
        sockList = Collections.synchronizedList(new ArrayList<Socket>());

        maxUsers = 4;
        currentUsers = 0;
    }

    synchronized public void addSocket(Socket sock) {
        if (sockList.size() < 4) {
            sockList.add(sock);
            currentUsers++;
        } else {
            System.out.println("sockList: Blad max uzytkownikow");
        }
    }

    synchronized public void addObjOutStrm(Socket sock) {
        try {
            obOutStrList.add(new ObjectOutputStream(sock.getOutputStream()));
        } catch (IOException e) {
            System.out.println("Problem z uzyskaniem strumienia wyjsciowego dla " + sock.getInetAddress().getHostAddress().toString());
            e.printStackTrace();
        }
    }

    synchronized public void addObjInStrm(Socket sock) {
        try {
            obInStrList.add(new ObjectInputStream(sock.getInputStream()));
        } catch (IOException e) {
            System.out.println("Problem z uzyskaniem strumienia wejsciowego dla " + sock.getInetAddress().getHostAddress().toString());
            e.printStackTrace();
        }
    }

    synchronized public void removeSock(Socket s) {
        if (sockList.contains(s)) {
            System.out.println("SOCKETLIST: Usuwam " + s.toString());
            sockList.remove((Socket) s);
        }
    }

    synchronized public List<Socket> getSockList() {
        return sockList;
    }

    synchronized public List<ObjectOutputStream> getOOSList() {
        return obOutStrList;
    }

    synchronized public List<ObjectInputStream> getOISList() {
        return obInStrList;
    }

    public String toString() {
        return sockList.toString();
    }

    public String[] extractToString() {
        String[] retArr = new String[currentUsers];

        for (int i = 0; i < sockList.size(); i++)
            retArr[i] = sockList.get(i).getInetAddress().getHostAddress()
                    .toString();

        return retArr;
    }

}
4

1 回答 1

1

你确定你要这么做吗?读取写入对象不是很便携,使用某种具有 XML 或 JSON 等数据结构的协议进行通信。这更便携。
对于您的问题:

new ObjectInputStream(sock.getInputStream())

将阻塞并等待,直到标头已写入流,如 JavaDoc 所述:

此构造函数将阻塞,直到相应的 ObjectOutputStream 已写入并刷新标头。

总的来说,我强烈建议您使用不同的架构,使用多线程来处理服务器上的客户端连接,可能使用线程池,甚至可能使用消息队列。请参阅包装 java.util.concurrent以了解您需要的所有内容。

看看这里,首先:http ://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html

于 2012-11-09T14:35:24.703 回答