0

好的,我正在尝试制作一个简单的聊天服务器和客户端。这个程序是聊天服务器。我有两个正在运行的线程。第一个线程处理所有客户端并使用 ServerSocket 将每个新客户端绑定到一个新套接字,第二个线程现在允许用户向连接的客户端发送消息。但是,我无法从该线程中获取用户输入!语句“String inputvar = br.readLine()”不会执行!我究竟做错了什么?

public void relay() throws IOException
        {
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                        try{
                            try
                            {
                            while (true)
                            {

                                Socket socket2 = null;       
                                Socket socket1 = socketmethod(socket2,0); 
                                System.out.println(socket1);
                                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                                while (socket1 != null)
                                { 
                                        PrintWriter outputriver = new PrintWriter(socket.getOutputStream(),true);
                                        boolean cfd = true;
                                        while(cfd==true)
                                        {

                                            System.out.println("this worked");
                                            System.out.print("Message: ");
                                            String inputvar = br.readLine();
                                            System.out.println("this worked2");
                                            if (inputvar.equals("disconnect_now"))
                                            {
                                                cfd = false;
                                            }
                                            outputriver.println("Unknown User: " + inputvar);

                                        }
                                }   
                            }
                            }
                            finally
                            {
                              System.out.println("error 1");
                            }
                        }
                        catch (IOException e)
                        {

                            System.out.println("Unexpected error: IOException in thread2");
                        }
                }
            }).start(); 
4

1 回答 1

0

我无法从您的代码中看出您在哪里开始接受您的客户端连接。我希望看到这样的东西......

public void replay() {
    try {
        serverSocket = new ServerSocket(port);

        while(true) {
            try {
                final Socket connection = serverSocket.accept();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                            String value = br.readLine();

                            PrintWriter pw = new PrintWriter(connection.getOutputStream(), true);
                            pw.println(value);

但是,如果您接受具有多个线程的多个客户端请求,您将让它们在竞争时阻塞 br.readline()System.in

还要确保您的输入发送正确的“换行”字符以满足readline()要求。

于 2014-09-21T19:25:33.763 回答