Nor3g Posted January 29, 2019 Share Posted January 29, 2019 (edited) 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: Console output, server side: 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 January 30, 2019 by Nor3g Quote Link to comment Share on other sites More sharing options...
dreameo Posted January 29, 2019 Share Posted January 29, 2019 (edited) 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 January 29, 2019 by dreameo Quote Link to comment Share on other sites More sharing options...
Nor3g Posted January 30, 2019 Author Share Posted January 30, 2019 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: 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.