2014-03-28 67 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

相关问题