Reputation:
I would like to open a two ports connection using sockets on java but it wont work. I read here that I should use SocketChannel instead of socket but I would like to use getInput/outputStream() methods which is impossible with Socket channels. I thought of something like this:
public class ServeurMaitre {
public ServerSocket serverSocket = null;
int poolSize = 15;
Selector selector = null;
private ExecutorService pool = null;
boolean logged=false;
ServeurMaitre(int port1, int port2, int size){
try {
serverSocket = new ServerSocket(port1, size);
serverSocket = new ServerSocket(port2, 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));
}
}
}
}
with this main:
public class Main{
public static void main(String[] args) throws IOException {
ServeurMaitre serveur = new ServeurMaitre(8000, 3333, 1);
serveur.Initialisation();
serveur.ConnexionServeur();
}
}
This is what both slaves are doing:
public class EsclaveHTTP implements Runnable{
Socket socket = null;
ServeurMaitre sm = null;
public EsclaveHTTP(Socket cnx, ServeurMaitre serveurMaitre) {
this.socket=cnx;
this.sm=serveurMaitre;
}
@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
String message_distant = "";
try {
in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
message_distant = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(message_distant); //etc etc and at the end:
if(!socket.isClosed())
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and the second one
public class EsclaveXML implements Runnable{
Socket socket = null;
public EsclaveXML(Socket cnx, ServeurMaitre serveurMaitre) {
this.socket=cnx;
}
@Override
public void run() {
try {
BufferedReader input = new BufferedReader(
new InputStreamReader(socket.getInputStream(), "8859_1"), 1024);
StringBuffer sb = new StringBuffer();
InputStream ch = socket.getInputStream();
//System.out.println(ch);
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(ch, new ParseurXML());
}catch (DOMException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
catch(IOException e) {System.out.println(e);}
finally {
try { if(socket != null) socket.close();}
catch(IOException e) {}
}
}
}
But it would only accept one connexion.
How could I do some sort of a parallel connexion?
Upvotes: 0
Views: 41
Reputation: 1055
You are overwritting serverSocket variable
serverSocket = new ServerSocket(port1, size);
serverSocket = new ServerSocket(port2, size);
so you are only accepting connections for the second one. You need two variables, serverSocket1 and serverSocket2 and call accept() on both.
Upvotes: 2