多线程通信,先后顺序聊天室

18 篇文章 0 订阅
8 篇文章 0 订阅

1.发送客户端代码

package com.dasenlin.threadchart;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * 多线程的发送客户端
 * @author Administrator
 *
 */
public class Send implements Runnable{
    private BufferedReader console;//控制台的输入流

    private DataOutputStream dos;//管道的输出流

    private boolean isRunning =true;//控制线程的标志符

    public Send(){
        console = new BufferedReader(new InputStreamReader(System.in));
    }
    /**
     * 发送控制台的输入
     * @param client
     */
    public Send(Socket client){
        this();//调用的是这里的无参默认的构造方法,必须是第一句
        try {
            dos = new DataOutputStream(client.getOutputStream());
        } catch (IOException e) {
            isRunning =false;
            IoCloserUtil.closeAll(dos,console);
        }
    }
    /**
     * 从控制台获取数据信息
     * @return
     */
    public String getMsgFromConsole(){
        try {
            return console.readLine();
        } catch (IOException e) {

        }
        return "";
    }
    /**
     * 从控制台获取数据
     * 从客户端发送出去
     */
    public void send(){
        String msg = getMsgFromConsole();
        try {
            if(null!=msg && !msg.equals("")){
                dos.writeUTF(msg);  
                dos.flush();
            }
        } catch (IOException e) {
            isRunning =false;
            IoCloserUtil.closeAll(dos,console);
        }
    }
    /**
     * 循环而已
     */
    @Override
    public void run() {
        //线程体
        while(isRunning){
            send();
        }
    }
}

2.工具类

package com.dasenlin.threadchart;

import java.io.Closeable;
import java.io.IOException;

/**
 * 关闭流
 * @author Administrator
 *
 */
public class IoCloserUtil {
    /**
     * 关闭所有流的方法
     * @param io
     */
    public static void closeAll(Closeable...io){
        for(Closeable temp:io){
                try {
                    if(null!=temp){
                        temp.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

3.客户端启动代码

package com.dasenlin.threadchart;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 客户端
 * @author Administrator
 *
 */
public class Client {

    public static void main(String[] args) {
        try {
            Socket client = new Socket("localhost",9999);
            new Thread(new Send(client)).start();//只需要给一个线程,实例化一个Send();然后调用线程的start()方法;
            new Thread(new Recieve(client)).start();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

4.接收端代码

package com.dasenlin.threadchart;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 接收端的封装
 * @author Administrator
 *
 */
public class Recieve implements Runnable{
    private DataInputStream dis;
    private Boolean isRunning=true;
    public Recieve(){

    }
    public Recieve(Socket client){
        try {
            dis = new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            isRunning=false;
            IoCloserUtil.closeAll(dis);
        }
    }
    /**
     * 接收信息
     * @return
     */
    public String recieve(){
        String msg="";
        try {
            msg = dis.readUTF();
        } catch (IOException e) {
            isRunning=false;
            IoCloserUtil.closeAll(dis);
        }
        return msg;
    }
    /**
     * 读取这些数据
     */
    @Override
    public void run() {
        while(isRunning){
            System.out.println(recieve());
        }
    }

}

5.服务器端

package com.dasenlin.threadchart;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端
 * @author Administrator
 *
 */
public class Server {

    public static void main(String[] args) {
        try {
            while(true){
                ServerSocket server = new ServerSocket(9999);
                Socket client = server.accept();
                //写出数据,输入流
                DataInputStream dis = new DataInputStream(client.getInputStream());
                DataOutputStream dos =  new DataOutputStream(client.getOutputStream());
                while(true){
                    String msg=dis.readUTF();
                    System.out.println(msg);
                    dos.writeUTF("服务器"+msg);
                    dos.flush();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值