码迷,mamicode.com
首页 > 编程语言 > 详细

java在线聊天项目0.6版 解决客户端关闭后异常问题 dis.readUTF()循环读取已关闭的socket

时间:2017-12-01 23:33:04      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:pen   action   jbutton   try   tput   修改   imp   回车   exce   

服务端对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

标签:pen   action   jbutton   try   tput   修改   imp   回车   exce   

原文地址:http://www.cnblogs.com/qingyundian/p/7944511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!