首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 核心技术 >> 网络编程 >> 正文

Java Socket 编程——多线程网络聊天程序


    //服务端子线程

        public ServerThread(Socket s, Face chat, int i) {
            this.i = ++i;
            Client = s;
            //this.port = port;
            //skt = new ServerSocket(port);
            this.chat = chat;

        }

        public int getI() {
            return this.i;
        }
        public Socket getClient() {
            return this.Client;
        }
        public void run() {

            //chat.ta.append("等待连线......");

            try {
                //Client = skt.accept(); /* 接收客户连接 */

                //chat.ta.append("客户端" + Client.getInetAddress() + "已连线\n");
                theInputStream = new BufferedReader(new InputStreamReader(Client
                        .getInputStream()));
                theOutputStream = new PrintStream(Client.getOutputStream());

                while (true) {
                    readin = theInputStream.readLine();
                    chat.ta.append(readin + "\n");
                }

            } catch (SocketException e) {
                chat.ta.append("连线中断!\n");

                chat.clientBtn.setEnabled(true);
                chat.serverBtn.setEnabled(true);
                chat.tfaddress.setEnabled(true);
                chat.tfport.setEnabled(true);
                try {
                    i--;
                    skt.close();
                    Client.close();
                } catch (IOException err) {
                    chat.ta.append(err.toString());
                }
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }

        public void dataout(String data) {
            theOutputStream.println(data);
        }
    }

    //Client.java

    import java.net.*;
    import java.io.*;
    import javax.swing.Timer;
    class Client extends Thread {
        Socket skt;
        InetAddress host;
        int port;

        BufferedReader theInputStream;
        PrintStream theOutputStream;
        String readin;

        Face chat;

        public Client(String ip, int p, Face chat) {
            try {
                host = InetAddress.getByName(ip);
                port = p;
                this.chat = chat;
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }

        public void run() {
            try {
                chat.ta.append("尝试连线......");

                skt = new Socket(host, port);
                chat.ta.append("连线成功\n");

                theInputStream = new BufferedReader(new InputStreamReader(skt
                        .getInputStream()));

                theOutputStream = new PrintStream(skt.getOutputStream());
                //Timer myTimer = new Timer();
                while (true) {
                    readin = theInputStream.readLine();
                    chat.ta.append(readin + "\n");
                }
            } catch (SocketException e) {
                chat.ta.append("连线中断!\n");
                chat.clientBtn.setEnabled(true);
                chat.serverBtn.setEnabled(true);
                chat.tfaddress.setEnabled(true);
                chat.tfport.setEnabled(true);
                try {
                    skt.close();
                } catch (IOException err) {
                    chat.ta.append(err.toString());
                }
            } catch (IOException e) {
                chat.ta.append(e.toString());
            }
        }

        public void dataout(String data) {
            theOutputStream.println(data);
        }
    }

上一页  [1] [2] [3] 下一页

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航