多线程服务器,循环只工作一次

时间:2014-06-16 10:48:40

标签: java multithreading loops while-loop client

我的桥式拍卖我的多线服务器有问题。它的主题并不那么重要,到目前为止,我需要做的就是让run方法中的循环不仅仅运行一次" lap"。我的意思是我的循环只为每个客户端工作一次然后它停止了,但我无法解决这个问题。它应该一直工作,并且在玩家N'序列之后。 E-> S-> W->它从球员N开始另一圈,但现在它只是停滞不前......

检查我的代码:

package serwer;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;

public class Serwer {

public static void main(String[] args) throws Exception {
    System.out.println("Started server to the bridge auction");
    int conCount = 0;
    ServerSocket serwer = new ServerSocket(9898);
    ArrayList<connection> connections = new ArrayList<connection>(){};

    try {
        //only 4 players are allowed to play bridge in one table
        while (connections.size() < 4) {
            connection p = new connection(serwer.accept(), conCount++);
            connections.add(p);
            connections.get(conCount-1).start();
        }
    } finally {
        serwer.close();
    }
}

/**
 * static class responsible for the connection in multithreaded server
 */
static class connection extends Thread {

    private Socket socket;
    private int conCount;
    private static int counter = 0;
    private final String[] Players;
    private String stringOnServer = "0";
    private int stake = 1;

    public connection(Socket socket, int conCount) {
        this.Players = new String[]{"N", "E", "S", "W"};
        this.socket = socket;
        this.conCount = conCount;
        System.out.println("New connection id: " + Players[conCount]);
    }

    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            //here is info what player are you
            out.println("You are player on position " + Players[conCount]);

            while (true) {
                synchronized (this) {
                    if (counter % 4 == conCount) {
                        while (true) {
                            out.println("Your turn " + Players[conCount] + ", please input the text: ");
                            String input = in.readLine();
                            System.out.println("\t" + Players[conCount] + " : " + input);
                            counter += 1;
                            //for now only the stringOnServer is simply echo
                            stringOnServer = input;

                            System.out.println("\tCurrent string on server = " + stringOnServer);
                            try {
                                this.wait();
                            } catch (InterruptedException ex) {
                                Logger.getLogger(Serwer.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            break;
                        }
                    } else {
                        this.notify();
                    }
                }

            }

        } catch (IOException e) {
            System.out.println("error with player: " + Players[conCount] + ": " + e);
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                System.out.println("can't closed");
            }
            System.out.println("connection with player" + Players[conCount] + " terminated");
        }
    }

}
}

客户的代码非常简单,但如果有人有时间和耐心来测试它,我会将其添加到:

package klient;

import java.net.*;
import java.io.*;


public class Klient {

public static void main(String[] args) throws IOException {
    try {
        Socket s = new Socket("127.0.0.1", 9898);
        String answer;
        System.out.println("Welcome on the server of auction.");

        //Here is displayed info from server what player are you
        BufferedReader fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.println(fromServer.readLine());

        while (true) {
            System.out.println(fromServer.readLine());
            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            answer = input.readLine();
            out.println(answer);
        }
    } catch (ConnectException ex) {
        System.out.println("There are 4 players on server or server is closed, try again later");
    }
}
}

问题出现在带有计数器的连接类的这个循环中。 在此先感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

您必须从另一个线程调用notify()。您当前的线程正在等待,无法通知自己。