先上代码:
public class ServerThread implements Runnable{
//定义当前线程所处理的Socket
Socket s=null;
BufferedReader bReader=null;
OutputStream os=null;
public ServerThread(Socket s) throws IOException{
this.s=s;
bReader=new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));
}
public void run() {
// TODO Auto-generated method stub
String contentString=null;
System.out.println("进入了");
try {
while((contentString=readFromClient())!=null){
for(Socket s:MyServer.socketList){
System.out.println(contentString);
os=s.getOutputStream();
os.write((contentString+"\n").getBytes("utf-8"));
os.flush();
}
}
os.close();
bReader.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.toString());
}
}
public String readFromClient(){
try {
return bReader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("没有接受到任何数据");
MyServer.socketList.remove(s);
}
return null;
}
}
在readLine()执行后已经接受到了客户端的信息,但是在os.write((contentString+"\n").getBytes("utf-8"));这一行直接报错了
报错信息如下:
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:124)
at ServerThread.run(ServerThread.java:29)
at java.lang.Thread.run(Thread.java:619)
什么问题导致的呢,百度了好多都没解决