用Socket实现了TCP的聊天室的简单模型,
服务器端有两个类:TCPListener是用来监听客户端的连接请求的,并且新起一个线程用来不断的读取socket中的数据并且发送给所有已经建立连接的客户;TCPConnection就是这个读写的线程
客户端有两个类:TCPClient是用来发送连接请求的,并且监听键盘输入的聊天内容并写进socket,每次连接新起一个线程用来不断读取socket里返回的其他用户的聊天内容;TCPClientThread就是这个线程
先运行TCPListener后,再运行TCPClient,问题就是:一个客户端输入内容后,服务器端用来不断读取数据的线程并没有反映,也就是黑体的代码没有执行,其他客户端也没有收到服务器端返回的这个客户端的聊天信息。
TCPListener
public class TCPListener {
public static List<Socket> sockets = Collections.synchronizedList(new ArrayList<Socket>());
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(3003);
while(true){
Socket socket = server.accept();
sockets.add(socket);
new Thread(new TCPConnection(socket)).start();
}
}
}
TCPConnection
public class TCPConnection implements Runnable{
private Socket socket = null;
private BufferedReader input = null;
public TCPConnection(Socket socket){
this.socket = socket;
try {
input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
String content = null;
try {
while((content = (input.readLine())) != null){
System.out.println(“不断读取socket中的数据的这段代码没有被执行”);
for(Socket target : TCPListener.sockets){
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(target.getOutputStream()));
output.write((content + " from server"));
System.out.println(content + " from server");
// output.flush();
}
}
} catch (IOException e) {
TCPListener.sockets.remove(socket);
System.out.println("A client has exited because of unexpected errors");
e.printStackTrace();
}
}
}
TCPClient
public class TCPClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 3003);
new Thread(new TCPClientThread(socket)).start();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
String content = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while((content = reader.readLine()) != null){
writer.println(content);
// writer.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TCPClientThread
public class TCPClientThread implements Runnable{
public Socket socket = null;
public BufferedReader input = null;
public TCPClientThread(Socket socket){
this.socket = socket;
try {
input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
String line = null;
while((line = input.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
------解决思路----------------------
根据楼主代码稍加修改,详情见注释
//TCPListener.java文件
import java.io.*;
import java.net.*;
import java.util.*;
public class TCPListener {
public static List<Socket> sockets = Collections.synchronizedList(new ArrayList<Socket>());
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(3003);
while(true){
Socket socket = server.accept();
sockets.add(socket);
new Thread(new TCPConnection(socket)).start();
}
}
}
class TCPConnection implements Runnable{
private Socket socket = null;
private BufferedReader input = null;
public TCPConnection(Socket socket){
this.socket = socket;
try {
input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
String content = null;
try {
//楼主之前没有响应,是因为客户端那边没有刷新缓冲区,这里就接收不到数据,程序会阻塞在这个地方,下面的代码也就无法执行
while((content = (input.readLine())) != null){
//System.out.println("不断读取socket中的数据的这段代码没有被执行");
for(Socket target : TCPListener.sockets){
PrintWriter writer = new PrintWriter(new OutputStreamWriter(target.getOutputStream()));//仿照客户端,改为PrintWriter输出
writer.println((content + " from server"));
//System.out.println(content + " from server");
writer.flush();//这里也要刷新,不然客户端程序也读不到数据,客户端程序也同样会阻塞
}
}
} catch (IOException e) {
TCPListener.sockets.remove(socket);
System.out.println("A client has exited because of unexpected errors");
e.printStackTrace();
}
}
}
//TCPClient.java文件
import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 3003);
new Thread(new TCPClientThread(socket)).start();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(
socket.getOutputStream()));
String content = null;
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
while ((content = reader.readLine()) != null) {
writer.println(content);
writer.flush();// 刷新缓冲区,不然服务器端接收不到数据,会导致其程序阻塞
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class TCPClientThread implements Runnable {
public Socket socket = null;
public BufferedReader input = null;
public TCPClientThread(Socket socket) {
this.socket = socket;
try {
input = new BufferedReader(new InputStreamReader(
this.socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
------解决思路----------------------
我觉得最好的办法就是使用非阻塞模式了。
------解决思路----------------------
你客户端和服务器之间的交互规则是一个业务逻辑问题。
而阻塞,线程同步之类的,都是纯技术问题。
不要把这两者搞在一起。
------解决思路----------------------
楼主你首先得清楚你这个示例程序应该具有啥样的业务逻辑!
若你的想法就是服务器程序等待某个客户端程序发送数据之后,再将这个数据转发到其它客户端,那么代码按你原来那么写就行了,细节稍微修改就行了