import java.io.*; import java.nio.*; import java.net.*; import java.nio.channels.*; /** * * Classe Server ouvant une socket TCP et envoyant un flux de bytes au client qui s'y connecte */ public class StreamServer { /** Socket pour la mise en attente des connexions. */ ServerSocketChannel variableServerSocketChannel = ServerSocketChannel.open(); // ServerSocketChannel is an abstract class /** port par defaut d'ecoute */ final static int portByDefault = 1000; /** port effectif d'ecoute */ int portEffectif; /** Fichier a transferer */ static File fileSource; /** Capacite du buffer */ int MAX_PACKET_SIZE = 1000000; //********************************************************************************************************************** // CONSTRUCTEUR //********************************************************************************************************************** /** Constructeur, qui attache la socket a la channel, la socket au port, le file au fileInputStream et le fileChannel au fileInputStream */ public StreamServer(int portArg,String fileSrcArg) throws IOException { portEffectif = portArg; fileSource = new File(fileSrcArg); ServerSocket socket = variableServerSocketChannel.socket(); // necessary to manipulate the SocketChannel try {socket.bind(new InetSocketAddress(portEffectif));} //TCP connection listening on the port portEffectif catch(BindException ex){ System.err.println("port used by another processus"); System.exit(1); } } //************************************************************************************************************************ // METHODES //************************************************************************************************************************ /** Demarre le server. */ public void startingServer(){ System.out.println("Server TCP ecoutant sur le port : " + portEffectif); try { FileInputStream fileSrcInputStream = new FileInputStream(fileSource); // Allocation du tampon pour l'ecriture ByteBuffer byteB = ByteBuffer.allocate(MAX_PACKET_SIZE); FileChannel fileChannelsrc = fileSrcInputStream.getChannel(); System.out.println("En attente de nouvelles connections..."); SocketChannel sc = variableServerSocketChannel.accept();//Now connections are accepted ;blocking until one connection is established String scString = sc.toString(); //information on SocketChannel String subStringsc = scString.substring(72,scString.length()-1); //obtemir l'ip du client, ne marche pas tres bien System.out.println("Le client " + subStringsc + " vient de se connecter"); // while (byteB.hasRemaining()) { int start = byteB.remaining(); fileChannelsrc.read(byteB); //ecrit le contenu du fichier sur byteB int afterRead = byteB.remaining(); int difference = start - afterRead; System.out.println("Il a été ecrit " + difference + " bytes du fichier vers le buffer."); byteB.flip(); //position set to zero and limit is set to last element, If the mark is defined then it is discarded. ByteBuffer byteBufferTemp = ByteBuffer.allocate(1); //temp buffer used to receive from the client sc.read(byteBufferTemp); //wait the client to be ready sc.write(byteB); //envoi byteB sur la socket // } variableServerSocketChannel.close(); } catch (FileNotFoundException ex) {System.err.println("Error, file not accessible");} catch (IOException ex) { System.err.println("Probleme d'ecriture sur la socket"); System.exit(1);} // catch (InterruptedException iex){System.out.println("Interrupt Exception");} } //********************************************************************************************************************* // MAIN //********************************************************************************************************************* public static void main(String[] args) throws IOException{ if (args.length != 2) { System.err.println("usage : portServer,file "); System.exit(1); } int port = portByDefault; try { port = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Le numero de port doit etre un entier"); System.exit(1); } StreamServer myStreamServer = new StreamServer(port,args[1]); myStreamServer.startingServer(); } }