gpt4 book ai didi

java - 为什么我的客户端不再接受输入

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

为什么我的客户端在输入一条 FIX 消息后不再接受输入。客户端会向服务器端发送一条FIX消息,服务器端会检查是否有错误,如果FIX消息上有错误或者没有错误,则将消息发回给客户端。

当我尝试从客户端发送另一条 FIX 消息时,问题就出现了,在发送消息之前,它不允许我发送任何内容。

客户端程序

public class TcpClient {

public static void main(String[] args) throws IOException {
String serverHostname = new String ("WA1235"); //127.0.0.1

if (args.length > 0)
serverHostname = args[0];
System.out.println ("Attemping to connect to host " +
serverHostname + " on port 57634.");

Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
// echoSocket = new Socket("taranis", 7);
echoSocket = new Socket(serverHostname, 57634);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: " + serverHostname);
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;

System.out.print ("input: ");

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);

System.out.println(in.readLine());
System.out.println(in.readLine());

if (userInput.equals("Bye.")){
System.out.println("Exit program");
break;
}
getValueLog(parseFixMsg(userInput,userInput));
System.out.print ("input: ");

}

out.close();
in.close();
stdIn.close();
echoSocket.close();
}

服务器程序

public static void main(String[] args) throws IOException{
Scanner console = new Scanner(System.in);
System.out.println("Type in CSV file location: ");
//String csvName = console.nextLine();
String csvName = "C:\\Users\\Downloads\\orders.csv";


ServerSocket serverSocket = null;

try {
serverSocket = new ServerSocket(57634);
}
catch (IOException e)
{
System.err.println("Could not listen on port: 57635.");
System.exit(1);
}

Socket clientSocket = null;
System.out.println ("Waiting for connection.....");

try {
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}

System.out.println ("Connection successful");
System.out.println ("Waiting for input.....");

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream()));

String inputLine, outputLine;

while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);



if (inputLine.trim().equals("Bye.")) {
System.out.println("Exit program");
break;
}

Scanner input1 = new Scanner(new File(csvName));
Scanner input2 = new Scanner(new File(csvName));
Scanner input3 = new Scanner(new File(csvName));
Scanner input4 = new Scanner(new File(csvName));


String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1, input2)), getCSVLine( input3, input4) );
outputLine = compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

out.println(outputLine);

input1.close();
input2.close();
input3.close();
input4.close();



}


out.close();
in.close();
clientSocket.close();
serverSocket.close();

}

方法

public static String compareClientFixCSV(String[] cTag, String[] cValue, String[] csvTag, String[] csvValue){
int size = csvTag.length;
int size2 = csvTag.length;
int size3 = cValue.length;
int size4 = csvValue.length;
System.out.println("cTag size : " + size + ", csvTag: " + size2);
System.out.println("csvTag value : " + size3 + ", csvValue: " + size4);
String output = null;
for(int i = 0; i<= size-1; i++){
if(cTag[i].equals(csvTag[i]) == false ){
output = ("Error in tag " + cTag[i]);


}
else if(cValue[i].equals(csvValue[i]) == false){
output = ("Error in value " + cValue[i]);


}
else{
output = ("No errors");
}
}


return output;
}

最佳答案

快速浏览一下代码,我发现客户端等待了 2 行:

System.out.println(in.readLine());
System.out.println(in.readLine());

服务器只发送 1:

out.println(outputLine);

可能这就是问题所在。

我还将把客户端的读取部分包含在 try ... catch ... finally block 中,如下所示:

try
{
while (true)
{
System.out.print("input: ");
userInput = stdIn.readLine();
if (userInput == null) break;
out.println(userInput);

System.out.println(in.readLine());
System.out.println(in.readLine());

if (userInput.equals("Bye."))
{
System.out.println("Exit program");
break;
}
getValueLog(parseFixMsg(userInput,userInput));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
// any of these lines could raise an exception as well.
out.close();
in.close();
stdIn.close();
echoSocket.close();
}

关于java - 为什么我的客户端不再接受输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17860975/

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