简体   繁体   English

如何以正确的顺序输出自动的Client-Server Java网络代码?

[英]How do I make my automated Client-Server Java networking code output in the correct sequence?

Let me start by saying I know this is a lot of code, and so please bear with me. 首先让我说我知道这是很多代码,所以请耐心等待。 I am working on a project, where I'm porting Oracle's Knock-Knock client/server example into a US State-Capital example. 我正在一个项目中,正在将Oracle的Knock-Knock客户端/服务器示例移植到美国State-Capital示例中。 So instead of KnockKnock jokes, it runs a series of state-capital queries. 因此,它运行一系列状态资本查询,而不是KnockKnock笑话。 It runs roughly like this : 它大致运行如下:

Server: May I have permission to send you a state-capital query? 服务器:我是否可以向您发送州名查询?

Client: Ok 客户:好的

Server: Send me a state , so I can send a capital? 服务器:请向我发送州名,这样我可以向您发送资金?

Client: Alabama 客户:阿拉巴马州

Server: The capital of Alabama is Hunstville . 服务器:阿拉巴马州的首都是汉斯维尔。 Want another one (y/n) ? 需要另一个(y / n)吗?

Client: n 客户:n

The motivation for doing this project is to later demonstrate some type of fail-safe ability (after I get this to work I want to use Powershell and use that to run the client-server as a single unit). 进行此项目的动机是稍后演示某种类型的故障保护功能(在我使它起作用之后,我想使用Powershell并使用该功能以单个单元运行客户端服务器)。

Back to my code. 回到我的代码。 When I run it it's giving me the following output, which is not what I'm looking for. 当我运行它时,它会给我以下输出,这不是我想要的。 It is supposed to be synchronized so that the client and server are working on one state at a time. 它应该是同步的,以便客户端和服务器一次处于一种状态。 Instead, the client-side is jumping ahead of the server side, and it isn't reaching the last state of Wyoming. 相反,客户端正在跳到服务器端,并且没有达到怀俄明州的最后状态。 I tried to add in some synchronization as per this question . 我试图根据这个问题添加一些同步。

mycodeoutput

I've linked the full code on pastebin 我已将完整代码链接到pastebin

FixedMessageSequenceClient FixedMessageSequenceClient

FixedMessageSequenceServer FixedMessageSequenceServer

FixedMessageSequenceProtocol FixedMessageSequenceProtocol

In the code, I am using a simple text log file of integer numbers : 在代码中,我正在使用一个简单的整数整数文本日志文件:

1
2
3
4

Note: I am utilizing the Apache ReversedLinesFileReader library . 注意:我正在使用Apache ReversedLinesFileReader库。

I have a suspicion that my error is because of Java IO writing takes too long. 我怀疑我的错误是由于Java IO编写花费的时间太长。 I think it may be because of this block of code: 我认为可能是由于以下代码块:

 for (int i = modlastNum; i < KKJokes.length ; i++){ String fromServer = br.readLine(); out.println(KKJokes[ i % KKJokes.length ]); if ( (i % 3 )==2){ try { Thread.sleep(11); } catch (InterruptedException ie) {} } // fromServer = br.readLine(); System.out.println ( fromServer ); fromServer = br.readLine(); System.out.println ( fromServer ); // String fromServer3 = br.readLine(); System.out.println ( fromServer3 ) ; System.out.println ( fromServer ) ; if(fromServer.equals("inputOfYes")){ while (!(fromServer.equals("nowlogged"))) { fromServer = br.readLine(); } } System.out.println ( fromServer ) ; } // end-forLoop starting at Ln. 93 

And so my hunch is that my automated sending of queries to the FixedMessageSequenceServer class (by way of the FixedMessageSequenceProtocol class ) is perhaps going too fast, and maybe the IO inside of FixedMessageSequenceClient is a bottleneck. 所以我的直觉是,我自动发送的查询到FixedMessageSequenceServer类(通过的方式FixedMessageSequenceProtocol类)也许是走得太快,也许IO内的FixedMessageSequenceClient是一个瓶颈。

any tips regarding how to debug this or figure out are appreciated, thanks 感谢有关如何调试或解决任何技巧

No need for new answers. 无需新答案。 Thanks to Paul above, for encouraging me to figure it out on own : 感谢上面的保罗,鼓励我自己解决这个问题:

The issue was that, I overlooked the importance of FixedSequenceMessageProtocol class 问题是,我忽略了FixedSequenceMessageProtocol类的重要性

Since the Client is hard-coded to send everything in the large StateResponses array, it needed to have a fourth field for "ack" (should ideally be a multidimensional array), which pairs with the Server's sending of "nowlogged" : 由于客户端经过硬编码后可以在大型StateResponses数组中发送所有内容,因此需要为“ ack”(理想情况下为多维数组)添加第四个字段,该字段与服务器的“ nowlogged”发送配对:

StateResponses[] =  { "Permission granted." , "What is Alabama population", "y", "ack",   //00
                               "Permission granted." , "What is Alaska population", "y", "ack",   

After this, in the FixedSequenceMessageProtocol class I added a new constant final variable, to indicate the pending-status after we log a state position, SENTPERMISSION2 : 之后,在FixedSequenceMessageProtocol类中,我添加了一个新的常量最终变量,以表示在我们记录状态位置SENTPERMISSION2后的未决状态:

private static final int WAITING = 0;
private static final int SENTPERMISSION = 1;
private static final int SENTPERMISSION2 = 2; // ** new line
private static final int SENTCLUE =  3;   
private static final int ANOTHER = 4; 

Following, I just need to add another case within the nested-if blocks in the FixedSequenceMessageProtocol class: 接下来,我只需要在FixedSequenceMessageProtocol类的nested-if块中添加另一种情况:

  /* etc more code */ {
        if (theInput.equalsIgnoreCase(clues[currentPopulationRequest])) {  
            theOutput = answers[currentPopulationRequest] + " Want another? (y/n)";
            currentPopulationRequest++; 
            state = SENTPERMISSION2 ;   //sentpermssion2,  & in sentPermission2 put another
        } else {
            theOutput = "You're supposed to say \"" + 
            clues[currentPopulationRequest]  + 
            "! Try again. Request to send a state population";
            state = SENTPERMISSION;
        }
    } 

    else if (state == SENTPERMISSION2) {
        if (theInput.equalsIgnoreCase("y")) {
            theOutput = "Status logged";
            state = ANOTHER;
        } else {
            theOutput = "Bye.";       
            state = WAITING;
        }
    }

    else if (state == ANOTHER) {
        if (theInput.equalsIgnoreCase("ack")) { /* etc more code */

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

相关问题 如何使我的Java Swing应用程序成为客户端 - 服务器应用程序? - How to make my Java Swing application a Client-Server application? 如何使客户端服务器Java应用程序在一个端口上发送消息,而在另一个端口上接收消息? - How do I make a client-server Java application to send messages on one port but receive them on another? PHP客户端和Java Server之间的客户端 - 服务器网络 - Client-Server Networking Between PHP Client and Java Server 如何进行循环以从服务器(在客户端-服务器应用程序中)读取/下载文件? - How do I make a loop to read/download file from server (in client-server application)? 如何在客户端服务器程序中集成log4j日志记录? - How do I integrate log4j logging in my client-server program? 如何在Java的客户端服务器应用程序中读写多行 - How do I read-write multiple lines in client-server app in Java 如何在Java客户端服务器程序中将消息发送到多个客户端 - How do i send the message to multiple clients in java Client-Server Program java 中的算术序列。如何解决我的编码问题? 我的代码带有 output 但它不是正确的 output - Arithmetic sequence in java. How to solve my coding? My code comes up with an output but its not the correct output Java客户端服务器 - Java client-server Java网络-客户端和服务器 - Java Networking - Client and server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM