Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Server/Client Socket passing Serializable Object


    Dampoe

    Recommended Posts

    Hello guys, I need some help creating this script. I have created a mule script that is based on clan chat. However I want to get away from this method for obvious reasons and I have been looking in to sockets lately. I'm trying to send a PlayerState from the client to the server so that the server can do something with this information and then possibly return it back to the client. However, I am struggling to pass this object to the server. The connection seems to be working fine, my client connects to the server correctly. However, when I try to pass the PlayerState the server seems to not be able to read it correctly. Take a look at my code, it's very basic. Very simple as I have never used anything like this before. Any help is appreciated.

    This is the client side

     

       try {
                PlayerState state = new PlayerState(getLocalPlayer().getName(),
                        getClient().getCurrentWorld(), 200, false);
                MethodProvider.log("Current PlayerState: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                MethodProvider.sleep(3000);
    
                MethodProvider.log("Connecting...");
                Socket socket = new Socket("127.0.0.1", PORT);
    
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
    
                outputStream.writeObject(state);
    
                state = (PlayerState) inputStream.readObject();
    
                MethodProvider.log("Received from server: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                outputStream.close();
                inputStream.close();
    
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }

     

    This is the server side

            PlayerState state = null;
    
            try {
                ServerSocket socket = new ServerSocket(PORT);
    
                MethodProvider.log("Waitinng for connection...");
    
                Socket pipe = socket.accept();
                MethodProvider.log("Connected!" + pipe.toString());
    
                ObjectInputStream inputStream = new ObjectInputStream(pipe.getInputStream());
                ObjectOutputStream outputStream = new ObjectOutputStream(pipe.getOutputStream());
    
                state = (PlayerState)inputStream.readObject();
    
                state.setGoTrade(true);
    
                outputStream.writeObject(state);
    
                MethodProvider.log("Succesfully sent follow state: " + state);
    
                inputStream.close();
                outputStream.close();
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            return z.rh(60000,90000);
        }

    And last, is the PlayerState class:

    public class PlayerState implements Serializable {
    
        private static final long serialVersionUID = -6541258022921922946L;
    
        private String name;
        private int world;
        private int itemCount;
        private boolean goTrade;
    
        public PlayerState(String name, int world, int itemCount, boolean goTrade) {
            this.name = name;
            this.world = world;
            this.itemCount = itemCount;
            this.goTrade = goTrade;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getWorld() {
            return world;
        }
    
        public void setWorld(int world) {
            this.world = world;
        }
    
        public int getItemCount() {
            return itemCount;
        }
    
        public void setItemCount(int itemCount) {
            this.itemCount = itemCount;
        }
    
        public boolean isGoTrade() {
            return goTrade;
        }
    
        public void setGoTrade(boolean goTrade) {
            this.goTrade = goTrade;
        }
    }

     

    Link to comment
    Share on other sites

    6 hours ago, Milasoft said:

    Are you getting an exception? or what exactly is happening?

     

    2 hours ago, ozeki6 said:

    I can't spot anything wrong. I think the problem is elsewhere. 

    That's weird.. Because I can't see the logs of the following in my debug console:

    Client side is never showing this:

     MethodProvider.log("Received from server: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());

     

    And server side is never showing this:

     MethodProvider.log("Succesfully sent follow state: " + state);

     

    The other logs are showing in the console apart from these two. 

    Link to comment
    Share on other sites

    1 minute ago, Dampoe said:

     

    That's weird.. Because I can't see the logs of the following in my debug console:

    Client side is never showing this:

    
     MethodProvider.log("Received from server: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());

     

    And server side is never showing this:

    
     MethodProvider.log("Succesfully sent follow state: " + state);

     

    The other logs are showing in the console apart from these two. 

    That's because the object wasn't sent or because there is no connection.

    Add this into your client side

    MethodProvider.log("Connecting...");
                Socket socket = new Socket("127.0.0.1", PORT);
    

    after you connect add :

    MethodProvider.log("Connected");

     

    If it logs that it means the problem is here :

     

    ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
    ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
    
    outputStream.writeObject(state);
    
    state = (PlayerState) inputStream.readObject();
    Link to comment
    Share on other sites

    On 11/25/2018 at 2:18 AM, Dampoe said:

    Hello guys, I need some help creating this script. I have created a mule script that is based on clan chat. However I want to get away from this method for obvious reasons and I have been looking in to sockets lately. I'm trying to send a PlayerState from the client to the server so that the server can do something with this information and then possibly return it back to the client. However, I am struggling to pass this object to the server. The connection seems to be working fine, my client connects to the server correctly. However, when I try to pass the PlayerState the server seems to not be able to read it correctly. Take a look at my code, it's very basic. Very simple as I have never used anything like this before. Any help is appreciated.

    This is the client side

     

    
       try {
                PlayerState state = new PlayerState(getLocalPlayer().getName(),
                        getClient().getCurrentWorld(), 200, false);
                MethodProvider.log("Current PlayerState: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                MethodProvider.sleep(3000);
    
                MethodProvider.log("Connecting...");
                Socket socket = new Socket("127.0.0.1", PORT);
    
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
    
                outputStream.writeObject(state);
    
                state = (PlayerState) inputStream.readObject();
    
                MethodProvider.log("Received from server: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                outputStream.close();
                inputStream.close();
    
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }

     

    This is the server side

    
            PlayerState state = null;
    
            try {
                ServerSocket socket = new ServerSocket(PORT);
    
                MethodProvider.log("Waitinng for connection...");
    
                Socket pipe = socket.accept();
                MethodProvider.log("Connected!" + pipe.toString());
    
                ObjectInputStream inputStream = new ObjectInputStream(pipe.getInputStream());
                ObjectOutputStream outputStream = new ObjectOutputStream(pipe.getOutputStream());
    
                state = (PlayerState)inputStream.readObject();
    
                state.setGoTrade(true);
    
                outputStream.writeObject(state);
    
                MethodProvider.log("Succesfully sent follow state: " + state);
    
                inputStream.close();
                outputStream.close();
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            return z.rh(60000,90000);
        }

    And last, is the PlayerState class:

    
    public class PlayerState implements Serializable {
    
        private static final long serialVersionUID = -6541258022921922946L;
    
        private String name;
        private int world;
        private int itemCount;
        private boolean goTrade;
    
        public PlayerState(String name, int world, int itemCount, boolean goTrade) {
            this.name = name;
            this.world = world;
            this.itemCount = itemCount;
            this.goTrade = goTrade;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getWorld() {
            return world;
        }
    
        public void setWorld(int world) {
            this.world = world;
        }
    
        public int getItemCount() {
            return itemCount;
        }
    
        public void setItemCount(int itemCount) {
            this.itemCount = itemCount;
        }
    
        public boolean isGoTrade() {
            return goTrade;
        }
    
        public void setGoTrade(boolean goTrade) {
            this.goTrade = goTrade;
        }
    }

     

    Try this. Delete ObjectInputStream from the client side.

    In the server declare ObjectInputStream and ObjectOutputStream right before using them.

    So it should be like 

    ObjectInputStream inputStream = new ObjectInputStream(pipe.getInputStream());
    
    state = (PlayerState)inputStream.readObject();
    
    state.setGoTrade(true);
    
    ObjectOutputStream outputStream = new ObjectOutputStream(pipe.getOutputStream());
    outputStream.writeObject(state);

    Read this 

    https://stackoverflow.com/questions/21863987/thread-hangs-when-creating-objectinputstream

    Edited by ozeki6
    Link to comment
    Share on other sites

    14 minutes ago, ozeki6 said:

    Try this. Delete ObjectInputStream from the client side.

    In the server declare ObjectInputStream and ObjectOutputStream right before using them.

    So it should be like 

    
    ObjectInputStream inputStream = new ObjectInputStream(pipe.getInputStream());
    
    state = (PlayerState)inputStream.readObject();
    
    state.setGoTrade(true);
    
    ObjectOutputStream outputStream = new ObjectOutputStream(pipe.getOutputStream());
    outputStream.writeObject(state);

     

    I tried, still didn't print out after I send the object from Client to Server. This is the log I am getting from Client side:

    image.png.8d2702d3d498fdd14ab3a4dfc7ee85ba.png 

    Server side:

    image.png.0a96a10a17e2315ffc5ad0e47dc1b422.png

     

    Link to comment
    Share on other sites

    19 minutes ago, Dampoe said:

    I tried, still didn't print out after I send the object from Client to Server. This is the log I am getting from Client side:

    image.png.8d2702d3d498fdd14ab3a4dfc7ee85ba.png 

    Server side:

    image.png.0a96a10a17e2315ffc5ad0e47dc1b422.png

     

    https://stackoverflow.com/questions/21863987/thread-hangs-when-creating-objectinputstream

    Your code should be :

     try {
                PlayerState state = new PlayerState(getLocalPlayer().getName(),
                        getClient().getCurrentWorld(), 200, false);
                MethodProvider.log("Current PlayerState: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                MethodProvider.sleep(3000);
    
                MethodProvider.log("Connecting...");
                Socket socket = new Socket("127.0.0.1", PORT);
    
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                outputStream.writeObject(state);
    
                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                state = (PlayerState) inputStream.readObject();
    
                MethodProvider.log("Received from server: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                outputStream.close();
                inputStream.close();
    
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
    PlayerState state = null;
    
            try {
                ServerSocket socket = new ServerSocket(PORT);
    
                MethodProvider.log("Waitinng for connection...");
    
                Socket pipe = socket.accept();
                MethodProvider.log("Connected!" + pipe.toString());
    
                ObjectInputStream inputStream = new ObjectInputStream(pipe.getInputStream());
                state = (PlayerState)inputStream.readObject();
    
                state.setGoTrade(true);
    
                ObjectOutputStream outputStream = new ObjectOutputStream(pipe.getOutputStream());
                outputStream.writeObject(state);
    
                MethodProvider.log("Succesfully sent follow state: " + state);
    
                inputStream.close();
                outputStream.close();
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            return z.rh(60000,90000);
        }

     

     

     

     

    Edited by ozeki6
    Link to comment
    Share on other sites

    52 minutes ago, ozeki6 said:

    https://stackoverflow.com/questions/21863987/thread-hangs-when-creating-objectinputstream

    Your code should be :

    
     try {
                PlayerState state = new PlayerState(getLocalPlayer().getName(),
                        getClient().getCurrentWorld(), 200, false);
                MethodProvider.log("Current PlayerState: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                MethodProvider.sleep(3000);
    
                MethodProvider.log("Connecting...");
                Socket socket = new Socket("127.0.0.1", PORT);
    
                ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                outputStream.writeObject(state);
    
                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                state = (PlayerState) inputStream.readObject();
    
                MethodProvider.log("Received from server: ");
                MethodProvider.log("Name = " + state.getName()
                        + "\n World = " + state.getWorld()
                        + "\n ItemCount = " + state.getItemCount()
                        + "\n Ready for trade: " + state.isGoTrade());
    
                outputStream.close();
                inputStream.close();
    
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
    
    PlayerState state = null;
    
            try {
                ServerSocket socket = new ServerSocket(PORT);
    
                MethodProvider.log("Waitinng for connection...");
    
                Socket pipe = socket.accept();
                MethodProvider.log("Connected!" + pipe.toString());
    
                ObjectInputStream inputStream = new ObjectInputStream(pipe.getInputStream());
                state = (PlayerState)inputStream.readObject();
    
                state.setGoTrade(true);
    
                ObjectOutputStream outputStream = new ObjectOutputStream(pipe.getOutputStream());
                outputStream.writeObject(state);
    
                MethodProvider.log("Succesfully sent follow state: " + state);
    
                inputStream.close();
                outputStream.close();
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            return z.rh(60000,90000);
        }

     

    That's exactly what my code looks like. I have absolutely no idea what is going wrong. I'll upload them to Git tomorrow so anyone can take a look at it to solve this issue. I'm going to bed now

     

     

     

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.