Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti threading multi client chat server
    primarykey
    data
    text
    <p>The title was bad i know but let me explain my situation. Okay, i am making a server that sits off to the side and communicates with my minecraft client. I can get one client to connect and transfer data back and fourth. When I try to connect with a second client i get the error:</p> <pre><code>java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source) at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source) at java.io.ObjectOutputStream.&lt;init&gt;(Unknown Source) at ConnectionHandler.setupStreams(ConnectionHandler.java:71) at ConnectionHandler.HandleChat(ConnectionHandler.java:30) at ConnectionHandler.&lt;init&gt;(ConnectionHandler.java:23) at Server.waitForConnection(Server.java:118) at Server.RunAll(Server.java:65) at Server.run(Server.java:50) at ServerTest.main(ServerTest.java:9) </code></pre> <p>Now in consequence, here is the resulting code:</p> <p>setupStreams: Line 71</p> <pre><code>public static void setupStreams() throws IOException { for(int i = 1; i &lt;= Server.ConnectionArray.size(); i++) { Socket TempSocket = (Socket) Server.ConnectionArray.get(i - 1); output = new ObjectOutputStream(TempSocket.getOutputStream());//Line 71 output.flush(); input = new ObjectInputStream(TempSocket.getInputStream()); whileChatting(); } } </code></pre> <p>Now i feel it would be best to explain how all this code is set up:</p> <p>It starts in a Class called ServerTest.Java Code:</p> <pre><code>public class ServerTest { public static void main(String[] args) { Server Minecraft = new Server(); Minecraft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Minecraft.run(); } } </code></pre> <p>I called the object "Minecraft". </p> <p>Now the class that it refers too implements the Runnable class and begins with the run() method. Here is that method:</p> <pre><code>@Override public void run() { RunAll(); } public void RunAll() { ConnectionHandler.showMessage("Waiting for client connections..."); try { server = new ServerSocket(1337, 10); while(true) { try { waitForConnection(); } catch (IOException e) { e.printStackTrace(); } } } catch(IOException Exception) { Exception.printStackTrace(); } finally { ConnectionHandler.closeCrap(); } } </code></pre> <p>Now from here it waits for connections and i think it loops back repeatedly but i don't know. Here is the code for that Method:</p> <pre><code>public void waitForConnection() throws IOException { connection = server.accept(); ConnectionArray.add(connection); ConnectionHandler connections = new ConnectionHandler(connection); connection = null; } </code></pre> <p>Now i think that what this method does is accepts the incoming connection from the client then adds that connection to my array of sockets, then hands the socket off to the handler class then sets the original socket back to Null to to wait for another incoming connection.</p> <p>Now this bring us to the ConnectionHandler class. In here it takes a socket X and sets it to the global socket as shown here:</p> <pre><code>static Socket Socket; public PrintWriter Out; String Message = ""; public static ObjectOutputStream output; public static ObjectInputStream input; public static int Players = 0; public ConnectionHandler(Socket X) { this.Socket = X; HandleChat(); } </code></pre> <p>This class also implements the Runnable class. But as you can see it is setting the global socket to X then running HandleChat(); Here is the Method:</p> <pre><code>public static void HandleChat() { try { setupStreams(); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <p>As you can see all this is doing is running the setupStreams(); method. Here is that method aswell:</p> <pre><code>public static void setupStreams() throws IOException { for(int i = 1; i &lt;= Server.ConnectionArray.size(); i++) { Socket TempSocket = (Socket) Server.ConnectionArray.get(i - 1); output = new ObjectOutputStream(TempSocket.getOutputStream()); output.flush(); input = new ObjectInputStream(TempSocket.getInputStream()); whileChatting(); } } </code></pre> <p>Now i "Think" This method creates a new socket called TempSocket and making it = the latest connection in the array then setting the output and input streams to that socket resulting in a socket for that individual client then runs whileChatting(); Here is the resulting code:</p> <pre><code>public static void whileChatting() throws IOException { String message = ""; do { try { message = (String) input.readObject(); showMessage("\n" + message); sendMessage("", message); } catch(ClassNotFoundException classNotFoundException) { ConnectionHandler.showMessage("\n idk wtf that user sent!"); } }while(!message.equals("/stop")); } </code></pre> <p>Again with the "Thinking" here. I think the read object is being set to the String message; Then being sent to the client and displayed on the consoles screen. (Swing) following this is the code for both methods (Send) and (Show):</p> <pre><code>public static void sendMessage(String message, String returnedMessage) { try { if(!message.isEmpty()) { output.writeObject("\247c[Server]\247d " + message); } else { output.writeObject(returnedMessage); } output.flush(); showMessage("\n[Server] " + message); } catch(IOException ioException) { Server.chatWindow.append("\n ERROR: DUDE I CANT SEND THAT MESSAGE"); } } static void showMessage(final String text) { SwingUtilities.invokeLater ( new Runnable() { public void run() { Server.chatWindow.append(text); } } ); } </code></pre> <p>Now as you can see here, When i would like to display a message on the client's side; I want it to look like: [Server] text. Also you will notice that it also sys returned message. I have the client set up to only sent to the server then display whats returned on screen so lets say i type "Hay" It will display nothing on my screen upon sending but then once the server receives it it will send me exact text back to everyone. That's the sendMessage(); Method. Now the display method is rather simple. it displays text upon receiving.</p> <p>That's the code in a nutshell basically. I would rather leave you with too much information i can than leave you sitting here wondering what is going on. Once more i am going to explain my situation more in depth. I am making a server that communicates with my minecraft client. one client connects fine but when another client tries to connect it errors and when the first client disconnects, the server will not allow the client to re-connect. I think that may have something to do with the array of connections and the socket is not being removed from the array upon disconnect but i dont know. Now finally, my question for you is how can i fix these errors? Thanks in advance for any replies i get on this question.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload