服务端3.1
package talkingthread;
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;
public class ChatServer3 {
static List<ChatThread> listClient = new ArrayList<>();
public static class ChatThread implements Runnable{
Socket socket;
BufferedReader reader;
PrintWriter writer;
public ChatThread(Socket nsocket) throws IOException{
socket = nsocket;
writer = new PrintWriter(socket.getOutputStream());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void sendMessage(String msg){
writer.println(msg);
writer.flush();
}
public void sendAll(String msg){
for(ChatThread client : listClient){
client.sendMessage(msg);
}
}
@Override
public void run() {
String msg = "";
try {
while ((msg = reader.readLine()) != null) {
sendAll(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
ServerSocket server = new ServerSocket(10000);
while (true) {
Socket socket = server.accept();
ChatThread thread = new ChatThread(socket);
listClient.add(thread);
Thread t = new Thread(thread);
t.start();
}
}
}
服务端3
package talkingthread;
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.Iterator;
import java.util.List;
public class ChatServer3 {
static List<String> message = new ArrayList<String>();
public static class ChatThread implements Runnable{
Socket socket;
BufferedReader reader;
PrintWriter writer;
public ChatThread(Socket nsocket){
socket = nsocket;
}
public void sendMessage() throws IOException{
// for (String client : message){
// writer = new PrintWriter(socket.getOutputStream());
// writer.println(client);
// writer.flush();
// }
Iterator<String> iterator = message.iterator();
while (iterator.hasNext()) {
writer = new PrintWriter(socket.getOutputStream());
writer.println(iterator.next());
writer.flush();
}
}
@Override
public void run() {
String msg = "";
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((msg = reader.readLine()) != null) {
message.add(msg);
sendMessage();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
ServerSocket server = new ServerSocket(10000);
while (true) {
Socket socket = server.accept();
ChatThread thread = new ChatThread(socket);
Thread t = new Thread(thread);
t.start();
}
}
}
客户端
package talkingthread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ChatClient2 {
private Thread readThread;
private Thread writeThread;
public ChatClient2() throws Exception, IOException{
@SuppressWarnings("resource")
Socket socket = new Socket("127.0.0.1", 10000);
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final PrintWriter out = new PrintWriter(socket.getOutputStream());
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
readThread = new Thread(new Runnable(){
@Override
public void run() {
String message = "";
while(true){
try {
message = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(message);
}
}
});
readThread.start();
writeThread = new Thread(new Runnable(){
public void run() {
String msg = "";
while(true){
try {
msg = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
out.println(msg);
out.flush();
}
}
});
writeThread.start();
}
public static void main(String[] args) throws Exception, IOException {
new ChatClient2();
}
}
问题1,客户端必须发出消息,服务端才会返回信息
问题2,服务端会返还之前所有的信息
将客户端socket记录下来,往每一个socket分别写信息。