我制作一个像简单套接字程序的菜单。但是在我成功发送并打印一个菜单后,控制台打印“null”,我无法再发送或打印任何菜单,并且控制台打印“Connection refused:connect”
喜欢这个
4个输入,一个成功打印和一个null,另外3个如下所示。
空
拒绝连接:连接
拒绝连接:连接
拒绝连接:连接
我确实还是新手,我使用的是jframe,这是我在Stack Overflow中很少看到的。
Server.java
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Delayed;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.INFORMATION_MESSAGE;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Dicko
*/
public class Server extends javax.swing.JFrame {
public static int PORT = 1111;
public static int PORT2 = 1996;
public static DataInputStream DIS;
static String bon = "";
static String harga = "";
/**
* Creates new form Server
*/
public Server() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btn_ok = new javax.swing.JButton();
lbl_total = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
txa_bon = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Server");
btn_ok.setText("OK");
btn_ok.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_okActionPerformed(evt);
}
});
lbl_total.setText("0");
jLabel2.setText("Rp");
txa_bon.setColumns(20);
txa_bon.setRows(5);
txa_bon.setEditable(false);
jScrollPane1.setViewportView(txa_bon);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 182, Short.MAX_VALUE)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lbl_total, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btn_ok))
.addComponent(jScrollPane1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_ok)
.addComponent(lbl_total)
.addComponent(jLabel2))
.addContainerGap())
);
setBounds(500, 150, 377, 345);
}// </editor-fold>
private void btn_okActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Server().setVisible(true);
Client cl = new Client();
cl.setVisible(true);
}
});
try {
ServerSocket SS = new ServerSocket(PORT);
JOptionPane.showMessageDialog(null, "Koneksi Terhubung", "Server Info", INFORMATION_MESSAGE);
Socket clientSocket = SS.accept();
DIS = new DataInputStream(clientSocket.getInputStream());
String tmp = "";
while (!tmp.equals("stop")) {
tmp = DIS.readUTF();
bon = tmp;
txa_bon.append(bon);
}
DIS.close();
SS.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
// Variables declaration - do not modify
private javax.swing.JButton btn_ok;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbl_total;
private static javax.swing.JTextArea txa_bon;
// End of variables declaration
}
Client.java
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
/**
*
* @author Dicko
*/
public class Client extends javax.swing.JFrame {
public static int Port = 1111;
public static int Port2 = 1996;
public static String IP = "localhost";
public static PrintStream cetak;
/**
* Creates new form Client
*/
public Client() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btn_nasi = new javax.swing.JButton();
btn_bakso = new javax.swing.JButton();
btn_mie = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Client");
setResizable(false);
btn_nasi.setText("Nasi");
btn_nasi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_nasiActionPerformed(evt);
}
});
btn_bakso.setText("Bakso");
btn_bakso.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_baksoActionPerformed(evt);
}
});
btn_mie.setText("Mie Ayam");
btn_mie.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_mieActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btn_nasi)
.addGap(18, 18, 18)
.addComponent(btn_mie)
.addGap(18, 18, 18)
.addComponent(btn_bakso)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn_nasi)
.addComponent(btn_bakso)
.addComponent(btn_mie))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
setBounds(150, 150, 265, 84);
}// </editor-fold>
private void btn_nasiActionPerformed(java.awt.event.ActionEvent evt) {
String str = "Nasi Rp 2.000 ,-";
try {
Socket cs = new Socket(IP, Port);
DataOutputStream dout = new DataOutputStream(cs.getOutputStream());
dout.writeUTF(str);
dout.flush();
dout.close();
cs.close();
} catch (IOException e) {
System.out.println(e);
}
}
private void btn_mieActionPerformed(java.awt.event.ActionEvent evt) {
String str = "Mie Ayam Rp 4.000 ,-";
try {
Socket cs = new Socket(IP, Port);
DataOutputStream dout = new DataOutputStream(cs.getOutputStream());
dout.writeUTF(str);
dout.flush();
dout.close();
cs.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void btn_baksoActionPerformed(java.awt.event.ActionEvent evt) {
String str = "Bakso Rp 5.000 ,-";
try {
Socket cs = new Socket(IP, Port);
DataOutputStream dout = new DataOutputStream(cs.getOutputStream());
dout.writeUTF(str);
dout.flush();
dout.close();
cs.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Client().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btn_bakso;
private javax.swing.JButton btn_mie;
private javax.swing.JButton btn_nasi;
// End of variables declaration
}
关闭Server.java
try {
ServerSocket SS = new ServerSocket(PORT);
JOptionPane.showMessageDialog(null, "Koneksi Terhubung", "Server Info", INFORMATION_MESSAGE);
Socket clientSocket = SS.accept();
DIS = new DataInputStream(clientSocket.getInputStream());
String tmp = "";
while (!tmp.equals("stop")) {
tmp = DIS.readUTF();
bon = tmp;
txa_bon.append(bon);
}
DIS.close();
SS.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
关闭Client.java
String str = "Nasi Rp 2.000 ,-";
try {
Socket cs = new Socket(IP, Port);
DataOutputStream dout = new DataOutputStream(cs.getOutputStream());
dout.writeUTF(str);
dout.flush();
dout.close();
cs.close();
} catch (IOException e) {
System.out.println(e);
}