键盘输入聊天

package com.udp.test;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDPClient1 {

public static void main(String[] args) {
    try {
        DatagramSocket ds = new DatagramSocket(5555);
        //创建接收线程
        new UDPReceiveThread(ds).start();
        new UDPSendThread(ds, InetAddress.getByName("localhost"), 9999).start();
        //创建发送线程
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

}

package com.udp.test;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDPClient2 {

public static void main(String[] args) {
    try {
        DatagramSocket ds = new DatagramSocket(9999);
        //创建接收线程
        new UDPReceiveThread(ds).start();
        new UDPSendThread(ds, InetAddress.getByName("localhost"), 5555).start();
        //创建发送线程
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

}

package com.udp.test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
* 用来接收的线程
*
*/
public class UDPReceiveThread extends Thread{

//需要一个套接字
private DatagramSocket ds ;

public UDPReceiveThread(DatagramSocket ds) {
    super();
    this.ds = ds;
}

@Override
public void run() {
    //接收的数据源
    byte b[] = new byte[1024];
    //接收的数据包
    DatagramPacket dp = new DatagramPacket(b, b.length);

        //不停的接收
        while(true){
            try {
                ds.receive(dp);
                String str = new String(dp.getData(), 0, dp.getLength());
                System.out.println(str);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


}

}

package com.udp.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSendThread extends Thread{

//需要有一个套接字
private DatagramSocket ds;
//要知道对方的IP地址
private InetAddress ia;
//需要知道对方的端口号
private int port;

public DatagramSocket getDs() {
    return ds;
}

public void setDs(DatagramSocket ds) {
    this.ds = ds;
}

public InetAddress getIa() {
    return ia;
}

public void setIa(InetAddress ia) {
    this.ia = ia;
}

public int getPort() {
    return port;
}

public void setPort(int port) {
    this.port = port;
}

public UDPSendThread(DatagramSocket ds, InetAddress ia, int port) {
    super();
    this.ds = ds;
    this.ia = ia;
    this.port = port;
}

@Override
public void run() {
    //从键盘输入
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while(true){
        try {
            String line = br.readLine(); //读取一行
            byte b[] = line.getBytes();
            //创建数据包
            DatagramPacket dp = new DatagramPacket(b, b.length, ia, port);
            //发送
            ds.send(dp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值