Advertisement
sindibadthesailor

Untitled

Mar 24th, 2023
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package serveur_mt;
  2.  
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6.  
  7. public class Serveur {
  8.     public final static int DEFAULT_PORT = 6789;
  9.     protected static int port;
  10.    
  11.  
  12.     public static void main(String[] args) {
  13.         ServerSocket listen_socket=null;
  14.         if(args.length>0) {
  15.             try {
  16.             port=Integer.parseInt(args[0]);
  17.             }catch(NumberFormatException nfe) {
  18.                 usage();
  19.                 System.out.println(nfe);
  20.                 System.exit(-1);
  21.             }
  22.            
  23.         }else {
  24.             port=DEFAULT_PORT;
  25.         }
  26.        
  27.     //création de la socket d'écoute du serveur
  28.         try {
  29.             listen_socket=new ServerSocket(port);
  30.            
  31.         } catch (IOException e) {
  32.             // TODO Auto-generated catch block
  33.             e.printStackTrace();
  34.         }
  35.         //boucle infinie du serveur
  36.         //on attend des connexions clientes
  37.         while(true) {
  38.            
  39.             try {
  40.                 Socket client=listen_socket.accept();
  41.                 Service s=new Service(client);
  42.                
  43.             } catch (IOException e) {
  44.                 // TODO Auto-generated catch block
  45.                 e.printStackTrace();
  46.             }
  47.            
  48.         }
  49.        
  50.  
  51.     }
  52.  
  53.     private static void usage() {
  54.         System.out.println("Syntaxe: java Serveur [numéro de port]");
  55.        
  56.     }
  57.  
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement