客户端程序:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
import java.util.concurrent.*;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class client extends JFrame {
private ObjectInputStream m_input;
private ObjectOutputStream m_output;
private JTextField m_enter;
private JTextArea m_display;
private int m_clientNumber = 0;
public client() throws IOException {
super("聊天程序客户端");
Container c = getContentPane();
m_enter = new JTextField();
m_enter.setEnabled(false);
m_enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String s = event.getActionCommand();
DesEncrypt des = new DesEncrypt(); // 实例化一个对像
des.getKey("aadd"); // 生成密匙
String strEnc = des.getEncString(s);// 加密字符串,返回String的密文
m_output.writeObject(strEnc);
m_output.flush();
mb_displayAppend("客户端" + s);
m_enter.setText("");
} catch (Exception e) {
System.err.println("发生异常:" + e);
e.printStackTrace();
}
}
});
c.add(m_enter, BorderLayout.NORTH);
m_display = new JTextArea();
c.add(new JScrollPane(m_display), BorderLayout.CENTER);
}
public void mb_displayAppend(String s) {
m_display.append(s + "/n");
m_display.setCaretPosition(m_display.getText().length());
m_enter.requestFocusInWindow();
}
public boolean mb_isEndSession(String m) {
if (m.equalsIgnoreCase("q"))
return (true);
if (m.equalsIgnoreCase("quit"))
return (true);
if (m.equalsIgnoreCase("exit"))
return (true);
if (m.equalsIgnoreCase("end"))
return (true);
if (m.equalsIgnoreCase("结束"))
return (true);
return (false);
}
public void mb_run(String host, int port) {
try {
mb_displayAppend("尝试连接 ");
Socket s = new Socket(host, port);
String m;
String strDes;
String senq;
m_output = new ObjectOutputStream(s.getOutputStream());
m_input = new ObjectInputStream(s.getInputStream());
m_enter.setEnabled(true);
m = (String) m_input.readObject();
mb_displayAppend(m);
do {
m = (String) m_input.readObject();
DesEncrypt des = new DesEncrypt(); // 实例化一个对像
des.getKey("aadd"); // 生成密匙
strDes = des.getDesString(m); // 把String 类型的密文解密
senq=des.getEncString("q");
mb_displayAppend("解密前服务器发送信息为:" + m);
mb_displayAppend("服务器发送信息为:" + strDes);
} while (!mb_isEndSession(strDes));
m_output.writeObject(senq);
m_output.flush();
m_output.close();
System.exit(0);
} catch (Exception e) {
System.err.println("发生异常:" + e);
e.printStackTrace();
mb_displayAppend("连接[" + m_clientNumber + "]异常");
}
}
public static void main(String[] args) throws IOException {
client app = new client();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(500, 400);
app.setVisible(true);
if (args.length == 0)
app.mb_run("localhost", 5000);
else
app.mb_run(args[0], 5000);
}
}