[英]Simple Client/Server Socket based on Netbeans
I'm a beginner and i wrote a simple client server socket model in netbeans - The server is a simple is listing on port 1119 and receive messages from the client and print them ( Console mod) .我是初学者,我在 netbeans 中编写了一个简单的客户端服务器套接字模型 - 服务器是一个简单的列表,在端口 1119 上列出并从客户端接收消息并打印它们(控制台模式)。 - The client is Based on netbeans Form model and has a text field and two buttons (Connect and send ) like in the image :
- 客户端基于 netbeans 表单模型,有一个文本字段和两个按钮(连接和发送),如图所示:
http://s24.postimg.org/on6svgjpx/sss.jpg http://s24.postimg.org/on6svgjpx/sss.jpg
i should press connect once and then when i type a message in the text field and press send it send it to the server which should print it ok , now at the beginning it works ,but after that it doesn't send anything and i need to press connect again and send to send it , the thing i want is ti make the client press connect once and only once and after that i can send messages when press send not using the connect again !!!!我应该按一次连接,然后当我在文本字段中键入消息并按发送时,将其发送到服务器,服务器应该可以正常打印,现在一开始它可以工作,但之后它什么也不发送,我需要再次按下连接并发送发送,我想要的是让客户端按下连接一次且仅一次,之后我可以在按下发送时发送消息而不再次使用连接!!!! so please help me how to do that !!!
所以请帮我怎么做!!!
here is the simple codes :这是简单的代码:
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");
}
}
}
the client code (it is netbeans IDE ):客户端代码(它是 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
}
what i want is ti make the client press connect once and only once and after that i can send messages when press send not using the connect again !我想要的是让客户端按一次连接,并且只有一次,然后我可以在按发送时发送消息而不再次使用连接!
Change this:改变这个:
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");
}
To:到:
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");
}
Keeping accept() in the forever loop will keep server waiting for new Connections.将 accept() 保持在永远循环中将使服务器等待新的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.