java控制臺實現聊天程序

本文實例為大傢分享瞭java控制臺實現聊天程序的具體代碼,供大傢參考,具體內容如下

發送端

package ip.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

public class TalkSend implements Runnable{
    DatagramSocket socket = null;
    BufferedReader reader = null;

    private int formIP;
    private   String toIP;
    private int toPort;

    public TalkSend(int formIP, String toIP, int toPort) {
        this.formIP = formIP;
        this.toIP = toIP;
        this.toPort = toPort;
        try {
            socket = new DatagramSocket();
            reader = new BufferedReader(new InputStreamReader(System.in));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void run() {

        while (true){
            try {
                String data = reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));

                socket.send(packet);
                if (data.equals("bye")){
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //關閉
        socket.close();
    }
}

服務端

package ip.chat;

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

public class TalkReceive implements Runnable{
    DatagramSocket socket = null;
    private int port;
    //設置消息來源
    private String msgFrom;

    public TalkReceive(int port ,String msgFrom) {
        this.port = port;
        this.msgFrom = msgFrom;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while(true){
            try {
                //準備接收包裹
                byte[] container = new byte[1024];

                DatagramPacket packet = new DatagramPacket(container,0,container.length);
                socket.receive(packet);//接收包裹

                //斷開連接
                byte[] data = packet.getData();
                String receiveData = new String(data, 0, data.length);

                System.out.println(msgFrom+":"+receiveData);
                if (receiveData.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        socket.close();
    }
}

學生端代碼

package ip.chat;

public class TalkStudent {
    public static void main(String[] args) {
        //開啟兩個線程
        new Thread(new TalkSend(7777,"localhost",9999)).start();
        new Thread(new TalkReceive(8888,"老師")).start();
        
    }
}

教師端代碼

package ip.chat;

public class TalkTeacher {
    public static void main(String[] args) {
        new Thread(new TalkSend(5555,"localhost",8888)).start();
        new Thread(new TalkReceive(9999,"學生")).start();
 
  }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: