| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
Reworld,下班在家制作游戏,1500万奖金等你拿编程微博 - 发现编程界的新鲜事
共有 217 人关注过本帖
标题:运行服务器与客户端出现死机,求大神解!
只看楼主 加入收藏
KQY
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-7-9
结帖率:66.67%
  已结贴   问题点数:18  回复次数:1   
运行服务器与客户端出现死机,求大神解!

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

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
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 java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
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 java.io.Closeable;
import java.io.IOException;

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: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:132
帖 子:1933
专家分:8543
注 册:2015-3-19
  得分:18 
报什么错

知命以下无敌
2019-07-29 14:33
快速回复:运行服务器与客户端出现死机,求大神解!
数据加载中...
 
   



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

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