使用talkserver为每个客户端创建多个线程

ktca8awb  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(212)

看看其他stackoverflow文章,似乎我只需要一个serversocket,需要多个线程。但是,目前我不能在我的talkserver while循环中创建一个新线程,因为talklister扩展了线程而不是talkconnection。
我不知道我应该编辑talklistener还是talkserver。如何让多人在一台服务器上通话?谢谢!
talkserver类

public class TalkServer{
    private static int port = 31415
    public static void main(String [] args) throw Exception{
        ServerSocket serverSocket = newServerSocket(port)
        String my address = InetAddress.getLocalHost().getHostAddress();
        while (true){
            Socket socket = serverSocket.accept();
            new ChatConnection(socket);
            socket.close();
        }
}

talkconnection类

class TalkConnection
{
    private Socket socket;
    private TalkListener talkListener;
    TalkConnection(Socket socket)
    {
        this.socket = socket;
        this.talkListener = new TalkListener();
        talkListener.start();
        talkSpeaker();
    }
    void talkSpeaker(){
        ... just gets the OutputStream and scan the input for things like QUIT in order to socket.shutdownInput.
    }

    class TalkListener extends Thread{
        public void run(){
            try{
                InetAddress ipAddress = sock.getInetAddress();
                InputStream inputStream = socket.getInputStream();
                InputStreamReader = new InputStreamReader(inputStreamReader);
                String message;
                do{
                    message = bufferedReader.readLine()
                } while (message != null);
            } catch (IDException e) {
                System.out.println(e);
            }
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题