Solved issue with sockets

Discussion in 'Plugin Development' started by Ace_Wolf2, Jun 9, 2017.

Thread Status:
Not open for further replies.
  1. Offline


    currently I have a plugin that get's data from a database and adds said data to a sign, the issue is it crashes on the Socket with the stacktrace "socket timed out" all other code works. the socket gets the player count for that server that the sign is linked to.

        private void repeatingSignUpdater(){
            task = Bukkit.getScheduler().runTaskLaterAsynchronously(this, new Runnable(){
                public void run() {
                    System.out.println("time test 1");
                    for(Sign s : signs){                       
                        sign = s;
                            serverName = ChatColor.stripColor(s.getLine(0));
                            //String server = mysql.getServers(serverName);   
                            port = mysql.getServerPort(serverName);
                            ip = mysql.getServerIp(serverName);
                            states =  mysql.getState(serverName);
                        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                            public void run() {
                                try {
                                    System.out.println(ip + " "+ port);
                                    Socket socket = new Socket();
                                    socket.connect(new InetSocketAddress(ip , port), 1 * 1000);
                                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                                    DataInputStream in = new DataInputStream(socket.getInputStream());
                                    StringBuilder str = new StringBuilder();
                                    int b;
                                    while ((b = != -1) {
                                        if (b != -1 && b > 16 && b != 255 && b != 23 && b != 24) {
                                            str.append((char) b);
                                    String[] data = str.toString().split("ยง");;
                                    onlinePlayers = Integer.valueOf(data[1]);
                                    int maxPlayers = 20; //Integer.valueOf(data[2]);
                                    //int count = maxPlayers;
                                    if(states == null){
                                        if(serverName.equalsIgnoreCase("survival") || serverName.equalsIgnoreCase("skyblock")){
                                            sign.setLine(2, states);
                                            sign.setLine(3, onlinePlayers + "/" + 100);
                                            sign.setLine(2, states);
                                            sign.setLine(3, onlinePlayers + "/" + maxPlayers);
                                } catch (Exception e) {
                                    sign.setLine(2, ChatColor.RED + "Error.");
  2. Offline


    Does the socket connection get established? Or does it just attempt to and fail?

    The only thought that comes to mind is that you're connecting to a socket that is on the servers port and that the ServerSocket might be listening on another port.
  3. Offline


    Add a little more time to the timeout may be.

    If you wanna send data, you need to call the method flush()
  4. Offline


    After a bit of work and looking at the ports that the socket listened to as suggested. it seems my issue was in the for loop looping through before the socket was even called, resulting in only the last port being called by the socket which was a closed server. I removed the for loop in replaced it for repeating tasks. so far it seems my issue was solved. I also took @Caderape2 advice and added a bit more time to the timeout just to be safe. no lag or crashes :) Thanks
Thread Status:
Not open for further replies.

Share This Page