English 中文(简体)
Java客户服务员/一对多个客户
原标题:Java client-server / one thread multiple clients

首先,我不清楚的是,如何解释,如果不加理性地混淆,我就试图使之简单。 我的服务器显示,作为两个角色游戏的一部分,“母子”是两个客户。 出现这种情况的方式是,当第一客户与服务器连接时,接收了电话连接,与该客户建立了新的客户协议,但没有启动。 当另一个客户与服务器连接时,他又被添加到以前的路面上,然后开始透镜。 现场了解两个投入流和两个产出流,每个客户一个。 然而,当我试图从客户或服务器旁读时,情况就没有发生。 这对单身客户来说是罚款的。 这里有一些法典:

Server side (main thread)

public static Queue<ContestProtocol> contesters;

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    contesters = new LinkedList<ContestProtocol>();

    try {
        serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 4444.");
        System.exit(-1);
    }

    while (true) {
        Socket socket = serverSocket.accept();

        ObjectInputStream ois;
        ObjectOutputStream oos;

        try {
            ois = new ObjectInputStream(socket.getInputStream());
            oos = new ObjectOutputStream(socket.getOutputStream());

            synchronized (contesters) {
                if (contesters.size() == 0) {
                    Contester contester1 = new contester(ois, oos);
                    contesters.add(new ContestProtocol(contester1));
                } else {
                    Contester contester2 = new Contester(ois, oos);
                    contesters.poll().hook(contester2).start();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

服务器(试验议定书)

private Contester contester1, contester2;

public ContestProtocol(Contester contester1) {
    super("ContestProtocol");
}

public ContestProtocol hook(Contester contester2) {
    this.contester2 = contester2;
    return this;
}

public void run() {
    try {
        contester1.getOOS().writeInt(-1);
        contester2.getOOS().writeInt(-2);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

服务器(续):

public Contester(ObjectInputStream ois, ObjectOutputStream oos) {
    this.ois = ois;
    this.oos = oos;
}

public ObjectInputStream getOIS() {
    return ois;
}

public ObjectOutputStream getOOS() {
    return oos;
}

客户方面:

try {
            socket = new Socket(serverAddr, 4444);
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
            int selectedChampion = ois.readInt();
} catch (Exception e) {
                e.printStackTrace();
}

《竞争议定书》 两个客户都在“Int”(Int)()上hang。 我不理解为什么。

最佳回答

This has to do mutex or semaphore issue in accessing the resources: The streams you are using- which in this case are ouput and input streams - are tring to read and write to the same resource. Because all these processes are happening on a continues one thread, their threads are interrupting one another, and creating wait processes on one another.
One solution is that, you register event liseners on the stream objecs, so that whenever one is finished the other one only will start.

问题回答

你们应当听取客户的附带投入。

while(true){
if(ois.avaible() > 0)
ois.readInt();
}

Tip: 寻找网球网,以取得更好的业绩。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签