2014-03-28 72 views
0

我是一名初學者,我在netbeans中編寫了一個簡單的客戶端服務器套接字模型 - 服務器是一個簡單的列表,在端口1119上列出並接收來自客戶端的消息並打印它們(Console mod)。 - 客戶端基於netbeans的形式模型,並具有文本字段和兩個按鈕(連接併發送)的圖像中,如:基於Netbeans的簡單客戶端/服務器套接字

http://s24.postimg.org/on6svgjpx/sss.jpg

我應該按連接一次,然後當我鍵入消息文本字段和按發送它發送到服務器,應該打印它確定,現在在開始它的工作,但之後,它不會發送任何東西,我需要再次按連接併發送發送它,事情我想要的是讓客戶端按連接一次,只有一次後,我可以發送消息時,按發送不使用連接再次!!!!所以請幫助我如何做到這一點!

這裏是一個簡單的代碼:

import java.net.*; 
import java.io.*; 

public class Server { 


    public static void main(String args []) throws IOException 
    { 
     System.out.println("Starting Server ...."); 
     ServerSocket ss=new ServerSocket(1119); 
     while(true){ 
     Socket connection=ss.accept(); 
    DataOutputStream dout=new DataOutputStream(connection.getOutputStream()); 
BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream())); 
String s=br.readLine(); 
System.out.println(s+"\n"); 
     } 
    } 
} 

客戶端代碼(這是NetBeans IDE中):

import java.io.*; 
import java.net.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class test extends javax.swing.JFrame { 
DataOutputStream dout; 
BufferedReader br; 
Socket cs=null; 
    /** Creates new form test */ 
    public test() { 
     initComponents(); 
    } 


    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jButton1 = new javax.swing.JButton(); 
     jTextField1 = new javax.swing.JTextField(); 
     jButton2 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

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

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

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(288, Short.MAX_VALUE) 
       .addComponent(jButton2) 
       .addGap(39, 39, 39)) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(42, 42, 42) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(jButton1) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addContainerGap(86, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addGap(38, 38, 38) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addComponent(jButton1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE) 
       .addComponent(jButton2) 
       .addGap(22, 22, 22)) 
     ); 

     pack(); 
    }// </editor-fold> 

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
     try { 
      cs = new Socket("127.0.0.1", 1119); 
      dout=new DataOutputStream(cs.getOutputStream()); 

      // TODO add your handling code here: 
     } catch (UnknownHostException ex) { 
      Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IOException ex) { 
      Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     try { 
      dout.writeBytes(jTextField1.getText()+"\n"); 
      // TODO add your handling code here: 
     } catch (IOException ex) { 
      Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 


    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new test().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JTextField jTextField1; 
    // End of variables declaration 

} 

回答

0

我要的是TI使客戶端按連接一次,只有一次,以後我可以在發送消息時不發送連接再發送消息!

更改此:

while(true){ 
     Socket connection=ss.accept();//here 
     DataOutputStream dout=new DataOutputStream(connection.getOutputStream()); 
     BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     String s=br.readLine(); 
     System.out.println(s+"\n"); 
} 

要:

Socket connection=ss.accept();//here 
while(true){ 
    DataOutputStream dout=new DataOutputStream(connection.getOutputStream()); 
    BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream())); 
    String s=br.readLine(); 
    System.out.println(s+"\n"); 
     } 

保持accept()方法中永遠的循環將保持服務器等待新的連接。

+0

非常感謝它確實工作,你能告訴我如何服務器多客戶端?比如通過編輯代碼來創建一個服務於每個客戶端的線程? 所有問候 –

+0

我有點想你可能想要這樣。您需要創建一個線程,該線程不斷監聽新連接,並將此新套接字傳遞給另一個線程,以便通過此套接字讀取傳入消息。 – user3329166

+0

檢查此鏈接:http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html – user3329166

相關問題