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
|
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; /** * */ class
Chat extends
Thread { private
Socket socket; private
List<Socket> socketList; private
int
count; public
Chat( int
count, Socket socket, List<Socket> socketList) { this .count
= count; this .socket
= socket; this .socketList
= socketList; } public
void
run() { BufferedReader
reader = null ; PrintWriter
writer = null ; try
{ reader
= new
BufferedReader( new
InputStreamReader( socket.getInputStream())); String
message = null ; while
( true )
{ message
= reader.readLine(); //
接收到客户端的bye信息,客户端即将退出,并将bye写入到该客户端 if
(message.equals( "bye" ))
{ writer
= new
PrintWriter(socket.getOutputStream()); writer.println( "bye" ); writer.flush(); continue ; } //
向所有的客户端发送接收到信息,实现群聊 for
( int
i = 0 ;
i < socketList.size(); i++) { writer
= new
PrintWriter(socketList.get(i) .getOutputStream()); writer.println(count
+ "
say: "
+ message); writer.flush(); } } } catch
(IOException e) { e.printStackTrace(); } } } public
class
ChatServer { /** *
Description * *
@param args */ public
void
startWork() throws
IOException { ServerSocket
serverSocket = new
ServerSocket( 2345 ); List<Socket>
socketList = new
ArrayList<Socket>(); Socket
socket = null ; int
count = 0 ; while
( true )
{ socket
= serverSocket.accept(); count++; System.out.println(count
+ "
clinet connected to the server!" ); //
将每一个连接到该服务器的客户端,加到List中 socketList.add(socket); //
每一个连接到服务器的客户端,服务器开启一个新的线程来处理 new
Chat(count, socket, socketList).start(); } } /** *
Description * *
@param args *
@throws IOException */ public
static
void
main(String[] args) throws
IOException { ChatServer
chatServer = new
ChatServer(); chatServer.startWork(); } } |
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
|
import
java.io.BufferedReader; import
java.io.IOException; import
java.io.InputStreamReader; import
java.io.PrintWriter; import
java.net.Socket; import
java.net.UnknownHostException; /** *
接受服务器信息 */ class
ReadMes extends
Thread { private
Socket socket; public
ReadMes(Socket socket) { this .socket
= socket; } public
void
run() { BufferedReader
reader = null ; try
{ reader
= new
BufferedReader( new
InputStreamReader( socket.getInputStream())); String
message = null ; while
( true )
{ message
= reader.readLine(); //
当读服务器信息线程接收到bye,该线程退出 if
(message.equals( "bye" ))
{ break ; } System.out.println(message); } } catch
(IOException e) { e.printStackTrace(); } finally
{ try
{ if
(reader != null )
{ reader.close(); } } catch
(IOException e) { e.printStackTrace(); } } } } /** * *
发送信息 */ class
SendMes extends
Thread { private
Socket socket; public
SendMes(Socket socket) { this .socket
= socket; } public
void
run() { BufferedReader
input = null ; PrintWriter
writer = null ; try
{ input
= new
BufferedReader( new
InputStreamReader(System.in)); writer
= new
PrintWriter(socket.getOutputStream()); String
message = null ; while
( true )
{ message
= input.readLine(); //
当输入bye客户端退出 if
(message.equals( "bye" ))
{ break ; } //
向服务器端发送信息 writer.println(message); writer.flush(); } } catch
(IOException e) { e.printStackTrace(); } finally
{ if
(input != null )
{ try
{ input.close(); } catch
(IOException e) { e.printStackTrace(); } } if
(writer != null )
{ writer.close(); } } } } public
class
ChatClient { private
String ipAdress; public
ChatClient(String ipAString) { this .ipAdress
= ipAdress; } public
void
startWork() throws
UnknownHostException, IOException { Socket
socket = new
Socket(ipAdress, 2345 ); new
ReadMes(socket).start(); new
SendMes(socket).start(); } /** *
Description * *
@param args *
@throws UnknownHostException *
@throws IOException */ public
static
void
main(String[] args) throws
UnknownHostException, IOException
{ ChatClient
chatClient = new
ChatClient(args[ 0 ]); chatClient.startWork(); } } |
原文:http://blog.csdn.net/zhangping871/article/details/44892359