toString() unavailable - no suspended threads
2016-05-22 10:37
495 查看
最近在练习java的socket
自己写了个例子代码如下:
server端
public class WeatherServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket serverSocket=new ServerSocket(12345);
System.out.println("启动天气查询...");
Socket socket=serverSocket.accept();
System.out.println("开始接受数据");
//接收客户端的消息
InputStream in=socket.getInputStream();
/*System.setIn(in);
Scanner scanner=new Scanner(System.in);
while(scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}*/
byte[] b=new byte[6];
int len=0;
String acceptMessage="";
while((len=in.read(b))!=-1){
String s=new String(b,0,len,"UTF-8");
acceptMessage+=s;
}
System.out.println("from the client..."+acceptMessage);
//响应给客户端的消息
String responseMessage="晴";
OutputStream out=socket.getOutputStream();
byte[] b1=responseMessage.getBytes();
out.write(responseMessage.getBytes());
System.out.println("to the client..."+responseMessage);
in.close();
out.close();
}
}
client端:
public class WeatherClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket=new Socket("127.0.0.1",12345);
//向服务器端发送消息
OutputStream out=socket.getOutputStream();
String sendMessage="tttt\n";
byte[] bb=sendMessage.getBytes();
out.write(sendMessage.getBytes());
out.flush();
// out.close();
// socket.shutdownOutput();
System.out.println("to the server..."+sendMessage);
//接收服务器端返回的消息
InputStream in =socket.getInputStream();
byte[] b=new byte[5];
int len=in.read(b);
String responseMessage="";
while((len=in.read(b))!=-1){
String s=new String(b,0,len,"UTF-8");
responseMessage+=s;
}
System.out.println("from the server..."+responseMessage);
in.close();
socket.close();
}
}
什么情况会出现这个问题?
代码一运行没有报错,输入流和输出流之间形成了死锁。由于client端的输出流没有告诉server端流已经结束,所以server端在等待client端的输出流,而client在等待server端的响应输出流,server端没有结束接收client端的输出,server也不会响应消息给client端。所以造成了死锁的现象。
用debugger模式可以看到server端的输入流出现这个提示:toString() unavailable - no suspended threads
我们在client端输出结束后加上这么一句:socket.shutdownOutput();就可以告诉server端,流已经结束
自己写了个例子代码如下:
server端
public class WeatherServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket serverSocket=new ServerSocket(12345);
System.out.println("启动天气查询...");
Socket socket=serverSocket.accept();
System.out.println("开始接受数据");
//接收客户端的消息
InputStream in=socket.getInputStream();
/*System.setIn(in);
Scanner scanner=new Scanner(System.in);
while(scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}*/
byte[] b=new byte[6];
int len=0;
String acceptMessage="";
while((len=in.read(b))!=-1){
String s=new String(b,0,len,"UTF-8");
acceptMessage+=s;
}
System.out.println("from the client..."+acceptMessage);
//响应给客户端的消息
String responseMessage="晴";
OutputStream out=socket.getOutputStream();
byte[] b1=responseMessage.getBytes();
out.write(responseMessage.getBytes());
System.out.println("to the client..."+responseMessage);
in.close();
out.close();
}
}
client端:
public class WeatherClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket=new Socket("127.0.0.1",12345);
//向服务器端发送消息
OutputStream out=socket.getOutputStream();
String sendMessage="tttt\n";
byte[] bb=sendMessage.getBytes();
out.write(sendMessage.getBytes());
out.flush();
// out.close();
// socket.shutdownOutput();
System.out.println("to the server..."+sendMessage);
//接收服务器端返回的消息
InputStream in =socket.getInputStream();
byte[] b=new byte[5];
int len=in.read(b);
String responseMessage="";
while((len=in.read(b))!=-1){
String s=new String(b,0,len,"UTF-8");
responseMessage+=s;
}
System.out.println("from the server..."+responseMessage);
in.close();
socket.close();
}
}
什么情况会出现这个问题?
代码一运行没有报错,输入流和输出流之间形成了死锁。由于client端的输出流没有告诉server端流已经结束,所以server端在等待client端的输出流,而client在等待server端的响应输出流,server端没有结束接收client端的输出,server也不会响应消息给client端。所以造成了死锁的现象。
用debugger模式可以看到server端的输入流出现这个提示:toString() unavailable - no suspended threads
我们在client端输出结束后加上这么一句:socket.shutdownOutput();就可以告诉server端,流已经结束
相关文章推荐
- Airbnb 分享经济步步紧逼:传统酒店老板要和出租车司机同病相怜?
- 【NOIP2013模拟】Rainbow的信号
- Block如何避免self retain
- LeetCode 70. Climbing Stairs(爬楼梯)
- 3881: [Coci2015]Divljak fail树+树链的并
- 人工智能:美女机器人能和男人产生感情吗?
- ArrayList<E>.<init> line: not available
- 【杭电oj】2057 - A + B Again(16进制输入输出)
- POJ 3421 X-factor Chains(数论)(筛法)()
- 2016/5/21 1002. format the book list again~
- 2016/5/21 1002. format the book list again~
- mybaits中文官方文档地址
- Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt' after
- 70. Climbing Stairs
- 安卓约束控件(ConstraintLayout)扁平化布局入门
- leetcode 11:Container With Most Water
- NSSearchPathForDirectoriesInDomains用法
- socketpair实现进程通信
- 10976 - Fractions Again?!
- http://blog.csdn.net/bendanban/article/details/44159903