客户端 - 服务器多线程

时间:2011-11-21 18:17:58

标签: java multithreading client

我在使用Java实现一个简单的多线程服务器时遇到了问题。

我的想法是服务器向所有客户端广播消息(不使用多播)。

为此,我正在实施广播方法。此方法将使用for循环,并循环遍历每个客户端Thread,我已将其存储在集合中。它将调用每个线程send(),它输出writeUTF()。

我的问题是2个客户A + B.

A输出:你好 B输出:你好

B不会收到你好,当B再次输入时,它会收到A的消息。代码示例:

import java.net.*;

import java.io. ; import java.util。;

public class ServerThreadHandler扩展了Thread {

private AuctionServer       server          = null;
private Socket              socket          = null;
private String              name            = null;
private int                 ID              = -1;       
private DataInputStream     dataIn          = null;
public  DataOutputStream    dataOut         = null;
private Thread              thread;


protected static Vector handlers = new Vector();

// reason server is used here is because ian was calling a server method broadcast
// from inside the 
public ServerThreadHandler(AuctionServer server, Socket socket, String name) throws IOException{
    this.server = server;
    this.socket = socket;
    this.name   = name;

    dataIn = new DataInputStream( new
                            BufferedInputStream(socket.getInputStream()));

    dataOut = new DataOutputStream(socket.getOutputStream());       
}



// handles a specific client.
public void run(){

    System.out.println("Server running.."); 

    while(true){
        try{
            // broadcast to all clients. This will only be one client in this case.
            server.broadcast(dataIn.readUTF());

            int pause = (int)(Math.random() * 3000);
            Thread.sleep(pause);
        }
        catch(IOException e){
            System.out.println(e.getMessage());
        }
        catch(InterruptedException e){
            System.out.println(e.getMessage());
        }
    }

}

public void send(String msg){
    try{
        dataOut.writeUTF(msg);
        dataOut.flush();
    }
    catch(IOException e){

    }
}

服务器代码:

// broadcast this to clients.
public void broadcast(String msg){
        for(int i = 0; i < clientCount; i++){
            clients[i].send(msg);       
        }
}

客户[]是

private ServerThreadHandler clients[] = new ServerThreadHandler[3];

1 个答案:

答案 0 :(得分:1)

我将给你一个粗略的轮廓,你可以填写其余的细节:

public class Server {

    private ServerSocket serverSocket;
    private List<Socket> clientSockets;

    // constructor(s)

    private class ThreadTask implements Runnable {

        private Socket clientSocket;

        public ThreadTask(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }

        @Override
        public void run() { 
             while(true) {      
                // read message from clientSocket
                for(Socket peerSocket: clientSockets) {
                    // send message on peerSocket
                }
             }
        }

    }

    private void acceptConnections() {
        while(true) {
            try {
                Socket clientSocket = serverSocket.accept();
                clientSockets.add(clientSocket);
                Thread t = new Thread(new ThreadTask(clientSocket));
                t.start();
            } catch (IOException e) {               
                e.printStackTrace();
            } 
        }
    }
}