问题:java编写的socket服务端,C编写的客户端,通信的时候,C接收不到响应报文。

原因:服务端使用了readLine(),一直在等待客户端报文的结束符,造成阻塞。

处理办法:用缓存替换readLine(),具体代码如下:

public void run() {

PrintWriter pw = null;

OutputStreamWriter os = null;

InputStream is = null;

try {

System.out.println(“socket请求处理中”);

String line;

StringBuilder sb = new StringBuilder();

is = socket.getInputStream();

//**********开始读取报文

byte[] buffer = new byte[8];  

int len = -1;

len = is.read(buffer);

String SocketLength = new String(buffer, 0, len, SdUtils.MSG_CHARACTOR); //获取报文长度描述  

sb.append(SocketLength);

for(int i=0; i<SocketLength.length();i++){

if(SocketLength.charAt(i) == ‘0’){

continue;

}

SocketLength = SocketLength.substring(i);  //获取报文长度

}



buffer = new byte[Integer.valueOf(SocketLength)];  

len = is.read(buffer);

String SocketContent = new String(buffer, 0, len, SdUtils.MSG_CHARACTOR);// 获取报文头报文体报文尾

sb.append(SocketContent);

line = sb.toString();

//******************读取结束

lg.info(“Thread-” + this.getId() + “读取到客户端信息:” + line);

socket.shutdownInput();// 关闭输入流



resultStr = resp.getSrcMsg();// 返回字符串报文





// TODO 4、获取输出流,响应客户端的请求

os = new OutputStreamWriter(socket.getOutputStream(),

SdUtils.MSG_CHARACTOR);

pw = new PrintWriter(os);



pw.write(resultStr);

pw.flush();

lg.info(“Thread-” + this.getId() + “返回客户端报文:” + resultStr);

socket.shutdownOutput();

} catch (Exception e) {

e.printStackTrace();

lg.error(e.getMessage());

} finally {

try {

pw.close();

os.close();

is.close();

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

小结,使用readLine()一定要注意:

1.读入的数据要注意有/r或/n或/r/n
2.没有数据时会阻塞,在数据流异常或断开时才会返回null
3.使用socket之类的数据流时,要避免使用readLine(),以免为了等待一个换行/回车符而一直阻塞


版权声明:本文为ttheng88原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ttheng88/article/details/78085083