J'ai un problème similaire et j'avais essayé la méthode de nraynaud mais le Thread attend une connexion sur le socket, et ca bloque donc jusqua ce que quelqu'un se connecte au socket
code :
Code :
- /**
- * Starts a new Thread for the server
- */
- public void run() {
- try{
- ss = new ServerSocket(port);
- while(ss!=null || !ss.isClosed()) {
- Socket s = ss.accept();
- new Request(s, ht);
- }
- }
- catch(SocketException e) {
- System.err.println("Serveur stoppé : Socket closed" );
- }
- catch(IOException e) {
- System.err.println(e);
- }
- }
-
- /**
- * Starts the server, it calls the method run() of this Runnable object
- */
- public synchronized void start() {
- if(!t.isAlive()) {
- //System.err.println("starting..." );
- t.start();
- }
- }
-
- /**
- * Stops the server
- */
- public synchronized void stop() {
- try {
- if(isRunning()) {
- ss.close();
- }
- }
- catch(IOException e) {
- System.err.println(e);
- }
- finally {
- ss = null;
- }
- }
-
-
- /**
- * Returns if the server is currently running
- * @return true if the server is running
- */
- public synchronized boolean isRunning() {
- return (ss != null && !ss.isClosed());
- }
|
Cette méthode marche à peu pres, mais tends à bugguer par moment.
Est-ce que quelq'un a une autre solution ?
Merci.