• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: Java
  • 标签: java  Socket  

资源简介

这个java程序以Eclipse为环境运行,可以支持多人聊天。 采用图形用户界面,能够很好的实现通信!

资源截图

代码片段和文件信息

package csu.com.socket;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

public class ChatClient extends frame {

        Socket s = null;

        DataOutputStream dos = null;

        DataInputStream dis = null;

        private boolean bConnected = false;

        TextField tfTxt = new TextField();

        TextArea taContent = new TextArea();

        Thread tRecv = new Thread(new RecvThread());

        public static void main(String[] args) {

                new ChatClient().launchframe();

        }

        public void launchframe() {

                setLocation(400 300);

                this.setSize(300 300);

                add(tfTxt BorderLayout.SOUTH);

                add(taContent BorderLayout.NORTH);

                pack();

                this.addWindowListener(new WindowAdapter() {

                        public void windowClosing(WindowEvent arg0) {

                                disconnect();

                                System.exit(0);

                        }

                });

                tfTxt.addActionListener(new TFListener());

                setVisible(true);

                connect();

                tRecv.start();

        }

        public void connect() {

                try {

                        s = new Socket(“127.0.0.1“ 8888);

                        dos = new DataOutputStream(s.getOutputStream());

                        dis = new DataInputStream(s.getInputStream());

                        bConnected = true;

                } catch (UnknownHostException e) {

                        e.printStackTrace();

                } catch (IOException e) {

                        e.printStackTrace();

                }

        }

        public void disconnect() {

                try {

                        dos.close();

                        dis.close();

                        s.close();

                } catch (IOException e) {}

        }

        private class TFListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {

                        String str = tfTxt.getText().trim();

                        tfTxt.setText(““);

                        try {

                                dos.writeUTF(str);

                                dos.flush();

                        } catch (IOException e1) {}

                }

        }

        private class RecvThread implements Runnable {

                public void run() {

                        try {

                                while(bConnected) {

                                        String str = dis.readUTF();

                                        taContent.setText(taContent.getText() + str + ‘\n‘);

                                }

                        } catch (IOException e) {}

   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3038  2010-12-25 18:08  socket\ChatClient.java

     文件       3590  2011-02-19 21:26  socket\ChatServer.java

     目录          0  2011-02-20 00:17  socket

----------- ---------  ---------- -----  ----

                 6628                    3


评论

共有 条评论