本文实例为大家分享了java基于c/s结构实现多线程聊天室的具体代码,供大家参考,具体内容如下
主要实现的功能:
服务器端建立serversocket阻塞监听来自客户端的socket连接,并为之开辟一个新的线程
读取来自该连接的数据,广播每一个客户端数据,这里简单地使用一个链表保存所有来自客户端的所有socket连接
客户端连接上服务器端后主要有两个线程在工作:
主线程:不断获取键盘的输入并写入该socket中传输给服务器
副线程:不断从服务器socket流中读取传来的数据,打印到屏幕上。
服务器端代码:
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
|
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printstream; import java.net.socket; import java.net.serversocket; import java.util.arraylist; import javax.swing.joptionpane; public class myserver { public static arraylist<socket> socketlist = new arraylist<socket>(); private static string port ; public static void main(string[] args) throws ioexception { //弹出一个对话框输入端口号 port = joptionpane.showinputdialog( "input the port number: " ); int serverport = new integer(port).intvalue(); serversocket ss = new serversocket(serverport); system.out.println( "server is initializating..." ); while ( true ) { system.out.println( "server is waiting..." ); //此处将阻塞监听 socket s = ss.accept(); system.out.println( "listening from: " + s.getinetaddress()); socketlist.add(s); new thread( new serverthread(s)).start(); } } } class serverthread implements runnable { socket s = null ; bufferedreader br = null ; public serverthread(socket s) throws ioexception { this .s = s; br = new bufferedreader( new inputstreamreader(s.getinputstream())); } public void run() { try { string content = null ; while ( (content = readfromclient()) != null ) { //播报每个客户端数据 for (socket s : myserver.socketlist) { printstream ps = new printstream(s.getoutputstream()); ps.println(content); } } } catch (ioexception io) { io.printstacktrace(); } } private string readfromclient() { try { return br.readline(); } catch (ioexception io) { myserver.socketlist.remove(s); system.out.println(s.getinetaddress() + " is disconnecting..." ); } return null ; } } |
客户端代码:
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
|
import java.net.socket; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printstream; public class myclient { public static void main(string[] args) throws exception { socket s = new socket( "192.168.1.164" , 30000 ); // new thread to read content from server. new thread( new clientthread(s)).start(); printstream ps = new printstream(s.getoutputstream()); string line = null ; bufferedreader br = new bufferedreader( new inputstreamreader(system.in)); while ((line = br.readline()) != null ) { ps.println(line); } } } class clientthread implements runnable { private socket s = null ; bufferedreader br = null ; public clientthread(socket s) throws ioexception { this .s = s; br = new bufferedreader( new inputstreamreader(s.getinputstream())); } public void run() { try { string content = null ; while ( (content = br.readline()) != null ) { system.out.println(content); } } catch (ioexception io) { io.printstacktrace(); } } } |
后期开发:
上面程序功能很简单,没有记录客户信息,考虑添加功能如下:(多人聊天室)
客户端发来的信息必须添加特殊标识,用于区别 登陆,私聊,公聊 三种,如果是登陆,则服务器端应该有一个map来保存用户名和对应输出流中间的关系,用来处理用户名重复的情况,还有如果是私聊,必须知道从客户端发来消息的用户名和将要发给哪一个用户的特殊标识,考虑在输入字符串里加入特殊标识符。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiaozhuaixifu/article/details/13006403