Hello i am new here, i want to ask about a problem of mine.
I have made a server code, so i can send commands from other program C#
i made that but my problem is when i start the java program because is Loading.
The Server is starting at the middle of it, so when i start it is stacking at BufferedRead.Readline so is not continiu at the other loading.
Can someone help me with it?

here the code:

package extensions.ServerManager.Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.ClassNotFoundException;
import java.lang.Runnable;
import java.lang.Thread;
import java.net.ServerSocket;
import java.net.Socket;

import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.datatables.MultiSell;
import com.l2jserver.gameserver.datatables.NpcTable;
import com.l2jserver.gameserver.instancemanager.QuestManager;

import core.L2VoyagerLoader;

import extensions.ServerManager.ServerManagerConfig;

public class ServerManagerSocket 
{
    private ServerSocket server;

    public ServerManagerSocket() {
        try {
            server = new ServerSocket(ServerManagerConfig.SM_PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void loadSocket() 
    {
        ServerManagerSocket sms = new ServerManagerSocket();
        sms.handleConnection();
    }

    public void handleConnection() {
        System.out.println("Waiting for manager command...");

        //
        // The server do a loop here to accept all connection initiated by the
        // client application.
        //
        while (true) {
            try {
                Socket socket = server.accept();
                new ConnectionHandler(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class ConnectionHandler implements Runnable {
    private Socket socket;

    public ConnectionHandler(Socket socket) {
        this.socket = socket;

        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        try
        {
            //
            // Read a message sent by client application
            //
            BufferedReader br=new BufferedReader(new 
                    InputStreamReader(socket.getInputStream()));
            System.out.println(br.readLine());
            PrintWriter wr=new PrintWriter(new 
                    OutputStreamWriter(socket.getOutputStream()),true);
            String message = br.readLine();
            int comid = Integer.getInteger(message);
            switch(comid)
            {
            //Reload Voyager Engine
            case 259023:
                L2VoyagerLoader.load();
                wr.println("Voyager Scripts have been reloaded");
                break;
            //Reload HTML
            case 293474:
                HtmCache.getInstance().reload();
                wr.println("Cache[HTML]: "+ HtmCache.getInstance().getMemoryUsage() + " megabytes on " + HtmCache.getInstance().getLoadedFiles() + " files loaded");
                break;
            //Reload Multisell
            case 234256:
                MultiSell.getInstance().reload();
                wr.println("All Multisells have been reloaded");
                break;
            //Reload NPC
            case 534452:
                NpcTable.getInstance().reloadAllNpc();
                wr.println("All NPCs have been reloaded");
                break;
            //Reload Quests
            case 354574:
                QuestManager.getInstance().reloadAllQuests();
                wr.println("All Quests have been reloaded");
                break;
            }

            wr.close();
            br.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Recommended Answers

All 7 Replies

BufferedReader's readLine() method is a blocking call, so it looks like your program is working as intended by Java, if not by you. When you call readLine(), your program is supposed to wait until there is a line to read from the input stream. If you don't want the program to wait, then don't call readLiine().

Can you please help me how should i do it. Just a tip not the whole code.

Is your test client using `BufferedWriter`? If yes, make sure you invoke the `flush()` method on the BufferedWriter after you are done with writing the message(comid in your case).

Yes is using it.

To begin with, remove line 79. If that doesn't fix the problem, ask another question.

Yes thanks about that and i was thinking why is print it at the console :)
I just wanna know how i can read a line without block other calls

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.