gpt4 book ai didi

java - 套接字输入流问题。显示为空

转载 作者:行者123 更新时间:2023-12-02 06:29:47 24 4
gpt4 key购买 nike

我似乎无法弄清楚这段代码发生了什么(我没有写它)。看起来像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

我似乎无法理解为什么当连接没有失败并且找到文件时没有任何内容可读取..?还有哪些其他原因会导致此失败?我已确保服务器确实正在监听我输入的端口号。我也在本地主机上运行 - 所以这应该可以工作:我相信我应该从套接字收到一条返回消息(查看代码),让我知道一切是否正常...有什么想法吗?任何帮助将不胜感激!

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

最佳答案

来自javadoc of BufferedReader.readLine :

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

使用str3的代码:

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

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

关于java - 套接字输入流问题。显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com