socket输入流麻烦。显示为null

时间:2013-11-24 22:47:49

标签: java sockets client-server inputstream soa

我似乎无法弄清楚这段代码发生了什么(我没写过)。看起来像 str3 = localBufferedReader1.readLine()为null,因此抛出异常。运行应用程序后看到的输出如下:

  >> Transmitting File : C:/some_folder/another_folder/folder/bin/msg/somefile.soa (32 bytes)
     >> Segment read from file (segLength=15 bytes, 16 bytes remaining to send)
     >> Segment read from file (segLength=13 bytes, 2 bytes remaining to send)
  >> Message sent ...
Exception in Transmitting Data : null

我似乎无法理解为什么当连接没有失败并且找到文件时没有什么可读的?还有什么其他原因导致失败?我已经确保服务器真正收听我输入的端口号。我也在localhost上运行 - 所以这应该工作:我相信我应该从套接字收到一条消息(查看代码)让我知道如果一切正常或不行......任何想法?任何帮助将不胜感激!

public class Client
{

  public static void main(String[] paramArrayOfString)
    throws IOException
  {
    Socket localSocket = null;
    PrintWriter localPrintWriter = null;
    BufferedReader localBufferedReader1 = null;




    String str1 = JOptionPane.showInputDialog(null, "Host to Talk to ?", "Specify the HOST", 1);



    String str2 = JOptionPane.showInputDialog(null, "What Port is it Listening on ?", "Specify the PORT", 1);
    int i;
    try
    {
      i = Integer.parseInt(str2);
    }
    catch (Exception localException1)
    {
      i = 3128;
    }
    char[] arrayOfChar1 = { '\013' };
    char[] arrayOfChar2 = { '\034' };
    char[] arrayOfChar3 = { '\r' };

    int k = 1;
    int m = 0;
    int n = 0;


    String str3 = "";
    String str5 = JOptionPane.showInputDialog(null, "Filename Containing HL7 Message", "Test Filename", 1);
    while (str5.length() > 0)
    {
      try
      {
        localSocket = new Socket(str1, i);
        localPrintWriter = new PrintWriter(localSocket.getOutputStream(), true);
        System.out.println(localSocket.getInputStream().available()); 

        localBufferedReader1 = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
      }
      catch (UnknownHostException localUnknownHostException)
      {
        System.out.println("Don't know about host: " + str1);
        System.exit(1);
      }
      catch (IOException localIOException)
      {
        System.out.println("Couldn't get I/O for the Connection to " + str1 + " on Port (" + str2 + ")");
        System.exit(1);
      }
      try
      {
        File localFile = new File(str5);
        BufferedReader localBufferedReader2 = new BufferedReader(new FileReader(localFile));

        long l = localFile.length();
        System.out.println("  >> Transmitting File : " + str5 + " (" + l + " bytes)");

        String str7 = new String(arrayOfChar1);
        String str6;
        while ((str6 = localBufferedReader2.readLine()) != null)
        {
          l = l - str6.length() - 1L;
          System.out.println("     >> Segment read from file (segLength=" + str6.length() + " bytes, " + l + " bytes remaining to send)");
          if (k != 0) {
            k = 0;
          }
          str7 = str7 + str6 + new String(arrayOfChar3);
          if (l < 6L)
          {
            if ((str7.indexOf("READY") >= 0) || (str7.indexOf("BYE") >= 0)) {
              str7 = str7.substring(0, str7.length() - 1);
            } else {
              str7 = str7 + new String(arrayOfChar2);
            }
            m = 1;
          }
          if (m != 0) {
            break;
          }
        }
        if (m == 0)
        {
          str7 = str7 + new String(arrayOfChar2);
          m = 1;
        }
        localPrintWriter.println(str7);
        localPrintWriter.flush();
        localBufferedReader2.close();
        System.out.println("  >> Message sent ...");


        str3 = localBufferedReader1.readLine();
        if (arrayOfChar3[0] == '\r')
        {
          n = 1;
          if (str3.indexOf("SOA|OK|") >= 0) {
            if (str3.indexOf("SOA|OK|||") >= 0)
            {
              if (str3.indexOf("SOA|OK||||") >= 0) {
                n = 0;
              }
            }
            else {
              n = 0;
            }
          }
          if (str3.indexOf("SOA|NOT-OK|") >= 0) {
            n = 0;
          }
          str3 = str3.substring(1) + "\n";
          while (n != 0)
          {
            String str4 = localBufferedReader1.readLine();
            int j = str4.length() - 1;
            if (str4.charAt(j) == arrayOfChar2[0]) {
              n = 0;
            } else {
              str3 = str3 + str4 + "\n";
            }
          }
        }
        System.out.println("  >> Server Responds : " + str3);
      }
      catch (Exception localException2)
      {
        System.out.println("Exception in Transmitting Data : " + localException2.getMessage());
        localPrintWriter.close();
        localBufferedReader1.close();
        localSocket.close();
        System.exit(1);
      }
      localPrintWriter.close();
      localBufferedReader1.close();
      localSocket.close();
      if (str3.indexOf("AE") >= 0) {
        break;
      }
      str5 = JOptionPane.showInputDialog(null, "Filename Containing HL7 Message", "Test Filename", 1);
      if (str5 == null) {
        System.exit(0);
      }
      k = 1;
      m = 0;
    }
    System.exit(0);
  }
}

1 个答案:

答案 0 :(得分:2)

来自javadoc of BufferedReader.readLine

<强>返回:     包含行内容的String,不包括任何行终止字符;如果已到达流的末尾,则为null

使用str3的代码:

str3 = localBufferedReader1.readLine();
if (arrayOfChar3[0] == '\r')
{
  n = 1;
  if (str3.indexOf("SOA|OK|") >= 0) {

因此,您的代码到达流的末尾,null被分配给str3。但是,代码似乎没有检查它并尝试在其上调用indexOf