Dampoe 3 Share Posted November 25, 2018 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 More sharing options...
Milasoft 202 Share Posted November 25, 2018 Are you getting an exception? or what exactly is happening? Link to comment Share on other sites More sharing options...
ozeki6 32 Share Posted November 25, 2018 I can't spot anything wrong. I think the problem is elsewhere. Link to comment Share on other sites More sharing options...
Dampoe 3 Author Share Posted November 25, 2018 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 More sharing options...
ozeki6 32 Share Posted November 25, 2018 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 More sharing options...
ozeki6 32 Share Posted November 26, 2018 (edited) 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 November 26, 2018 by ozeki6 Link to comment Share on other sites More sharing options...
Dampoe 3 Author Share Posted November 26, 2018 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: Server side: Link to comment Share on other sites More sharing options...
Milasoft 202 Share Posted November 26, 2018 Try flushing the outputstream after writing the object. Also, make sure if you are using packages that PlayerState is in the exact same package on both programs. Link to comment Share on other sites More sharing options...
ozeki6 32 Share Posted November 26, 2018 (edited) 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: Server side: 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 November 26, 2018 by ozeki6 Link to comment Share on other sites More sharing options...
Dampoe 3 Author Share Posted November 26, 2018 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now