应用多线程来实现服务器与多客户端之间的通信
工具类
public class ThreadUtils extends Thread{
private Socket socket = null;
public ThreadUtils (Socket socket){//创建构造方法
this.socket = socket;
}
public void run() {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader bf = null;
try {
is = socket.getInputStream(); //获得字节输入流
isr = new InputStreamReader(is);//将字节输入流转为 字符输入流
bf = new BufferedReader(isr); //将字符输入流加入缓冲区
String data = null;//返回的数据
while (bf.readLine()!=null){
data = bf.readLine(); //读取数据
}
socket.shutdownInput();//关闭输入流
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(bf!=null)
bf.close();
if(isr!=null)
isr.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Action类