

创建四个类,实现双向聊天的功能。
接收线程:
import java.io.IOException;
import java.net.*;
public class ReceiveThread implements Runnable{
    private int port;
    public ReceiveThread (int port){
        this.port=port;
    }
    @Override
    public void run() {
        DatagramSocket ds = null;//如果要监听,则在构造的时候就要指定需要监听的端口。
        try {
            ds = new DatagramSocket(port);
            while(true){
                byte[] buf = new byte[1024];
                int length = buf.length;
                DatagramPacket dp = new DatagramPacket(buf,length);
                try {
                    ds.receive(dp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String str = new String(dp.getData(),0,dp.getLength());
                InetAddress ip = dp.getAddress();
                System.out.println("来自:"+dp.getSocketAddress()+"的信息\n"+str+"\n-----------");
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }finally {
            ds.close();
        }
    }
}
发送线程:
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class SendThread implements Runnable{
    private int port;
    public SendThread (int port){
        this.port=port;
    }
    @Override
    public void run() {
        DatagramSocket ds = null;
        try {
            ds = new DatagramSocket();
            Scanner sc = new Scanner(System.in);
            while(true){
                //        byte[] buf = "我是王二萌remoo,你也可以叫我remoo".getBytes();
                String str = sc.next();
                if(str.equals("end"))break;
                byte[] buf = str.getBytes();
                int length = buf.length;
                InetAddress ip = null;
                try {
                    ip = InetAddress.getByName("localhost");
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                DatagramPacket dp = new DatagramPacket(buf,length,ip,port);
                try {
                    ds.send(dp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            ds.close();
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}
用户1:
public class Chat_User01 {
    public static void main(String[] args) {
        //用户1的接收端线程启动
        ReceiveThread rt = new ReceiveThread(8898);
        new Thread(rt).start();
        //用户1的发送端口线程
        SendThread st = new SendThread(8899);
        new Thread(st).start();
    }
}
用户2:
public class Chat_User02 {
    public static void main(String[] args) {
        //用户2的接收端线程启动
        ReceiveThread rt = new ReceiveThread(8899);
        new Thread(rt).start();
        //用户1的发送端口线程
        SendThread st = new SendThread(8898);
        new Thread(st).start();
    }
}
然后启动用户1、用户2:


                    
                
                
            
        
浙公网安备 33010602011771号