服务器之家

服务器之家 > 正文

java控制台实现聊天程序

时间:2021-10-06 01:11     来源/作者:wuhuizzj

本文实例为大家分享了java控制台实现聊天程序的具体代码,供大家参考,具体内容如下

发送端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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();
    }
}

服务端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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();
    }
}

学生端代码

?
1
2
3
4
5
6
7
8
9
10
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();
        
    }
}

教师端代码

?
1
2
3
4
5
6
7
8
9
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();
 
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wuhuizzj/article/details/108985608

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部