基于java UDP/TCP的在线聊天室

基于java UDP/TCP的在线聊天室

一、UDP

/**
 * 发送接收不分离,只用一个DatagramSocket
 *
 */

public class Machine{
    private  InetSocketAddress address;
    private  int machinePort;

    public void setMachinePort(int machinePort){
        this.machinePort=machinePort;
    }
    public void setDestinAddress(String hostName,int hostPort){
        this.address=new InetSocketAddress(hostName, hostPort);
    }

    public  void Running() throws Exception {

        DatagramSocket socket = new DatagramSocket(machinePort);

        //接收
        new Thread(()->{
            while (true) {
                byte[] buff = new byte[1024];
                DatagramPacket packet = new DatagramPacket(buff, 0, buff.length);
                try {
                    socket.receive(packet);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                byte[] info = packet.getData();
                String s = new String(info, 0, info.length);
                System.out.println(s);
            }
        }).start();

        //发送
        new Thread(()->{
            while (true) {
                String info = new Scanner(System.in).next();
                byte[] bytes = info.getBytes();
                DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, address);
                try {
                    socket.send(packet);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

二、TCP

public class TCPMachine {
    private int receivePort;
    private String destinHostName;
    private Integer destinHostPort;


    public void setReceivePort(int receivePort) {
        this.receivePort = receivePort;
    }

    public void setDestinHostName(String destinHostName) {
        this.destinHostName = destinHostName;
    }

    public void setDestinHostPort(Integer destinHostPort) {
        this.destinHostPort = destinHostPort;
    }

    public  void running() throws InterruptedException {

        new Thread(() -> {
            try {
                ServerSocket socket = new ServerSocket(receivePort);  //接收方
                while (true) {
                    byte[] buff = new byte[1024 * 1024];
                    Socket accept = socket.accept();
                    InputStream is = accept.getInputStream();
                    int len = is.read(buff);
                    String info = new String(buff, 0, len);
                    System.out.println(info);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        Thread.sleep(1000);
        new Thread(() -> {
            try {
                while (true) {
                    Scanner scanner = new Scanner(System.in);
                    String info = scanner.next();

                    Socket socket = new Socket(destinHostName,destinHostPort);
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    writer.write(info);
                    writer.flush();
                    writer.close();
                    socket.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值