user8143344
user8143344

Reputation:

Open two ports connection with one server on java

I would like to launch a server who listens simultaneously to two different ports and with a different treatment to incoming connections (I launch a differnet slave depending on port). I did something like this:

public class ServeurMaitre {
public static ServerSocket serverSocket = null;
int poolSize = 15;
private ExecutorService pool = null;
ServeurMaitre(int port, int size){
    try {
        serverSocket = new ServerSocket(port, size);
        pool = Executors.newFixedThreadPool(poolSize);
        System.out.println("Serveur en marche. En attente des clients");

    } catch (IOException ex) {
        Logger.getLogger(ServeurMaitre.class.getName()).log(Level.SEVERE, null, ex);
    }
}

void ConnexionServeur() throws IOException {
    while(true) {               
        Socket cnx = serverSocket.accept();
        if (cnx.getLocalPort()==3333) {
            pool.execute(new EsclaveXML(cnx, this));
        }
        if(cnx.getLocalPort()==8000) {
            pool.execute(new EsclaveHTTP(cnx, this));
        }           
    }
}

public class Main{
public static void main(String[] args) throws IOException {
    ServeurMaitre serveur = new ServeurMaitre(8000, 1);
    ServeurMaitre serveur1 = new ServeurMaitre(3333, 1);
    serveur.Initialisation();
    serveur.ConnexionServeur();
    serveur1.ConnexionServeur();

}}

Problem: connexions that arrive on port 3333 are well treated but those who are on 8000 don't. Any help please? Thank you.

Upvotes: 0

Views: 97

Answers (1)

chrome
chrome

Reputation: 659

I think, cause of the problem is "static serverSocket" variable.

You can change this line
public static ServerSocket serverSocket = null;

to

public ServerSocket serverSocket = null;

Upvotes: 1

Related Questions