-2

クライアントにストリームを送信しています。クライアントのそのストリームを1行ずつ、クライアントのテキスト領域に読み込むにはどうすればよいですか?

データを送信するための私のサーバーコード:

パブリッククラスサーバーは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);
         }
     }

}

}

クライアントコード:

/ * *このテンプレートを変更するには、[ツール]|[ツール]を選択します。テンプレート*そしてエディターでテンプレートを開きます。* /

  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));



}

}

サーバーストリームを読み取り、クライアントのテキスト領域に一度に1行ずつデータを表示したいと思います。誰かが私を助けることができますか?

4

1 に答える 1

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

ドキュメントを参照してください。詳細についてJTextComponent.(Reader,Object)は。

于 2013-01-25T07:49:08.733 に答える