简体   繁体   English

socket输入流麻烦。 显示为null

[英]socket inputstream trouble. appears as null

i can't seem to figure out what is going on with this code (i didn't write it). 我似乎无法弄清楚这个代码发生了什么(我没有写它)。 it looks like str3 = localBufferedReader1.readLine() is null and hence an exception is thrown. 它看起来像str3 = localBufferedReader1.readLine()为null,因此抛出异常。 The output i see after running the application is as follows: 运行应用程序后看到的输出如下:

  >> 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

I can't seem to understand why there is nothing to read when the connection did not fail and the file was found..? 我似乎无法理解为什么当连接没有失败并找到文件时没有什么可读的? what other reasons could this fail for? 还有什么其他原因导致失败? i've ensured that the server is really listening on the port number i enter. 我已经确保服务器真正收听我输入的端口号。 i'm also running on localhost - so this should work :S i believe that i should be getting a message back (looking at the code) from the socket letting me know if everything is OK or NOT-OK ... any ideas? 我也在localhost上运行 - 所以这应该工作:我相信我应该从套接字收到一条消息(查看代码)让我知道如果一切正常或不行......任何想法? any help would be appreciated! 任何帮助,将不胜感激!

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);
  }
}

From the javadoc of BufferedReader.readLine : BufferedReader.readLinejavadoc

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached 返回:包含行内容的String,不包括任何行终止字符;如果已到达流的末尾,则返回null

The code where str3 is used: 使用str3的代码:

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

So your code reaches end of stream, and null is assigned to str3. 因此,您的代码到达流的末尾,并将null分配给str3。 However the code doesn't seem to check it and tries to invoke indexOf on it. 但是,代码似乎没有检查它并尝试在其上调用indexOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM