本文實例為大家分享了java實現(xiàn)聊天室的具體代碼,供大家參考,具體內(nèi)容如下
聊天室界面:
源碼:
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
public class clientframe extends frame { private textfield textfieldcontent = new textfield(); private textarea textareacontent = new textarea(); private socket socket = null ; private outputstream out = null ; private dataoutputstream dos = null ; private inputstream in = null ; private datainputstream dis = null ; private boolean flag = false ; /** * 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午9:19:51 功能:啟動客戶端程序 * * @param args */ public static void main(string[] args) { new clientframe().init(); } /** * 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午9:20:43 功能:對窗口進(jìn)行初始化 */ private void init() { this .setsize( 300 , 300 ); setlocation( 250 , 150 ); setvisible( true ); settitle( "wechatroom" ); // 添加控件 this .add(textareacontent); this .add(textfieldcontent, borderlayout.south); textareacontent.setfocusable( false ); pack(); // 關(guān)閉事件 addwindowlistener( new windowadapter() { public void windowclosing(windowevent e) { system.out.println( "用戶試圖關(guān)閉窗口" ); disconnect(); system.exit( 0 ); } }); // textfieldcontent添加回車事件 textfieldcontent.addactionlistener( new actionlistener() { public void actionperformed(actionevent e) { onclickenter(); } }); // 建立連接 connect(); new thread( new recivemessage()).start(); } private class recivemessage implements runnable { @override public void run() { flag = true ; try { while (flag) { string message = dis.readutf(); textareacontent.append(message + "\n" ); } } catch (eofexception e) { flag = false ; system.out.println( "客戶端已關(guān)閉" ); // e.printstacktrace(); } catch (socketexception e) { flag = false ; system.out.println( "客戶端已關(guān)閉" ); // e.printstacktrace(); } catch (ioexception e) { flag = false ; system.out.println( "接受消息失敗" ); e.printstacktrace(); } } } /** * 功能:當(dāng)點擊回車時出發(fā)的事件 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午9:49:30 */ private void onclickenter() { string message = textfieldcontent.gettext().trim(); if (message != null && !message.equals( "" )) { string time = new simpledateformat( "h:m:s" ).format( new date()); textareacontent.append(time + "\n" + message + "\n" ); textfieldcontent.settext( "" ); sendmessagetoserver(message); } } /** * 功能:給服務(wù)器發(fā)送消息 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:13:48 * * @param message */ private void sendmessagetoserver(string message) { try { dos.writeutf(message); dos.flush(); } catch (ioexception e) { system.out.println( "發(fā)送消息失敗" ); e.printstacktrace(); } } /** * 功能:申請socket鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:00:38 */ private void connect() { try { socket = new socket( "localhost" , 8888 ); out = socket.getoutputstream(); dos = new dataoutputstream(out); in = socket.getinputstream(); dis = new datainputstream(in); } catch (unknownhostexception e) { system.out.println( "申請鏈接失敗" ); e.printstacktrace(); } catch (ioexception e) { system.out.println( "申請鏈接失敗" ); e.printstacktrace(); } } /** * 功能:關(guān)閉流和鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:01:32 */ private void disconnect() { flag = false ; if (dos != null ) { try { dos.close(); } catch (ioexception e) { system.out.println( "dos關(guān)閉失敗" ); e.printstacktrace(); } } if (out != null ) { try { out.close(); } catch (ioexception e) { system.out.println( "dos關(guān)閉失敗" ); e.printstacktrace(); } } if (socket != null ) { try { socket.close(); } catch (ioexception e) { system.out.println( "socket關(guān)閉失敗" ); e.printstacktrace(); } ; } } } |
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package com.chat; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.eofexception; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.bindexception; import java.net.serversocket; import java.net.socket; import java.net.socketexception; import java.util.arraylist; import java.util.list; public class chatserver { private list<client> clients = new arraylist<>(); /** * 功能:啟動chatsever 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:26:41 * * @param args */ public static void main(string[] args) { new chatserver().init(); } /** * 功能:對chatserver初始化 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午10:27:09 */ private void init() { system.out.println( "服務(wù)器已開啟" ); // bindexception serversocket ss = null ; socket socket = null ; try { ss = new serversocket( 8888 ); } catch (bindexception e) { system.out.println( "端口已被占用" ); e.printstacktrace(); } catch (ioexception e1) { e1.printstacktrace(); } try { client client = null ; while ( true ) { socket = ss.accept(); system.out.println( "客戶駕到" ); client = new client(socket); clients.add(client); new thread(client).start(); } } catch (ioexception e) { e.printstacktrace(); } } private class client implements runnable { private socket socket = null ; inputstream in = null ; datainputstream din = null ; outputstream out = null ; dataoutputstream dos = null ; boolean flag = true ; public client(socket socket) { this .socket = socket; try { in = socket.getinputstream(); din = new datainputstream(in); } catch (ioexception e) { system.out.println( "接受消息失敗" ); e.printstacktrace(); } } public void run() { string message; try { while (flag) { message = din.readutf(); // system.out.println("客戶說:" + message); forwordtoallclients(message); } } catch (socketexception e) { flag = false ; system.out.println( "客戶下線" ); clients.remove( this ); // e.printstacktrace(); } catch (eofexception e) { flag = false ; system.out.println( "客戶下線" ); clients.remove( this ); // e.printstacktrace(); } catch (ioexception e) { flag = false ; system.out.println( "接受消息失敗" ); clients.remove( this ); e.printstacktrace(); } if (din != null ) { try { din.close(); } catch (ioexception e) { system.out.println( "din關(guān)閉失敗" ); e.printstacktrace(); } } if (in != null ) { try { in.close(); } catch (ioexception e) { system.out.println( "din關(guān)閉失敗" ); e.printstacktrace(); } } if (socket != null ) { try { socket.close(); } catch (ioexception e) { system.out.println( "din關(guān)閉失敗" ); e.printstacktrace(); } } } /** * 功能:轉(zhuǎn)發(fā)給所有客戶端 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午11:11:59 * * @param message * @throws ioexception */ private void forwordtoallclients(string message) throws ioexception { for (client c : clients) { if (c != this ) { out = c.socket.getoutputstream(); dos = new dataoutputstream(out); forwordtoclient(message); } } } /** * 功能:發(fā)送給一個客戶端 學(xué)校:山東師范大學(xué) 程序員:外力_victor 日期:2016年5月8日 上午11:16:12 * * @throws ioexception */ private void forwordtoclient(string message) throws ioexception { dos.writeutf(message); dos.flush(); system.out.println( "轉(zhuǎn)發(fā)成功!" ); } } } |
源碼下載:java聊天室代碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_24082497/article/details/51347426