开发者

one server multiple clients

开发者 https://www.devze.com 2023-02-08 02:46 出处:网络
I am engaging the following problems: I need to write a server program, will accept multiple clients all clients are subscribing the same data from server, for example the stock price update.

I am engaging the following problems:

  1. I need to write a server program, will accept multiple clients
  2. all clients are subscribing the same data from server, for example the stock price update.
  3. each clients can send simple commands to server like "logon", "stop"

So here is my solution, Since I am not very experianced in multithread/tcp, I want to know is it a good solution? if not, is there any better solution? is it necessary to have a thread for each client socket? Thanks BTW: sorry for confusing every one, it is a small project that only involve 5-10 classes.

class AcceptThread {
    ......
    public void run () {
        ControlThread controlThread = new ControlThread();
        controlThread.start();

        Socket socket = new Socket(port);
        while (!stop) {
            Socket s = socket.accept();
            controlThr开发者_运维百科ead.addClient (s);
        }
    }
}

class ControlThread {
    Set<Scoket> clients;
    SendDataThread sendDataThread;  

    public ControlThread () {
        sendDataThread = new SendDataThread();
        sendDataThread.start();     
    }

    public void addClient (Socket socket) {
        clients.add(socket);
        sendDataThread.addListener(socket);
    }

    public void run () {
        ......
        for (Socket s : clients) {
            if (s.getInputStream().available()) {
                //read command from s
            }
        }
        ......              
    }
}

class SendDataThread () {
    Set<Scoket> listeners;

    public void addListener (Socket s) {
        listeners.add(s);
    }

    public void run () {
        for (Socket s: listeners) {
            // send data to each listener
        }
    }
}


is it necessary to have a thread for each client socket?

No, as a matter of fact, I wouldn't even not recommend it. If it's a small project and you don't want to use any existing library, I would suggest you use the java.nio package and the SelectableChannels. With a so called selector you can easily monitor clients for incoming data in a non-blocking way.

Here are a few useful links:

  • NIO Examples (from the official tutorial)
  • The Rox Java NIO Tutorial


What about using standards technology for solving this problem?

JMS topic A distribution mechanism for publishing messages that are delivered to multiple subscribers.

Tutorial


Thanks BTW: sorry for confusing every one, it is a small project that only involve 5-10 classes.

There's definately nothing wrong with it. All higher level abstractions are based on sockets one way or another. Unless your project is sufficiently large there is no need to pull a series other frameworks/toolkit to perform the same job. Threads are cheap (and can even benefit from a multicore architecture), although using SelectableChannels as @aioobe suggested is not a bad idea either.

When your projects require it, you can always learn about other inter-process communication methods (message passing, remote method invocaton etc., and its about 100 implementations) then.

You might however want to limit the number of threads you server use simultaneously. This can easily be achieved by claiming e.g., a semaphore of a size equal to the number of threads you want to serve. Another interesting thing to consider is using java thread pools to better reuse your resources.


Others have mentioned that you can use the nio library to reduce the number of threads necessary. i just wanted to point out that your current example code will not work. you must use a thread per socket when using the standard io streams. the available() method is pretty much useless (in general) and will not stop your control thread from blocking.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号