Jump to content

Server/Client only sends one line to eachother [SOLVED]


Nor3g

Recommended Posts

I'm trying to build a mule script, and thus trying to build a server/client, where the client is the bot.

 

This is the client side of the code:

        while (true) {
                out.println(Protocol.REQUESTING_MULE);
                out.flush();
            System.out.println(in.readLine());

            if (in.readLine().startsWith("mule:")) {
                mule = in.readLine().substring(5);
                out.println(Protocol.GOT_MULE);
                out.flush();
                System.out.println(in.readLine());
               }
            if (in.readLine().equals(Protocol.REQUESTING_WORLD)) {
                out.println("World:123");
                out.flush();
                System.out.println(in.readLine());
            }
            if (in.readLine().equals(Protocol.REQUESTING_NAME)) {
                out.println("bot:bot");
                out.flush();
                System.out.println(in.readLine());
            }'
            Thread.sleep(2000);
        }

This is the server side of the code:

                while (true) {
                    System.out.println(in.readLine());
                    if (in.readLine().equals(Protocol.REQUESTING_MULE)); {
                        out.println("mule:mule");
                        out.flush();
                        System.out.println(in.readLine());
                    }
                    if (in.readLine().equals(Protocol.GOT_MULE)) {
                        out.println(Protocol.REQUESTING_WORLD);
                        out.flush();
                        System.out.println(in.readLine());
                    }
                    if (in.readLine().startsWith("World:")) {
                        world = in.readLine().substring(8);
                        out.println(Protocol.REQUESTING_NAME);
                        out.flush();
                        System.out.println(in.readLine());
                    }
                    if (in.readLine().startsWith("bot:")) {
                        bot = in.readLine().substring(4);
                        System.out.println(in.readLine());
                    }
                    System.out.println(bot);
                    System.out.println(world);

                    //Thread.sleep(2000);
                }

Console output, client side: image.png.af42a2ef94d2c09207999b585cbc48a0.png

Console output, server side: image.png.db4a586bff9875d4d34baefe5a01fe8b.png

 

The protocol:

public class Protocol {
    public static final String BREAK = "0";
    public static final String REQUESTING_MULE = "1";
    public static final String REQUESTING_NAME = "2";
    public static final String REQUESTING_WORLD = "3";
    public static final String GOT_MULE = "4";

}

 

I thought that because they were in a while(true) they would finish the conversation, but it seems they get stuck on the first message they are supposed to send each.
I've tried messing around with switch(in.println) and a lot of other stuff but I can't seem to get it to work properly. Any help would be greatly appreciated!

Edited by Nor3g
Link to comment
Share on other sites

readLine is a blocking call and waits for input.

Your issue is that your making multiple reads when you should be just reading the value once at the time of your while loop.

msg = in.ReadLine();

if(msg == ..){
  
} else if (msg == ..){

} else {

}

You have to make your client and server very ping pong like:

If you read, then you must immediately write and vice versa. However, there should only be one read and it should be at the top.

 

You can look at this but it might be a bit confusing:

Also make your outputstream autoflash (set true in constructor)

Edited by dreameo
Link to comment
Share on other sites

8 hours ago, dreameo said:

readLine is a blocking call and waits for input.

Your issue is that your making multiple reads when you should be just reading the value once at the time of your while loop.


msg = in.ReadLine();

if(msg == ..){
  
} else if (msg == ..){

} else {

}

You have to make your client and server very ping pong like:

If you read, then you must immediately write and vice versa. However, there should only be one read and it should be at the top.

 

You can look at this but it might be a bit confusing:

Also make your outputstream autoflash (set true in constructor)

 

I don't know how to set outputstream to autoflush if that's what you meant. 

After implementing the other changes you suggested, and some other changes (like closing the socket), it eventually gets it right:

image.png.d01bf36f3fa86ba7f501b2535ec9c4ac.png

 

Even though It isn't breaking the program, I don't know why the client spams three consecutive messages to the server, or maybe it's the server spam printing one message three times to the console, but I'd rather have it a clean conversation. I'm going to work on this some more...

Thank you so much for helping me! :) Greatly appreciated!

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...