本文实例为大家分享了java实现多人聊天室的具体代码,供大家参考,具体内容如下
程序源代码及运行截图:
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
//server.java
package socket;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.serversocket;
import java.net.socket;
import java.util.arraylist;
import java.util.list;
import java.util.scanner;
public class server implements runnable { // 服务端
static list<socket> socketlist= new arraylist<socket>();
// 读取 in
static socket socket = null ;
static serversocket serversocket = null ;
public server() { // 构造方法
try {
serversocket = new serversocket( 9999 );
} catch (ioexception e) {
e.printstacktrace();
}
}
public static void main(string[] args) {
scanner input = new scanner(system.in);
system.out.println( "************服务端*************" );
server t = new server();
int count = 0 ;
while ( true ) {
try {
// system.out.println("端口9999等待被连接......");
socket = serversocket.accept();
count++;
system.out.println( "第" + count + "个客户已连接" );
socketlist.add(socket);
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
print p = new print(socket);
thread read = new thread(t);
thread print = new thread(p);
read.start();
print.start();
}
}
@override
public void run() {
// 重写run方法
try {
thread.sleep( 1000 );
bufferedreader in = new bufferedreader( new inputstreamreader(socket
.getinputstream()));
while ( true ) {
string jieshou = in.readline();
system.out.println( jieshou);
for ( int i = 0 ; i < socketlist.size(); i++) {
socket socket=socketlist.get(i);
printwriter out = new printwriter(socket.getoutputstream());
if (socket!= this .socket) {
out.println(jieshou);
} else {
out.println( "(你)" +jieshou);
}
out.flush();
}
}
} catch (exception e) {
e.printstacktrace();
}
}
}
class print implements runnable {
static list<socket> socketlist= new arraylist<socket>();
scanner input = new scanner(system.in);
public print(socket s) { // 构造方法
try {
socketlist.add(s);
} catch (exception e) {
e.printstacktrace();
}
}
@override
public void run() {
try {
thread.sleep( 1000 );
while ( true ) {
string msg = input.next();
for ( int i = 0 ; i < socketlist.size(); i++) {
socket socket=socketlist.get(i);
printwriter out = new printwriter(socket.getoutputstream());
// system.out.println("对客户端说:");
out.println( "服务端说:" +msg);
out.flush();
}
}
} catch (exception e) {
// todo: handle exception
e.printstacktrace();
}
}
}
|
client.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
50
51
52
53
54
55
56
57
58
59
60
61
62
|
//client.java
package socket;
import java.io.bufferedreader;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.net.socket;
import java.util.scanner;
public class client implements runnable { // 客户端
static socket socket = null ;
scanner input = new scanner(system.in);
static string name= null ;
public static void main(string[] args) {
int x=( int )(math.random()* 100 );
client.name= "client" +x;
system.out.println( "************客户端" +x+ "*************" );
try {
socket = new socket( "127.0.0.1" , 9999 );
system.out.println( "已经连上服务器了" );
} catch (exception e) {
e.printstacktrace();
}
client t = new client();
read r = new read(socket);
thread print = new thread(t);
thread read = new thread(r);
print.start();
read.start();
}
@override
public void run() {
try {
thread.sleep( 1000 );
printwriter out = new printwriter(socket.getoutputstream());
while ( true ) {
string msg = input.next();
out.println(name+ "说:" +msg);
out.flush();
}
} catch (exception e) {
e.printstacktrace();
}
}
}
class read implements runnable {
static socket socket = null ;
public read(socket socket) {
this .socket = socket;
}
@override
public void run() {
try {
thread.sleep( 1000 );
bufferedreader in = new bufferedreader( new inputstreamreader(socket
.getinputstream()));
while ( true ) {
system.out.println( in.readline());
}
} catch (exception e) {
e.printstacktrace();
}
}
}
|
测试数据一:
服务端程序运行截图:
客户端1程序运行截图:
客户端2程序运行截图:
由测试数据一可以看出:程序可以实现多人聊天,并且效果和性能还算可以。而且每个客户端连接进服务器时,都会随机产生一个随机数作为自身的标志,避免通话过程中,分辨不清彼此,而导致交流效果不好。
测试数据二:
服务端运行截图:
客户端1程序运行截图:
客户端2程序运行截图:
客户端3程序运行截图:
客户端4程序运行截图:
客户端5程序运行截图:
客户端6程序运行截图:
客户端7程序运行截图:
客户端8程序运行截图:
客户端9程序运行截图:
客户端10程序运行截图:
测试二一共连入了10个客户端进行程序稳定性的测试,程序运行结果稳定,符合实验预期结果,充分说明了程序的鲁棒性较强。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_29606255/article/details/78679815