Jump to content

Recommended Posts

Posted

Hello, I have a project that consists of linking several clients together without the mod being installed on the server, even if the players are on a different server the communication must be possible so I decided to use the Socket after several attempts it works the worry is that after calling the method to accept a connection on the server side, minecraft stops as if the main thread stopped when I close the vps minecraft resumes as if nothing was wrong, I use threads whether in client or server to make the connection and exchange information, is the socket the right way used if yes how could I do not stop the main thread of minecraft here are the client code and the server one 

  public ServerListener() {
       debug("Creating the server");
       clientList = new ArrayList<ConnectionToClient>();
       messages = new LinkedBlockingQueue<Object>();
       debug("Client list is created");
       try {
           serverSocket = new ServerSocket(2406);
           debug("opening the port 2406");
       } catch (IOException e) {
         e.printStackTrace();
           debug(e.getMessage());
       }
    debug("creating the accept thread");
       Thread accept = new Thread() {
           public void run() {
    debug("thread created");
               while (true) {
                   try {
                       Socket s = serverSocket.accept();
if(s!=null) {
                       debug("new client joined");
                       clientList.add(new ConnectionToClient(s));
                       debug("new client was accepted ["+ s.getInetAddress() + "/" + s.getPort()+"]");
                
}
 } catch (IOException e) {
                       e.printStackTrace();
                      debug(e.getMessage());
                   }
               }
           }
       };
       accept.setDaemon(true);
       accept.start();

       Thread messageHandling = new Thread() {
           public void run() {
               while (true) {
                   try {
                       Object message = messages.take();
                       debug("we parse the ping");
                       parse(message);
                       System.out.println("Message Received: " + message);
                   } catch (InterruptedException e) {
                   }
               }
           }
       };

       messageHandling.setDaemon(true);
       messageHandling.start();
   }

   private void parse(Object message) {
       String msg = String.valueOf(message);
       if(msg.contains("-")) {
           if(msg.contains("key")) {
               debug("key detected");
               key = msg.split("-")[1];
           } else if(msg.contains("name")) {
               debug("name detected");
               name = msg.split("-")[1];
           } else if(msg.contains("world")) {
               debug("worldname detected");
               world = msg.split("-")[1];
           } else if(msg.contains("server")) {
               debug("servername detected");
               server = msg.split("-")[1];
           } else if(msg.contains("x")) {
               debug("x detected");
               x = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("y")) {
           debug("y detected");
               y = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("z")) {
                debug("z detected");
               z = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("isEntity")) {
                   debug("entity detected");
               entity = msg.split("-")[1].equalsIgnoreCase("true");
           } else if(msg.contains("ticks")) {
               debug("ticks detected");
               lTicks = Integer.parseInt(msg.split("-")[1]);
           } else if(msg.contains("end")) {
               debug("we got everything detected");
               toSend = new IrcMPING(key, name, world, server, x, y, z, entity, lTicks);
               for(String str : toSend.getArgs()) {
                   
                   sendToAll(str);
               }
               toSend=null;
           }
       }
   }

   public void sendToOne(int index, Object message) throws IndexOutOfBoundsException {
       clientList.get(index).write(message);
   }

   public void sendToAll(Object message) {
       for (ConnectionToClient client : clientList) {
           debug("sending the ping to " + client.socket.getInetAddress());
           client.write(message);
       }
   }

   public void debug(String str) {
           System.out.println("SOROS DEBUG [MULTIPING] : " + str) ;
     
       }

   private class ConnectionToClient {
       ObjectInputStream in;
       ObjectOutputStream out;
       Socket socket;

       ConnectionToClient(Socket socket) throws IOException {
           this.socket = socket;
           in = new ObjectInputStream(socket.getInputStream());
           out = new ObjectOutputStream(socket.getOutputStream());

           Thread read = new Thread() {
               public void run() {
                   while (true) {
                       try {
                           Object obj = in.readObject();
                           messages.put(obj);
                       } catch (IOException e) {
                           e.printStackTrace();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       } catch (ClassNotFoundException e) {
                           e.printStackTrace();
                       }
                   }
               }
           };

           read.setDaemon(true); // terminate when main ends
           read.start();
       }

       public void write(Object obj) {
           try {
     
               out.writeObject(obj);
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       
    
   }

}
public IRCClient() throws IOException {
        socket = new Socket(InetAddress.getByAddress(new byte[]{(byte) 185, (byte) 242, (byte) 180,97}), 2406);
        messages = new LinkedBlockingQueue<Object>();
        server = new ConnectionToServer(socket);

        Thread messageHandling = new Thread() {
            public void run() {
                while (true) {
                    try {
                        Object message = messages.take();
                        parse(message);
                        System.out.println("Message Received: " + message);
                    } catch (InterruptedException e) {
                    }
                }
            }
        };

        messageHandling.setDaemon(true);
        messageHandling.start();
    }

    private void parse(Object message) {
        String msg = String.valueOf(message);
        if (msg.contains("-")) {
            if (msg.contains("key")) {
                key = msg.split("-")[1];
            } else if (msg.contains("name")) {
                name = msg.split("-")[1];
            } else if (msg.contains("world")) {
                world = msg.split("-")[1];
            } else if (msg.contains("server")) {
                serverName = msg.split("-")[1];
            } else if (msg.contains("x")) {
                x = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("y")) {
                y = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("z")) {
                z = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("isEntity")) {
                entity = msg.split("-")[1].equalsIgnoreCase("true");
            } else if (msg.contains("ticks")) {
                lTicks = Integer.parseInt(msg.split("-")[1]);
            } else if (msg.contains("end")) {
                if (key.equalsIgnoreCase(MultiPingMod.getKey()) && !name.equalsIgnoreCase(Minecraft.getMinecraft().thePlayer.getName())) {
                    MultiPing m = new MultiPing(name, world, serverName, x, y, z, entity);
                    MultiPingMod.setTime(lTicks);
                    m.render = true;
                    if (MultiPingMod.render.getToRender().containsKey(name)) {
                        MultiPingMod.render.getToRender().get(name).render = false;
                        MultiPingMod.render.getToRender().remove(name);
                    }
                    MultiPingMod.render.addToRender(m);
                }
            }
        }
    }

    public void send(Object obj) {
        server.write(obj);
    }

    private class ConnectionToServer {
        ObjectInputStream in;
        ObjectOutputStream out;
        Socket socket;

        ConnectionToServer(Socket socket) throws IOException {
            this.socket = socket;
            in = new ObjectInputStream(socket.getInputStream());
            out = new ObjectOutputStream(socket.getOutputStream());

            Thread read = new Thread() {
                public void run() {
                    while (true) {
                        try {
                            Object obj = in.readObject();
                            messages.put(obj);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };

            read.setDaemon(true);
            read.start();
        }

        private void write(Object obj) {
            try {
                out.writeObject(obj);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

 

Posted
  On 4/21/2021 at 7:13 AM, diesieben07 said:

Using raw threads, raw sockets, ObjectOutputStream and terrible exception handling. All of these are no-gos. You should use Netty.

Expand  

So netty allow me to communicate between my minecraft and a distant vps right ? There is some way to use it with forge or should i use it like i can use it on normal Java programme ? 

Posted (edited)
  On 4/21/2021 at 12:11 PM, diesieben07 said:

Netty is a library for network communication. Minecraft already uses it for its networking so you can "just use it".

Expand  

I implémented Netty everything seems working the server actually receive the connection of the client but the game still stop when i connect to the server, how am I supposed to use Netty to get minecraft working ? 

The client part source : https://pastebin.com/v5dN94BJ

Edited by SmokingDude
Posted (edited)

Yes i have a class called '' KeybindListener '' And inside I have this method :

@SubscribeEvent

public void onKeyPress(InputEvent.KeyInputEvent event) { if(Keyboard.isKeyDown(Keyboard.KEY_R))

if(irc==null) {

irc = new IRCClient(''samuele.site'',2406);

irc.run();

}

showgui();

}

 

I do register this class with MinecraftForge.EVENT_BUS.register(new KeybindListener()) ;

  Quote

 

Expand  
Edited by SmokingDude
Posted
  On 4/22/2021 at 1:15 PM, diesieben07 said:

So you are running the network code inside a key press event handler.

Which means you are blocking the main thread while the network code runs (forever).

Expand  

So where i am supposed to run it ? Because I dont see any other way doing it on the init of the mod make minecraft stop on the loading forge screen, can you help me or give me a lead please? 

Posted
  On 4/22/2021 at 2:01 PM, diesieben07 said:

For example, yes.

Expand  

I tried I mean I succeeded and until then there's no more worries the server gets the connection, minecraft no longer stops, but I can not communicate between the client and the server, the client must send a certain string to the server when it performs a specific action no matter how I do it (synchronised irc or synchronised boolean etc the client never sends anything to the server, would you have a lead on what I need to do to get to settle this please) 

Posted (edited)

Yes sorry for my bad english, on the IRCClient class i have a method called '' send '' who basically send a string to the server, this method is called from the KeybindListener when the player hit the '' R '' key, When the server receives a message it is supposed to write ''message received from the client : the message '' But the server does not print anything in the terminal which means that it receives nothing and I think it comes from the fact that IRCClient is declared in a different thread to the thread in which the keyinput event is, and I do not know how to be able to call 'send' from the thread for it to works 

Edited by SmokingDude

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • wait, i just did that. why didn't it work? also it's the same. help me!!!!
    • Culprit has been located! It was Tough As Nails + (plus) which was among one of the mods *added* before re-entering, in an attempt to improve compatibility. It, for some reason or another, did not want to work!
    • "You can either try to load it with only the vanilla data pack ("safe mode"), or go back to the title screen and fix it manually." Hello! I encountered this error just now while attempting to re-enter my world for the first time. Notably, no mods were removed before encountering this error and, while being able to find this error elsewhere on the internet, in my scenario it is seemingly caused by a specific mod, of which I cannot isolate. Things I have tried: - Enter "Safe Mode" (fails) - Remove datapacks (no change) - Reverted to old save data (no change) Here is the server log: https://mclo.gs/9vJQEfN I use Modrinth mod loader so please let me know how to share my mod list, and if it is needed.
    • So, i'm hosting (or attempting to host) a port-forwarded modded server for 1.12.2. There's quite a few mods in this pack, but they all run and have no compatibility issues (besides something causing the game to crash if you go fullscreen, a problem ive given up trying to identify or fix). It can run on 6gb of ram or less, and works fine in singleplayer. All of my friends that want to join have the exact same mod/config/game setup as I do (I personally helped them set everything up). Just to be safe, we've all allocated 12 gigabytes of RAM to the installation, and I've also allocated 12GB to the server itself. I am able to join with no issue and it runs fine. However, when they try to join, it gets stuck in the 'logging in' screen before their game becomes unresponsive. When they close it, it gives them the exit code 805306369, which usually means not enough RAM, but this cant be the case. On my screen, in the server, it shows them joining, and then disconnecting, so I dont think its a port-forwarding issue. They can all run it just fine in singleplayer as well. The annoying bit about this particular issue is that it generates no crash log. Does anyone have any suggestions? Thanks! Here is my debug log, and his, starting from the same place. MINE: [132647] [Netty Client IO #5INFO] [FML] Attempting connection with missing mods [ctm, fusion] at SERVER 132653.817 [132653] [Client threadINFO] [minecraftGuiConnecting] Connecting to 0, 25565 132654.682 [132654] [Netty Client IO #9INFO] [STDOUT] [xaero.common.core.transformer.ClassNodeTransformertransform29] Transforming class brz net.minecraft.client.network.NetHandlerPlayClient 132654.684 [132654] [Netty Client IO #9INFO] [STDOUT] [xaero.map.core.transformer.ClassNodeTransformertransform29] Transforming class net.minecraft.client.network.NetHandlerPlayClient 132654.764 [132654] [Netty Client IO #9INFO] [FML] Server protocol version 2 132654.777 [132654] [Netty Client IO #9INFO] [FML] Attempting connection with missing mods [ctm, fusion] at SERVER 132656.193 [132656] [Client threadINFO] [FML] Injecting existing registry data into this client instance 132659.081 [132659] [Client threadINFO] [FML] Applying holder lookups 132659.082 [132659] [Client threadINFO] [FML] Holder lookups applied 132659.092 [132659] [Netty Client IO #9INFO] [FML] [Netty Client IO #9] Client side modded connection established HIS: [13:12:16] [Netty Client IO #0/INFO] [FML]: Attempting connection with missing mods [ctm, fusion] at SERVER 13:12:25.597 [13:12:25] [Client thread/INFO] [minecraft/GuiConnecting]: Connecting to (my IP address and the server port 25565) 13:12:26.805 [13:12:26] [Netty Client IO #1/INFO] [STDOUT]: [xaero.common.core.transformer.ClassNodeTransformer:transform:29]: Transforming class brz net.minecraft.client.network.NetHandlerPlayClient 13:12:26.808 [13:12:26] [Netty Client IO #1/INFO] [STDOUT]: [xaero.map.core.transformer.ClassNodeTransformer:transform:29]: Transforming class net.minecraft.client.network.NetHandlerPlayClient 13:12:26.936 [13:12:26] [Netty Client IO #1/INFO] [FML]: Server protocol version 2 13:12:27.114 [13:12:27] [Netty Client IO #1/INFO] [FML]: Attempting connection with missing mods [ctm, fusion] at SERVER 13:12:27.480 [13:12:27] [Client thread/INFO] [FML]: Injecting existing registry data into this client instance 13:12:30.669 [13:12:30] [Client thread/INFO] [FML]: Applying holder lookups 13:12:30.671 [13:12:30] [Client thread/INFO] [FML]: Holder lookups applied 13:13:04.700 Process crashed with exit code -805306369 P.S - Both mods "CTM" and "FUSION" are present in both our folders so idk what that's about.
    • Please read the FAQ (link is orange banner at top of page), and post logs as described there, to an external site such as https://mclo.gs  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.