簡體   English   中英

服務器未收到消息-Java

[英]Server not recieving message - Java

我有一個服務器和一個客戶端,在我的服務器UI中,我有3個使用MouseClick的JLabel,可以與連接到服務器的各個客戶端通信。 當我單擊JLabel1消息將轉到Client1 ,當Client1 JLabel1消息時,它應該響應,但是當它收到來自服務器的消息時,它不響應服務器。 希望有人指導我我的代碼有什么問題。

//服務器

void connect_clients()
    {
        try {
            ServerSocket listener = new ServerSocket(7700);
            jButton1.setText("Server Running!");
            jButton1.setEnabled(false);
                while (true) {

                    socket = listener.accept();
                    socketList.add(socket);

             //RECIEVE METHOD FROM CLIENT WILL COME HERE.

                }

            }
        catch(IOException ex)
        {
            JOptionPane.showMessageDialog(null, "5"+ex);
        }
    }
 *******here when i click my jlabel1,message is goign to 1st client ,it should respond on seeing the message from client. but it is not.****




private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {                                     

    PrintWriter out;
     try {
         socket = socketList.get(0);
         //JOptionPane.showMessageDialog(null, socket);
         out = new PrintWriter(socket.getOutputStream(), true);
         out.println("PC 1");

     } catch (IOException ex) {
     JOptionPane.showMessageDialog(null, "1"+ex);}

//客戶

void connect_server() throws IOException
    {
        try {
            // TODO code application logic here
            String serverAddress = JOptionPane.showInputDialog(
                    "Enter IP Address of a machine that is\n" +
                            "running the date service on port 9090:");
            s = new Socket(serverAddress, 7700);

            while(true){
                BufferedReader input =
            new BufferedReader(new InputStreamReader(s.getInputStream()));
            String answer = input.readLine();
            System.out.println(answer);
                if(answer != null)
                {

                    //respond method to server will come here.
                    JOptionPane.showMessageDialog(null,"Answer is not null");
               }

            }

          }

您必須在循環外創建BufferedReader 實際上與插座同時。 如果readLine()返回null ,則應將其關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM