0

i wanted to create a java server and client in a same file...because this is my structure of project

SERVER ---> CLIENT/SERVER ----> CLIENT

the coding for the SERVER and CLIENT part is quite simple but i have a problem when creating for CLIENT/SERVER part.. where my code can only run CLIENT part and not starting the SERVER part. because of my thread run() code.

package com.main.datadistributor;

    import java.io.IOException;
    import java.net.ServerSocket;

    public class Slave {
        public static void main(String args[]) throws IOException{
        Config cfg = new Config("Slave");
        String MasterServerIP = cfg.getProperty("MasterServerIP");
        String MasterServerPort = cfg.getProperty("MasterServerPort");
        String SlaveServerPort = cfg.getProperty("SlaveServerPort");

        Client client = new Client(MasterServerIP,Integer.parseInt(MasterServerPort),"SLAVEONE");
        client.run();

        int numClient = 0;
        ServerSocket listener = new ServerSocket(Integer.parseInt(SlaveServerPort));
        System.out.println("Server starts running");

        try{
            while(true){
                new Server(listener.accept(), numClient++, Integer.parseInt(SlaveServerPort), "SLAVESERVER").start();
            }
        } finally {
            listener.close();
        }


    }
}

from the code above i have problem only executing client.run() and the code just stop there without proceeding to execute new Server below at the try section

2

2 Answers 2

2

If Client subclasses Thread, then you need to call start(), not run(). If it implements Runnable then you need to instantiate a Thread to execute it and call the thread's start() method.

Sign up to request clarification or add additional context in comments.

Comments

1

Not knowing what Client is, or seeing its code limits our ability to help. If it has a run() method it may implement Runnable, so you can possibly just call new Thread(client).start() instead of calling its run method directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.