博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
awt多线程聊天
阅读量:5377 次
发布时间:2019-06-15

本文共 4255 字,大约阅读时间需要 14 分钟。

public class ChatServer {        boolean started = false;    ServerSocket serverSocket = null;        public void start(){                //DataInputStream inputStream = null;        try {            serverSocket = new ServerSocket(8888);                    }catch(BindException e){            System.out.println("端口被占用");            System.exit(0);        }catch(IOException e){            e.printStackTrace();                    }        try {            started = true;            while(started){                Socket socket = serverSocket.accept();                Client c = new Client(socket);                new Thread(c).start();            }        }catch (IOException e) {                    }finally{            try {                serverSocket.close();            } catch (IOException e) {                e.printStackTrace();            }        }            }        class Client implements Runnable{                private DataInputStream inputStream = null;        private Socket socket = null;                private boolean connect = false;                public Client(Socket socket) {            this.socket = socket;            connect = true;            try {                inputStream = new DataInputStream(socket.getInputStream());            } catch (IOException e) {                e.printStackTrace();            }        }                public void run() {            try {                while(connect){                    String str = inputStream.readUTF();                    System.out.println("客户端发送--->"+str);                }                    }catch (EOFException e){                //客户端关闭            }catch (IOException e) {                            }finally{                try {                    if(inputStream!=null)inputStream.close();                    //客户端掉线,或关闭了                    if(socket!=null)socket.close();                } catch (IOException e1) {                    e1.printStackTrace();                }            }        }    }        public static void main(String[] args) {        ChatServer chatServer = new ChatServer();        chatServer.start();            }}
public class ChatClient extends Frame{        private static final long serialVersionUID = 287499141806289407L;    TextField textField = new TextField();        TextArea textArea = new TextArea();        Socket socket = null;        DataOutputStream outputStream = null;        public void launchFrame(){                setSize(300, 300);        setLocationRelativeTo(null);        add(textArea, BorderLayout.NORTH);        add(textField,BorderLayout.SOUTH);        pack();        addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                disconnect();                System.exit(0);            }                    });                textField.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                //TextField field = (TextField)e.getSource();                String text = textField.getText();                textArea.setText(text);                textField.setText("");                try {                    outputStream.writeUTF(text);                    outputStream.flush();                    //outputStream.close();                } catch (IOException e1) {                    e1.printStackTrace();                }                //System.out.println("文本框输入"+field.getText());            }        });                setVisible(true);        connect();    }            public void connect(){        try {            socket = new Socket("localhost", 8888);            outputStream = new DataOutputStream(socket.getOutputStream());        } catch (UnknownHostException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }            public void disconnect(){        try {            outputStream.close();            socket.close();        } catch (IOException e) {            e.printStackTrace();        }            }    public static void main(String[] args) {                ChatClient client = new ChatClient();        client.launchFrame();    }}

 

转载于:https://www.cnblogs.com/jentary/p/6403293.html

你可能感兴趣的文章
<cpp 从零学起:1>输出100内素数
查看>>
初入servlet:Allocate exception for servlet
查看>>
CSU1911 Card Game 【FWT】
查看>>
使用storyboard实现页面跳转,简单的数据传递
查看>>
Android窗口管理服务WindowManagerService显示窗口动画的原理分析
查看>>
工程android配置windows7下cocos2d-x、android的开发环境
查看>>
参数展示初始三层架构
查看>>
项目视频光盘项目中所学概览-html5+批处理+bat转exe
查看>>
hdu 1028
查看>>
思考技术 (一) —— 什么是新技术
查看>>
ubuntu更改源为aliyun的源;ROS改为新加坡源
查看>>
正则表达式入门
查看>>
Halcon标定与自标定
查看>>
Non-local Neural Networks
查看>>
apache Internal Server Error 解决方法
查看>>
Ubuntu14.04安装CUDA8.0与Cudnn5.1
查看>>
(七)STM32的RTC简单操作
查看>>
Floyd最短路算法
查看>>
【转】Java并发编程:并发容器之ConcurrentHashMap
查看>>
IIS
查看>>