【Java TCP/IP Soket】— TCP通信中由read返回值造成的的死锁问题

来源:互联网 发布:数据库工作前景怎么样 编辑:程序博客网 时间:2024/04/20 04:15

[+]

http://blog.csdn.net/ns_code/article/details/14642873

  书上示例

    在第一章《基本套接字》中,作者给出了一个TCP Socket通信的例子——反馈服务器,即服务器端直接把从客户端接收到的数据原原本本地反馈回去。

     书上客户端代码如下:

[java] view plaincopy
  1. import java.net.Socket;  
  2. import java.net.SocketException;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6.   
  7. public class TCPEchoClient {  
  8.   
  9.     public static void main(String[] args) throws IOException {  
  10.   
  11.         if ((args.length < 2) || (args.length > 3))  // Test for correct # of args  
  12.             throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]");  
  13.   
  14.         String server = args[0];       // Server name or IP address  
  15.         // Convert argument String to bytes using the default character encoding  
  16.         byte[] data = args[1].getBytes();  
  17.   
  18.         int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;  
  19.   
  20.         // Create socket that is connected to server on specified port  
  21.         Socket socket = new Socket(server, servPort);  
  22.         System.out.println("Connected to server...sending echo string");  
  23.   
  24.         InputStream in = socket.getInputStream();  
  25.         OutputStream out = socket.getOutputStream();  
  26.   
  27.         out.write(data);  // Send the encoded string to the server  
  28.   
  29.         // Receive the same string back from the server  
  30.         int totalBytesRcvd = 0;  // Total bytes received so far  
  31.         int bytesRcvd;           // Bytes received in last read  
  32.         while (totalBytesRcvd < data.length) {  
  33.             if ((bytesRcvd = in.read(data, totalBytesRcvd,data.length - totalBytesRcvd)) == -1)  
  34.                 throw new SocketException("Connection closed prematurely");  
  35.             totalBytesRcvd += bytesRcvd;  
  36.         }  // data array is full  
  37.   
  38.         System.out.println("Received: " + new String(data));  
  39.   
  40.         socket.close();  // Close the socket and its streams  
  41.     }  
  42. }  

     书上的服务器端代码如下:

[java] view plaincopy
  1. import java.net.*;  // for Socket, ServerSocket, and InetAddress  
  2. import java.io.*;   // for IOException and Input/OutputStream  
  3.   
  4. public class TCPEchoServer {  
  5.   
  6.     private static final int BUFSIZE = 32;   // Size of receive buffer  
  7.   
  8.     public static void main(String[] args) throws IOException {  
  9.   
  10.     if (args.length != 1)  // Test for correct # of args  
  11.         throw new IllegalArgumentException("Parameter(s): <Port>");  
  12.   
  13.     int servPort = Integer.parseInt(args[0]);  
  14.   
  15.     // Create a server socket to accept client connection requests  
  16.     ServerSocket servSock = new ServerSocket(servPort);  
  17.   
  18.     int recvMsgSize;   // Size of received message  
  19.     byte[] receiveBuf = new byte[BUFSIZE];  // Receive buffer  
  20.   
  21.     while (true) { // Run forever, accepting and servicing connections  
  22.         Socket clntSock = servSock.accept();     // Get client connection  
  23.   
  24.         SocketAddress clientAddress = clntSock.getRemoteSocketAddress();  
  25.         System.out.println("Handling client at " + clientAddress);  
  26.         
  27.         InputStream in = clntSock.getInputStream();  
  28.         OutputStream out = clntSock.getOutputStream();  
  29.   
  30.   
  31.         // Receive until client closes connection, indicated by -1 return  
  32.         while ((recvMsgSize = in.read(receiveBuf)) != -1) {  
  33.             out.write(receiveBuf, 0, recvMsgSize);  
  34.         }  
  35.   
  36.         clntSock.close();  // Close the socket.  We are done with this client!  
  37.     }  
  38.     /* NOT REACHED */  
  39.   }  
  40. }  

       示例程序当然运行无误,运行结果如下:

 


   问题的引出

     首先明确几点:

     1、客户端与服务器端在接收和发送数据时,read()和write()方法不一定要对应,比如,其中一方可以一次发送多个字节的数据,而另一方可以一个字节一个字节地接收,也可以一个字节一个字节地方送,而多个字节多个字节地接收。因为TCP协议会将数据分成多个块进行发送,而后在另一端会从多个块进行接收,再组合在一起,它并不仅能确定read()和write()方法中所发送信息的界限。

     2、read()方法会在没有数据可读时发生阻塞,直到有新的数据可读。

     注意客户端中下面部分代码

[java] view plaincopy
  1. while (totalBytesRcvd < data.length) {  
  2.     if ((bytesRcvd = in.read(data, totalBytesRcvd,data.length - totalBytesRcvd)) == -1)  
  3.         throw new SocketException("Connection closed prematurely");  
  4.     totalBytesRcvd += bytesRcvd;  
  5. }  // data array is full  
     客户端从Socket套接字中读取数据,直到收到的数据的字节长度和原来发送的数据的字节长度相同为止,这里的前提是已经知道了要从服务器端接收的数据的大小,如果现在我们不知道要反馈回来的数据的大小,那么我们只能用read方法不断读取,直到read()返回-1,说明接收到了所有的数据。我这里采用一个字节一个字节读取的方式,代码改为如下:

[java] view plaincopy
  1. while((bytesRcvd = in.read())!= -1){  
  2.     data[totalBytesRcvd] = (byte)bytesRcvd;  
  3.     totalBytesRcvd++;  
  4. }   

    这时问题就来了,输出结果如下:


     

    问题的分析

      客户端没有数据打印出来,初步推断应该是read()方法始终没有返回-1,导致程序一直无法往下运行,我在客客户端执行窗口中按下CTRL+C,强制结束运行,在服务器端抛出如下异常:

Exception in thread "main" java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.net.SocketInputStream.read(Unknown Source)
        at TCPEchoServer.main(TCPEchoServer.java:32)
     异常显示,问题出现在服务端的32行,没有资源可读,现在很有可能便是由于read()方法始终没有返回-1所致,为了验证,我在客户端读取字节的代码中加入了一行打印读取的单个 字符的代码,如下:
[java] view plaincopy
  1. while((bytesRcvd = in.read())!= -1){  
  2.     data[totalBytesRcvd] = (byte)bytesRcvd;  
  3.     System.out.println((char)data[totalBytesRcvd]);  
  4.     totalBytesRcvd++;  
  5. }  
     此时运行结果如下:
     很明显,客户端程序在打印出最有一个字节后不再往下执行,没有执行其后面的System.out.println("Received: " + new String(data));这行代码,这是因为read()方法已经将数据读完,没有数据可读,但又没有返回-1,因此在此处产生了阻塞。这便造成了TCP Socket 通信的死锁问题。

    问题的解决

     查阅相关资料,仔细阅读了书上的每个细节,在通过对比书上的代码和自己的代码,发现了问题所在。问题就出现在read()方法上,这里的重点是read()方法何时返回-1,在一般的文件读取中,这代表流的结束,亦即读取到了文件的末尾,但是在Socket套接字中,这样的概念很模糊,因为套接字中数据的末尾并没有所谓的结束标记,无法通过其自身表示传输的数据已经结束,那么究竟什么时候read()会返回-1呢?答案是:当TCP通信连接的一方关闭了套接字时。
     再次分析改过后的代码,客户端用到了read()返回-1这个条件,而服务端也用到了,只有二者有一方关闭了Socket,另一方的read()方法才会返回-1,而在客户端打印输出前,二者都没有关闭Socket,因此,二者的read()方法都不会返回-1,程序便阻塞在此处,都不往下执行,这便造成了死锁。
     反过来,再看书上的给出的代码,在客户端代码的while循环中,我们的条件是totalBytesRcvd < data.length,而不是(bytesRcvd = in.read())!= -1,这样,客户端在收到与其发送相同的字节数之后便会退出while循环,再往下执行,便是关闭套接字,此时服务端的read()方法检测到客户端的关闭,便会返回-1,从而继续往下执行,也将套接字关闭。因此,不会产生死锁。
     那么,如果在客户端不知道反馈回来的数据的情况下,该如何避免死锁呢?Java的Socket类提供了shutdownOutput()和shutdownInput()另个方法,用来分别只关闭Socket的输出流和输入流,而不影响其对应的输入流和输出流,那么我们便可以在客户端发送完数据后,调用shutdownOutput()方法将套接字的输出流关闭,这样,服务端的read()方法便会返回-1,继续往下执行,最后关闭服务端的套接字,而后客户端的read()方法也会返回-1,继续往下执行,直到关闭套接字。
    客户端改变后的代码部分如下:
[java] view plaincopy
  1. out.write(data);  // Send the encoded string to the server  
  2. socket.shutdownOutput();  
    这样,便得到了预期的运行结果,如下:


    总结

      由于read()方法只有在另一端关闭套接字的输出流时,才会返回-1,而有时候由于我们不知道所要接收数据的大小,因此不得不用read()方法返回-1这一判断条件,那么此时,合理的程序设计应该是先关闭网络输出流(亦即套接字的输出流),再关闭套接字。