2013-01-25 59 views
-2

我正在向我的客戶發送一個流。我怎樣才能在客戶端一行一行地讀取客戶端的文本區域?如何getinput流並顯示在textArea?

用於發送數據的我的服務器代碼:

公共類服務器擴展javax.swing.JFrame中{

public Server() 
{ 
    int server=5555; 
    initComponents(); 
} 




    jRadioButton1.setText("a"); 
    jRadioButton1.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      jRadioButton1ActionPerformed(evt); 
     } 
    }); 

    jRadioButton2.setText("b"); 
    jRadioButton2.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      jRadioButton2ActionPerformed(evt); 
     } 
    }); 

    jRadioButton3.setText("c"); 
    jRadioButton3.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      jRadioButton3ActionPerformed(evt); 
     } 
    }); 

    jRadioButton4.setText("d"); 
    jRadioButton4.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      jRadioButton4ActionPerformed(evt); 
     } 
    }); 


    jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N 
    jButton1.setText("SEND"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      Greedy gg=new Greedy(); 
      gg.greedy(); 
      jButton1ActionPerformed(evt); 
     } 
    }); 


    jMenu1.setText("OPEN FILE"); 
    jMenu1.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      OPEN(evt); 

     } 
    }); 

    jMenuItem3.setText("OPEN"); 
    jMenuItem3.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      String cwd=System.getProperty("user.dir"); 
      JFileChooser jfc=new JFileChooser(cwd); 

      Component frame = null; 
      if (jfc.showOpenDialog(frame) !=JFileChooser.APPROVE_OPTION) 
      return; 


      File f = jfc.getSelectedFile(); 


      try { 
       content1(f.toString()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


      readin(f.toString(), jTextArea1); 
     } 
    }); 

private void readin(String string, JTextArea jTextArea12) 
{ 
    try 
      { 

        FileReader fr = new FileReader(string); 
        jTextArea1.read(fr, null); 
        fr.close(); 
      } 
      catch (IOException e) 
       { 
        System.err.println(e); 
       } 

} 



private void EXIT(java.awt.event.ActionEvent evt) 
{      
    System.exit(0); 
}      

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{ 



} 

private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{ 

      } 

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) 
{ 


      } 


private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) 
{ 

} 


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 


    { 

你好(); }

public static void main(String args[]) 
{ 

      java.awt.EventQueue.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      new Server().setVisible(true); 
      System.out.println("SERVER IS STARTED....."); 
     } 
    }); 

    int port=5000; 
    while(true) 
    { 
     try 

    { 
    ServerSocket ser=new ServerSocket(port+10); 

    System.out.println("CLIENT A IS CONNECTED"); 

    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 
     try 
    { 
    ServerSocket ser=new ServerSocket(port+20); 

    ser.accept(); 

    System.out.println("CLIENT B IS CONNECTED"); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 
     try 
    { 
    ServerSocket ser=new ServerSocket(port+30); 

    ser.accept(); 

    System.out.println("CLIENT C IS CONNECTED"); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 
     try 
    { 
    ServerSocket ser=new ServerSocket(port+40); 

    ser.accept(); 

    System.out.println("CLIENT D IS CONNECTED"); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 

    } 
} 

public void hello() 

{ 



    try 
    { 
    FileReader buf=new FileReader("e:\\buffer.txt"); 

    int port=5010; 
    try 
    { 

     @SuppressWarnings("resource") 
     ServerSocket ser=new ServerSocket(port); 
     Socket soc=ser.accept(); 
     BufferedReader toclient=new BufferedReader(buf); 
     DataOutputStream dos=new DataOutputStream(soc.getOutputStream()); 
     System.out.println(dos.toString()); 

     dos.flush(); 
     dos.close(); 




    }catch(Exception e) 
    { 

    } 
    } 
    catch(Exception e) 
    { 
     System.out.println(e+"BUFFER IS EMPTY"); 
    } 

} 




public void content1(String s) throws IOException { 
    try (BufferedReader br = new BufferedReader(new FileReader(s)); 
      PrintWriter pr = new PrintWriter(new File("e:\\buffer.txt"))) { 
     for (String line; (line = br.readLine()) != null;) { 
      pr.println(line); 
     } 
    } 

}

}

客戶端代碼:

/* * 要改變這個模板,選擇Tools |模板 *並在編輯器中打開模板。 */

public class NodeA extends javax.swing.JFrame 
    { 


public JLabel sys=new JLabel(); 



public NodeA() 
{ 

    initComponents(); 
} 


@SuppressWarnings("unchecked") 

private void initComponents() { 




    jLabel1.setText("NODE A"); 


    jTextArea1.setColumns(20); 
    jTextArea1.setRows(5); 
    jScrollPane1.setViewportView(jTextArea1); 


    jButton1.setText("CONNECT"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      jButton1ActionPerformed(evt); 

     } 
    }); 

    jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); 
    jButton2.setForeground(new java.awt.Color(255, 0, 51)); 
    jButton2.setText("OFFLOAD DATA"); 

    jButton2.addActionListener(new java.awt.event.ActionListener() 
    { 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      jButton2ActionPerformed(evt); 
     } 
    }); 

    jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "B", "C", "D" })); 
    jComboBox2.addActionListener(new java.awt.event.ActionListener() 
    { 


     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      jComboBox2ActionPerformed(evt); 
     } 
    }); 

    jLabel2.setText("SELECT NODE TO OFFLOAD"); 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{ 


    try 
    { 
    Socket client=new Socket("localhost",5010); 

    if(client.isConnected()) 
    { 
     System.out.println("SERVER ACCEPTS CONNECTION"); 
    } 

    } 
    catch(Exception e) 
    { 
     System.out.println("SERVER IS BUSY......."); 
    } 
} 

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{ 

} 

private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) 
{ 

} 

public void data() 
{ 

    DataInputStream is; 
    BufferedReader bsr=new BufferedReader(new InputStreamReader(System.in)); 



} 

}

我想讀取服務器流,並在同一時間在我的客戶文本區域顯示的數據一行。有人能幫我嗎?

+0

你試過調試嗎?你有錯誤嗎? – GGrec

+0

號請幫我解決這個問題。 – ankitaloveroses

+0

你的代碼在哪裏試圖做到這一點?它怎麼不工作?你卡在哪裏?到目前爲止,您發佈的內容不是可回答的問題,而是代碼轉儲。請儘量改進,以免因無法辨認而關閉。另外,你不想忽略異常。 –

回答

3
FileReader b = new FileReader("read.txt"); 
jTextArea.read(b, "read.txt"); // we're done! 

請參閱文檔。有關詳細信息,請致電JTextComponent.(Reader,Object)