Java实现在线聊天功能

效果

关键代码

创建Client.java

COPY
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

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 07-04 22:13
*
* 1. SendThread 发送消息线程
* 2. RecieveThread 接受消息线程
*/
public class Client {
public static void main(String[] args) {

try {
// 创建8888端口
Socket s = new Socket("127.0.0.1", 8888);

// 启动发送消息线程
new SendThread(s).start();
// 启动接受消息线程
new RecieveThread(s).start();

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

创建Server.java

COPY
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

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 07-04 22:13
*
* 1. SendThread 发送消息线程
* 2. RecieveThread 接受消息线程
*/
public class Server {
public static void main(String[] args) {
try {

// 监听8888端口
ServerSocket ss = new ServerSocket(8888);

System.out.println("监听在端口号:8888");
Socket s = ss.accept();

//启动发送消息线程
new SendThread(s).start();
//启动接受消息线程
new RecieveThread(s).start();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

创建RecieveThread.java

COPY
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

import java.io.*;
import java.net.Socket;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 07-04 22:13
*
* 1. SendThread 发送消息线程
* 2. RecieveThread 接受消息线程
*/
public class RecieveThread extends Thread {

private Socket s;

public RecieveThread(Socket s) {
this.s = s;
}

public void run() {
try {

// 接收对方输入的内容
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);

while (true) {
String msg = dis.readUTF();
System.out.println(msg);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

创建SendThread.java

COPY
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

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
* @author Lete
* @乐特的程序永无BUG
* @createDate 2020- 07-04 22:13
*
* 1. SendThread 发送消息线程
* 2. RecieveThread 接受消息线程
*/
public class SendThread extends Thread {

private Socket s;

public SendThread(Socket s) {
this.s = s;
}

public void run() {
try {

// 获取输入的内容
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);

while (true) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
dos.writeUTF(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
Authorship: Lete乐特
Article Link: https://blog.imlete.cn/article/javachat.html
Copyright: All posts on this blog are licensed under the CC BY-NC-SA 4.0 license unless otherwise stated. Please cite Lete乐特 's Blog !