| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1736 人关注过本帖
标题:运行服务器与客户端出现死机,求大神解!
只看楼主 加入收藏
KQY
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-7-9
结帖率:66.67%
收藏
已结贴  问题点数:18 回复次数:1 
运行服务器与客户端出现死机,求大神解!

}
程序代码:
//服务器
  package com.fs.chat02;

import import import import import public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(9999);
        int num = 1;
        while (true) {
            Socket client = server.accept();
            int a = num++;
            System.out.println("一个客户端加入群聊");
            System.out.println("群里有" + a + "个人");
            new Thread(new sonServer(client)).start();
        }
    }

    static class sonServer implements Runnable {
    private Socket client;
        private DataInputStream din;
        private DataOutputStream dout;
    public sonServer(Socket client) {
            this.client = client;
            try {
                din = new DataInputStream(client.getInputStream());
                dout = new DataOutputStream(client.getOutputStream());
            } catch (IOException e) {
                System.out.println("构造器处发生异常");
                release();
            }
        }

        private String receive() {// 接收数据
            String data = "";
            try {
                data = din.readUTF();
            } catch (IOException e) {
                System.out.println("服务器接收数据出现异常");
                release();
            }
            return data;
        }
        private void send(String msg) {// 发送数据
            try {
                dout.writeUTF(msg);
                dout.flush();
            } catch (IOException e) {
                System.out.println("服务器发送数据出现异常");
                release();
            }
        }

        private void release() {// 释放资源
            Close.close(dout, din);
        }

        public void run() {
            String data=receive();
            if(data.equals("汪洋")){
                send("登录成功");
            }else{
                send("登录失败");
            while (true) {    
                String msg=receive();
                send(msg);
                }
            }

        }

    }

}

//客户端
import import import import import import import public class Client {

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(
                System.in));
        Socket client = new Socket("localhost", 9999);
        System.out.println("请输入用户名:");
        read.readLine();
        while (true) {
            new Thread(new Send(client)).start();

            new Thread(new Receive(client)).start();
        }

    }

    static class Send implements Runnable {

        private Socket client;
        private DataOutputStream dout;

        private BufferedReader read;

        public Send(Socket client) {
            this.client = client;
            try {
                dout = new DataOutputStream(client.getOutputStream());

                read = new BufferedReader(new InputStreamReader(System.in));
            } catch (IOException e) {
                System.out.println("构造器出现异常");
            }
        }

        private void send() {// 发送消息
            try {
                String msg = read.readLine();
                dout.writeUTF(msg);
                dout.flush();
            } catch (IOException e) {
                System.out.println("服务端发送消息出现异常");
            }
        }

        private void release() {// 释放资源
            Close.close(dout, client);
        }

        public void run() {
            while (true) {
                send();
            }

        }

    }

    static class Receive implements Runnable {
        private Socket client;
        private DataInputStream din;
        public Receive(Socket client) {
            this.client = client;
            try {
                din = new DataInputStream(client.getInputStream());
            } catch (IOException e) {
                System.out.println("服务端发送消息出现异常");
            }
        }

        private String receive() {// 接收消息
            String msg = "";
            try {
                msg = din.readUTF();
            } catch (IOException e) {
                System.out.println("客户端接收消息出现异常");
            }
            return msg;
        }
        private void release() {// 释放资源
            Close.close(din, client);
        }
        public void run() {
            while (true) {
                receive();
            }
        }
    }
}

//释放资源
import import public class Close {

    public static void close(Closeable... targets) {
        for (Closeable off : targets) {
            if (off.equals("")) {
                try {
                    off.close();
                } catch (IOException e) {
                    System.out.println("释放资源出现异常");
                }
            }
        }
    }
搜索更多相关主题的帖子: import java new client private 
2019-07-27 22:58
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:18 
报什么错

剑栈风樯各苦辛,别时冰雪到时春
2019-07-29 14:33
快速回复:运行服务器与客户端出现死机,求大神解!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.070410 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved