Chatbox多线程java,GUI和套接字连接问题

时间:2018-02-06 17:09:39

标签: java sockets user-interface

由于socket,我尝试在java中创建一个聊天框。当我使用终端时,我的项目正在工作,但现在我尝试将此部分与我的GUI连接。现在,我的项目分为两部分客户端和服务器。

在我的GUI部分:

  • 首先我有一个Preframe(用户输入用户名并点击回车)。如果用户名有效(没有'@'并包含至少2个字母),我打开与此功能的连接:

    public void connection(String username){
        Socket socket = null;
        try
        {
            socket = new Socket("localhost",2222);
            clientSocket = socket;
            os= new PrintStream(clientSocket.getOutputStream());
            is = new DataInputStream(clientSocket.getInputStream());
            inputLine = new BufferedReader(new InputStreamReader(is));
    
            if (clientSocket != null && os != null && is != null) {
                try {
                    /* Create a thread to read from the server. */
                    new Thread(new IncomingReader()).start();
                    while (!closed) {
                        os.println(inputLine.readLine().trim());
                        System.out.println(inputLine.readLine().trim());
                    }
                    /*
                     * Close the output stream, close the input stream, close the socket.
                     */
    
                    os.close();
                    is.close();
                    clientSocket.close();
                } catch (IOException e) {
                    System.err.println("IOException: " + e);
                }
            }
    
        }catch (UnknownHostException e) {
            System.err.println("Don't know about host " + "localhost");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to the host "+ "localhost");
        }
    }
    
  • 这是我开始的主题:

    public class IncomingReader implements Runnable{
    
        @Override
        public void run() {
            /*
             * Keep on reading from the socket till we receive "Bye" from the * server. Once we received that then we want to break.
             */
            String responseLine;
            try {
                while ((responseLine = inputLine.readLine()) != null) {
                    //chatBox.append(responseLine);
                    System.out.println(responseLine);
    
                    if (responseLine.indexOf("*** Bye") != -1)
                        break;
                }
                closed = true;
            } catch (IOException e) {
                System.err.println("IOException: " + e);
            }
        }
    }
    

在服务器端:

  • 我接受连接

    while (true) {
        try {
            clientSocket = serverSocket.accept();
            if(threads.isEmpty()){
                clientThread t = new clientThread(clientSocket,threads,names);
                t.start();
                threads.add(t);
            }else{
                clientThread t = new clientThread(clientSocket,threads,names);
                t.start();
                threads.add(t);
            }
    
            if (threads.size() == maxClientsCount) {
                PrintStream os = new PrintStream(clientSocket.getOutputStream());
                os.println("Server too busy. Try later.");
                os.close();
                clientSocket.close();
            }
        } catch (IOException e) {
            System.out.println(e);
        }  //System.err.println("Couldn't get I/O for the connection to the host ");
    }
    
  • 我创建了一个新的clientThread:

    public void run() {
        LinkedHashSet<clientThread> threads = this.threads;
        try {
            /*
             * Create input and output streams for this client.
             */
            is = new DataInputStream(clientSocket.getInputStream());
            os = new PrintStream(clientSocket.getOutputStream());
    
            System.out.println(is.readLine().trim());
    
            String name = is.readLine().trim();
            System.out.println(name);
    
            /* Welcome the new the client. */
            os.println("Welcome " + name + " to our chat room.\nTo leave enter /quit in a new line.");
    

现在我注意到的是当我尝试读取名称时,我不能然后我的应用程序冻结。为什么不起作用?

Ps:我对套接字和网络一般不太好。

0 个答案:

没有答案