- //Client.java
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- class RecvMegThread extends Thread
- {
- DataInputStream in;
- DataOutputStream out;
- Socket ServerSock;
- javax.swing.JTextArea TextArea;
- Client MainDialog;
- RecvMegThread(Client dlg)
- {
- MainDialog = dlg;
- ServerSock = dlg.ServerSock;
- TextArea = dlg.jTextArea1;
- try
- {
- in = new DataInputStream(ServerSock.getInputStream());
- out = new DataOutputStream(ServerSock.getOutputStream());
- }
- catch (IOException ex)
- { }
- start();
- }
- public void run()
- {
- while(true)
- {
- try
- {
- String word = in.readUTF();
- TextArea.setText(TextArea.getText() + word + "\n");
- }
- catch (IOException ex)
- {
- TextArea.setText(TextArea.getText() + "和服务器失去了联系!!" + "\n");
- MainDialog.ServerSock = null;
- return;
- }
- }
- }
- }
- public class Client extends javax.swing.JDialog implements ActionListener
- {
- private javax.swing.JTextField IP;
- private javax.swing.JTextField Port;
- private javax.swing.JTextField Username;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JScrollPane jScrollPane1;
- public javax.swing.JTextArea jTextArea1;
- private javax.swing.JTextField jTextField1;
- Socket ServerSock;
- String username;
- public Client(java.awt.Frame parent, boolean modal)
- {
- super(parent, modal);
- initComponents();
- }
- @SuppressWarnings("unchecked")
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- jTextField1 = new javax.swing.JTextField();
- jButton1 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- IP = new javax.swing.JTextField();
- jLabel2 = new javax.swing.JLabel();
- Port = new javax.swing.JTextField();
- jLabel3 = new javax.swing.JLabel();
- Username = new javax.swing.JTextField();
- jButton2 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
- getContentPane().setLayout(null);
- jTextArea1.setColumns(20);
- jTextArea1.setEditable(false);
- jTextArea1.setRows(5);
- jScrollPane1.setViewportView(jTextArea1);
- getContentPane().add(jScrollPane1);
- jScrollPane1.setBounds(10, 40, 570, 220);
- getContentPane().add(jTextField1);
- jTextField1.setBounds(10, 270, 460, 21);
- jButton1.setText("发送");
- jButton1.addActionListener(this);
- getContentPane().add(jButton1);
- jButton1.setBounds(495, 270, 80, 25);
- jLabel1.setText("服务器IP:");
- getContentPane().add(jLabel1);
- jLabel1.setBounds(10, 10, 70, 20);
- IP.setText("127.0.0.1");
- getContentPane().add(IP);
- IP.setBounds(70, 10, 100, 21);
- jLabel2.setText("端口:");
- getContentPane().add(jLabel2);
- jLabel2.setBounds(180, 10, 50, 20);
- Port.setText("8888");
- getContentPane().add(Port);
- Port.setBounds(220, 10, 70, 21);
- jLabel3.setText("用户名:");
- getContentPane().add(jLabel3);
- jLabel3.setBounds(300, 10, 70, 20);
- Username.setText("无名氏");
- getContentPane().add(Username);
- Username.setBounds(360, 10, 100, 21);
- jButton2.setText("连接");
- jButton2.addActionListener(this);
- jTextField1.addActionListener(this);
- getContentPane().add(jButton2);
- jButton2.setBounds(490, 10, 90, 25);
- pack();
- }
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- if ((evt.getSource()==jButton1)||(evt.getSource()==jTextField1))
- {
- if(ServerSock == null)
- {
- jTextArea1.setText(jTextArea1.getText() + "没有连接服务器!!" + "\n");
- return;
- }
- try
- {
- DataOutputStream out = new DataOutputStream(ServerSock.getOutputStream());
- if(jTextField1.getText() != null)
- out.writeUTF(username + ": " + jTextField1.getText());
- }
- catch (IOException ex)
- { }
- }
- if( evt.getSource()==jButton2 )
- {
- if(IP.getText().equals("") || Port.getText().equals("") )
- {
- this.jTextArea1.setText(this.jTextArea1.getText() + "IP 和 端口不能为空!!\n");
- return;
- }
- try
- {
- Integer.parseInt(Port.getText());
- }
- catch(Exception e)
- {
- this.jTextArea1.setText(this.jTextArea1.getText() + "端口号应为数字!!\n");
- return;
- }
- try
- {
- if(ServerSock != null)
- {
- String str = "" + ServerSock.getInetAddress() + ServerSock.getPort();
- System.out.println(str);
- System.out.println("/" + IP.getText() + Port.getText());
- if(str.equals("/" + IP.getText() + Port.getText()))
- {
- this.jTextArea1.setText(this.jTextArea1.getText() + "已经连接了服务器!!\n");
- return;
- }
- }
- ServerSock = new Socket(IP.getText(), Integer.parseInt(Port.getText()));
- username = Username.getText();
- new RecvMegThread(this);
- }
- catch (Exception ex)
- {
- jTextArea1.setText(jTextArea1.getText() + "连接不上服务器!!\n");
- return;
- }
- }
- }
- public static void main(String args[])
- {
-
- Client dlg = new Client(null,true);
- dlg.setBounds(300, 200, 605, 340);
- dlg.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- dlg.setVisible(true);
- }
- }
复制代码 |