Java,发送带套接字连接的消息的问题

时间:2011-05-16 01:08:02

标签: java sockets client message

我的Java程序有问题。它在服务器和许多客户端之间有一个套接字连接。这是服务器(涉及问题的部分):

private static ArrayList<ParallelServer> clientConnected = new ArrayList<ParallelServer>();


public Server(int port) {
    this.port = port;
    if (!startServer())
        JOptionPane.showMessageDialog(new JFrame(""),
                "Error!", "ERROR!",
                JOptionPane.ERROR_MESSAGE);
}

private boolean startServer() {
    try {
        server = new ServerSocket(port);
        loadDatabase();
    } catch (IOException ex) {
        ex.printStackTrace();
        return false;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return true;
}

public void runServer() {
    while (true) {
        try {
            client = server.accept();
            ParallelServer pServer = new ParallelServer(client);
            clientConnected.add(pServer);
            Thread thread = new Thread(pServer);
            thread.start();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}


public static void sendBroadcast(String username) throws IOException {
    for(int i = 0; i < clientConnected.size(); i++)
        clientConnected.get(i).sendAnswer("@change," + username);
}

并行服务器是:

    private Socket client;
    private InputStreamReader inputstreamreader;
    private BufferedReader bufferedreader;
    private PrintWriter printwriter;

    public ParallelServer(Socket client) {
        this.client = client;
    }

    public void run() {
        try {
            inputstreamreader = new InputStreamReader(client.getInputStream());
            bufferedreader = new BufferedReader(inputstreamreader);
            printwriter = new PrintWriter(client.getOutputStream(), true);
            String lineread = "";

            while (client.isConnected()) {
                lineread = bufferedreader.readLine();
                doCommand(lineread);
            }
        } catch (UnknownHostException unhe) {
        } catch (InterruptedIOException intioe) {
        } catch (IOException ioe) {
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void sendAnswer(String answer) throws IOException {
        printwriter = new PrintWriter(client.getOutputStream(), true);
        printwriter.println(answer);
        printwriter.flush();
    }

这是客户:

private String serverurl = "localhost";
private int serverport = 7777;
private PrintWriter printwriter;
private InputStreamReader inputstreamreader;
private BufferedReader bufferedreader;
private Socket server;

public Client() {
    server = null;
    try {
        server = new Socket(serverurl, serverport);
        server.setSoTimeout(5000);
    } catch (UnknownHostException unhe) {
        System.out.println("UnknownHostException: " + unhe.getMessage());
    } catch (InterruptedIOException intioe) {
        System.out.println("Timeout while attempting to establish socket connection.");
    } catch (IOException ioe) {
        JOptionPane.showMessageDialog(new JFrame(),"Unable to reach the server!","ERROE!",JOptionPane.ERROR_MESSAGE);
    }
}

public String sendCommand(String command) throws IOException {
    if(server == null) {
        try {
            server = new Socket(serverurl, serverport);
            server.setSoTimeout(5000);
        } catch (UnknownHostException unhe) {
            System.out.println("UnknownHostException: " + unhe.getMessage());
        } catch (InterruptedIOException intioe) {
            System.out.println("Timeout while attempting to establish socket connection.");
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(new JFrame(),"Unable to reach the server!","ERROR!",JOptionPane.ERROR_MESSAGE);
        }
    }
    if(server != null) {
        printwriter = new PrintWriter(server.getOutputStream(), true);
        printwriter.println(command);
        printwriter.flush();
        inputstreamreader = new InputStreamReader(server.getInputStream());
        bufferedreader = new BufferedReader(inputstreamreader);

        return bufferedreader.readLine();
    }
    else
        return "@serverProblem";
}

该程序是一个简单的在线游戏。玩家轮流是通过队列创建的,当玩家轮到他的时候,服务器会发送一条广播消息,上面写着“现在是'玩家1'回合”。 (例如)。我的问题是,当客户端收到消息时,就像它添加答案“现在它是'播放器1'转。”到它将收到的下一条消息。在我的情况下:当一名球员轮到他时,他会发送“@ passTurn,username”。 ParallelServer类从队列中轮询它,将其放在队列的底部,发送客户端“@ok”告诉它转弯已成功更改并告诉Server类发送广播消息。然后,当同一个客户端尝试做进一步的操作时,它会考虑“现在它是'玩家1'转弯。”作为服务器给它的答案。相反,我希望服务器和客户端一如既往地工作,当广播消息是正确的时,通知客户端没有任何附带效果。 我能做什么? 感谢。

1 个答案:

答案 0 :(得分:1)

您的双向消息传递机制应如下所示:

服务器:

Wait on any client InputStream
if (broadcast) 
  broadcast_message()
else 
  process_message()

客户端:

Receiving Thread:
Wait on server broadcast

Sending Thread:
Wait on messages to be sent to server from the User Input

这应该可以解决问题:)

希望它有所帮助。干杯!