1. 程式人生 > >java在線聊天項目0.6版 解決客戶端關閉後異常問題 dis.readUTF()循環讀取已關閉的socket

java在線聊天項目0.6版 解決客戶端關閉後異常問題 dis.readUTF()循環讀取已關閉的socket

異常 oid jpanel read pen false tput ets java.net

服務端對try catch finally重新進行了定義,當發生異常,主動提示,或關閉出現異常的socket

服務器端代碼修改如下:

package com.swift;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {

    public static void main(String[] args) {
        boolean started=false
; ServerSocket ss=null; DataInputStream dis=null; Socket s = null; try { ss = new ServerSocket(8888); started=true; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
try { while(started) { boolean connected=false; s = ss.accept(); connected=true; System.out.println("a client connected success"); dis=new DataInputStream(s.getInputStream()); while(connected) { String str
=dis.readUTF(); System.out.println(str); } } } catch (Exception e) { //e.printStackTrace(); System.out.println("client has closed."); }finally { if(dis!=null) { try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } if(s!=null) { try { s.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

客戶端換了一個OutputStream,使用DataOutputStream發送信息,dos.writeUTF(tf.getText());

0.6版效果圖

技術分享圖片

客戶端代碼修改如下:

package com.swift;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatClientFrame extends JFrame {

    private static final long serialVersionUID = -118470059355655240L;
    Socket s=null;
    DataOutputStream dos=null;
    JLabel label_shang=new JLabel();
    JLabel label_xia=new JLabel();
    JTextField tf = new JTextField(38);
    JTextArea ta = new JTextArea(15, 50);
    JButton button=new JButton();

    public ChatClientFrame() {
        setBounds(200, 200, 500, 400);
        setTitle("客戶端聊天工具 —— 0.6");
        //對窗口進行大的布局,分為三行一列,在pBasic面板上添加三個面板shang zhong xia
        JPanel pBasic=new JPanel();
        pBasic.setLayout(new BorderLayout());//不設置默認也是這種布局模式
        setContentPane(pBasic);//把面板放在窗口上,不記得用this.關鍵字
        JPanel shang=new JPanel();
        JPanel zhong=new JPanel();
        JPanel xia=new JPanel();
        //設置JPanel面板的大小
        shang.setSize(470, 25);
        zhong.setSize(470, 180);
        xia.setSize(470, 40);
        pBasic.add(shang,BorderLayout.NORTH);
        pBasic.add(zhong,BorderLayout.CENTER);
        pBasic.add(xia,BorderLayout.SOUTH);
        shang.setBackground(Color.red);
        zhong.setBackground(Color.yellow);
        xia.setBackground(Color.blue);

        label_shang.setText("聊天記錄");
        shang.add(label_shang);
        ta.setLineWrap(true);// 自動換行
        JScrollPane scroll=new JScrollPane(ta);// 增加滾動條,以便不增加行數
        zhong.add(scroll);
        label_xia.setText("輸入信息");
        xia.add(label_xia,BorderLayout.WEST);
        /*
         * 增加功能,窗口監聽事件,窗口打開時設置光標焦點在tf文本域中
         */
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                tf.requestFocus();
            }
        });
        xia.add(tf,BorderLayout.CENTER);
        button.setText("發送");
        xia.add(button,BorderLayout.EAST);

        final class ShareListener implements ActionListener{

            @Override
            public void actionPerformed(ActionEvent e) {
                String taText=ta.getText();
                String tfText=tf.getText()+"\r\n";
                String tfText1=tf.getText();
                ta.setText(taText+tfText);
                tf.setText("");
                //當回車或發送按鈕時,tfText發送到服務器
                try {
                    dos.writeUTF(tfText1);
                    dos.flush();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                
            }
        }
        button.addActionListener(new ShareListener());
        tf.addActionListener(new ShareListener());
        //通過壓縮自動調整各個面板
        pack();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                disconnect();
                System.exit(0);
            }
        });
        setVisible(true);
        //創建窗體直接調用連接服務器
        connect();
    }
    
    public void connect() {
        try {
            s=new Socket("127.0.0.1",8888);
            System.out.println("connected!");
            dos=new DataOutputStream(s.getOutputStream());
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void disconnect() {
        try {
            dos.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    public static void main(String[] args) {
        new ChatClientFrame();
    }

}

java在線聊天項目0.6版 解決客戶端關閉後異常問題 dis.readUTF()循環讀取已關閉的socket