簡體   English   中英

socket輸入流麻煩。 顯示為null

[英]socket inputstream trouble. appears as null

我似乎無法弄清楚這個代碼發生了什么(我沒有寫它)。 它看起來像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);
  }
}

BufferedReader.readLinejavadoc

返回:包含行內容的String,不包括任何行終止字符;如果已到達流的末尾,則返回null

使用str3的代碼:

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

因此,您的代碼到達流的末尾,並將null分配給str3。 但是,代碼似乎沒有檢查它並嘗試在其上調用indexOf

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM