Télcharger des fichiers avec les sockets-java

potter1111 Messages postés 4 Statut Membre -  
potter1111 Messages postés 4 Statut Membre -
Bonjour,

j'aimerais télécharger automatiquement des fichiers, dans le meme dossier, en utilisant les sockets,normalement c'est bon pour le code,mais il m'affiche un problème de synchronisation,je suis en train de regarder le code mais pourriez vous s'il vous plait voir avec moi s'il y a un quelconque problème?
je vous remercie de votre aide :)
coté client:
import java.io.*;
import java.net.*;

public class client {

public static void file_recive(String nom_file,BufferedReader in) throws IOException{

BufferedWriter rapport = new BufferedWriter (new FileWriter ("C:\\Users\\potter\\Desktop\\cours+devoirs\\p2a\\Orange Caraibe - LAMENTIN\\new\\20110307\\"+nom_file+".xls")) ;
String reponse=in.readLine();
while(reponse !=null)
{
if (reponse.equals("fin") )
break;
rapport.write(reponse+"\r\n");
rapport.flush();
reponse=in.readLine();
}

}


public static boolean existe(String nom_file) throws IOException{
BufferedReader fichier_rec = new BufferedReader(new FileReader("C:\\Users\\potter\\Desktop\\client_rec.txt"));
String f=fichier_rec.readLine();
boolean ok=false;
while (f!=null){

if(f.equals(nom_file+".xls")) { ok=true;
System.out.println(ok);}
f=fichier_rec.readLine();
}
return ok;
}

public static void main(String[] args) throws IOException
{
BufferedReader in;

String Nom_serveur = "127.0.0.1";
PrintStream out;
String destination=null;
String reponse;


String pathToExplore = "C:\\p2aclient";


DiskFileExplorer diskFileExplorer = new DiskFileExplorer(pathToExplore, false,"C:\\Users\\potter\\Desktop\\client_rec.txt");
Long start = System.currentTimeMillis();
diskFileExplorer.list();
System.out.println("----------");
System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses");
System.out.println(diskFileExplorer.dircount + " dossiers");
System.out.println(diskFileExplorer.filecount + " fichiers");


//BufferedReader an= new BufferedReader (new InputStreamReader (System.in));
BufferedWriter rapport1 = new BufferedWriter (new FileWriter ("C:\\Users\\potter\\Desktop\\liste_serv_recu.txt")) ;
try
{
Socket cl_sock = new Socket (Nom_serveur,8189);
in= new BufferedReader (new InputStreamReader (cl_sock.getInputStream()));
out = new PrintStream (cl_sock.getOutputStream ());


// authentification;
out.println("alcatel");
//System.out.println(in.readLine());

// reception nom file

String nom_file_a_rc=in.readLine();
//if (!nom_file_a_rc.equals("fin")){

while(nom_file_a_rc!=null)
{
if (nom_file_a_rc.equals("fin"))
break;
boolean r=client.existe(nom_file_a_rc);
if (!r){
out.println("ok");
client.file_recive(nom_file_a_rc,in);
//rapport1.write(nom_file_a_rc+"\r\n");
//rapport1.flush();
System.out.println("file :"+nom_file_a_rc+" recived");
}
else{
//nom_file_a_rc=in.readLine();
out.println("ko");
}

nom_file_a_rc=in.readLine();
}
cl_sock.close();
}


catch (Exception e)
{ e.printStackTrace();}
}
}


coté serveur:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class serveur {
ServerSocket Serveur;
Socket Connexion;
static PrintStream p;
BufferedReader br;
String rep=null;

public static void sendfile(String nom_fichier) throws IOException{

BufferedReader Rep = new BufferedReader(new FileReader("C:\\Users\\potter\\Desktop\\cours+devoirs\\p2a\\Orange Caraibe - LAMENTIN\\Orange Caraibe - LAMENTIN\\20110307"+nom_fichier));
String reponse=Rep.readLine();
while(reponse !=null)
{
p.println(reponse);
reponse=Rep.readLine();
}
p.println("fin");

}

public static void main(String[] args) throws IOException {
String nom_fichier=null;
String pathToExplore = ("C:\\Users\\potter\\Desktop\\cours+devoirs\\p2a\\Orange Caraibe - LAMENTIN\\Orange Caraibe - LAMENTIN\\20110307");
String fichier[]=new String[1000];
DiskFileExplorer diskFileExplorer = new DiskFileExplorer(pathToExplore, false,"C:\\Lyrics\\20110307");
long start = System.currentTimeMillis();
diskFileExplorer.list();
System.out.println("----------");
System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses");
System.out.println(diskFileExplorer.dircount + " dossiers");
System.out.println(diskFileExplorer.filecount + " fichiers");


///-----------------------------------
ServerSocket Serveur;
Socket Connexion;
//PrintStream p;
BufferedReader br;
String rep=null;
try
{
ServerSocket s = new ServerSocket(8189);
try
{
Connexion = s.accept();
p= new PrintStream (Connexion.getOutputStream());
br = new BufferedReader (new InputStreamReader (Connexion.getInputStream()));
// p.println("authentification??");
rep=br.readLine();
if (rep.equals("alcatel"))
{p.println("authentification reussite");


BufferedReader Rep1 = new BufferedReader(new FileReader("C:\\users\\potter\\Desktop\\liste_serv.txt"));
String reponse1=Rep1.readLine();
p.println(reponse1);
//reponse1=Rep1.readLine();
while((reponse1 !=null))
{
reponse1=Rep1.readLine();
rep=br.readLine();
if(rep.equals("ok")){
serveur.sendfile(reponse1);
System.out.println("file :"+ reponse1+ " send ok");
}
reponse1=Rep1.readLine();
if (!reponse1.equals(null)){
p.println(reponse1);
}
}
p.println("fin");

//

}

else {p.println("you're not authorisized");}
}
catch (IOException e){
s.close();
System.err.println(e);}
}
catch (IOException e)
{ System.err.println(e);}
}
}

classe DiskFileExplorer:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
* Lister le contenu d'un répertoire
* @author fobec 2010
*/
public class DiskFileExplorer {

private String initialpath = "";
private boolean recursivePath = false;
public int filecount = 0;
public int dircount = 0;
String nom_f;
/**
* Constructeur
* @param path chemin du répertoire
* @param b analyse des sous dossiers
*/
public DiskFileExplorer(String path, boolean b, String nom_f) {
super();
this.initialpath = path;
this.recursivePath = b;
this.nom_f=nom_f;
}

public void list() throws IOException {
this.listDirectory(this.initialpath);
}

private void listDirectory(String dir) throws IOException {
File file = new File(dir);
File[] files = file.listFiles();
BufferedWriter rapport;
rapport = new BufferedWriter (new FileWriter (nom_f));
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory() == true) {
System.out.println("Dossier" + files[i].getAbsolutePath());
this.dircount++;
} else {

try {

rapport.write(files[i].getName()+"\r\n");
rapport.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Fichier" + files[i].getName());
this.filecount++;
}
if (files[i].isDirectory() == true && this.recursivePath == true) {
this.listDirectory(files[i].getAbsolutePath());
}
}
}
}

}
A voir également:

1 réponse

potter1111 Messages postés 4 Statut Membre
 
pour l'instant,j'ai réussi à corriger les codes du client et serveur,maintenant je peux télécharger tous les fichiers quoiqu'il m'affiche un pb de synchro après l'envoi de tous les fichiers,quelqu'un peut m'aider svp?

serveur:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class serveur {
ServerSocket Serveur;
Socket Connexion;
static PrintStream p;
BufferedReader br;
String rep=null;

public static void sendfile(String nom_fichier) throws IOException{

BufferedReader Rep = new BufferedReader(new FileReader("C:\\p2aserveur\\"+nom_fichier));
String reponse=Rep.readLine();
while(reponse !=null)
{
p.println(reponse);
reponse=Rep.readLine();
}
p.println("fin");

}

public static void main(String[] args) throws IOException {
String nom_fichier=null;
String pathToExplore = ("C:\\p2aserveur");
String fichier[]=new String[1000];
DiskFileExplorer diskFileExplorer = new DiskFileExplorer(pathToExplore, false,"C:\\Lyrics\\liste_serv.txt");
long start = System.currentTimeMillis();
diskFileExplorer.list();
System.out.println("----------");
System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses");
System.out.println(diskFileExplorer.dircount + " dossiers");
System.out.println(diskFileExplorer.filecount + " fichiers");


///-----------------------------------
ServerSocket Serveur;
Socket Connexion;
//PrintStream p;
BufferedReader br;
String rep=null;
try
{
ServerSocket s = new ServerSocket(8189);
try
{
Connexion = s.accept();
p= new PrintStream (Connexion.getOutputStream());
br = new BufferedReader (new InputStreamReader (Connexion.getInputStream()));
// p.println("authentification??");
rep=br.readLine();
if (rep.equals("alcatel"))
{p.println("authentification reussite");


BufferedReader Rep1 = new BufferedReader(new FileReader("C:\\lyrics\\liste_serv.txt"));
String reponse1=Rep1.readLine();
p.println(reponse1);
//reponse1=Rep1.readLine();
while((reponse1 !=null))
{
//reponse1=Rep1.readLine();
rep=br.readLine();
if(rep.equals("ok")){
serveur.sendfile(reponse1);
System.out.println("file :"+ reponse1+ " send ok");
}
reponse1=Rep1.readLine();
//if (!reponse1.equals(null)){
p.println(reponse1);
//}
}
p.println("fin");

//

}

else {p.println("you're not authorisized");}
}
catch (IOException e){
s.close();
System.err.println(e);}
}
catch (IOException e)
{ System.err.println(e);}
}
}

client:


import java.io.*;
import java.net.*;

public class client {

public static void file_recive(String nom_file,BufferedReader in) throws IOException{

BufferedWriter rapport = new BufferedWriter (new FileWriter ("C:\\p2aclient\\"+nom_file+".xls")) ;
String reponse=in.readLine();
while(reponse !=null)
{
if (reponse.equals("fin") )
break;
rapport.write(reponse+"\r\n");
rapport.flush();
reponse=in.readLine();
}

}


public static boolean existe(String nom_file) throws IOException{
BufferedReader fichier_rec = new BufferedReader(new FileReader("C:\\Users\\potter\\Desktop\\client_rec.txt"));
String f=fichier_rec.readLine();
boolean ok=false;
while (f!=null){

if(f.equals(nom_file+".xls")) { ok=true;
System.out.println(ok);}
f=fichier_rec.readLine();
}
return ok;
}

public static void main(String[] args) throws IOException
{
BufferedReader in;

String Nom_serveur = "127.0.0.1";
PrintStream out;
String destination=null;
String reponse;


String pathToExplore = "C:\\p2aclient";


DiskFileExplorer diskFileExplorer = new DiskFileExplorer(pathToExplore, false,"C:\\lyrics\\client_rec.txt");
Long start = System.currentTimeMillis();
diskFileExplorer.list();
System.out.println("----------");
System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses");
System.out.println(diskFileExplorer.dircount + " dossiers");
System.out.println(diskFileExplorer.filecount + " fichiers");


//BufferedReader an= new BufferedReader (new InputStreamReader (System.in));
BufferedWriter rapport1 = new BufferedWriter (new FileWriter ("C:\\lyrics\\liste_serv_recu.txt")) ;
try
{
Socket cl_sock = new Socket (Nom_serveur,8189);
in= new BufferedReader (new InputStreamReader (cl_sock.getInputStream()));
out = new PrintStream (cl_sock.getOutputStream ());


// authentification;
out.println("alcatel");
System.out.println(in.readLine());

// reception nom file

String nom_file_a_rc=in.readLine();
//if (!nom_file_a_rc.equals("fin")){

while(nom_file_a_rc!=null)
{
if (nom_file_a_rc.equals("fin"))
break;
boolean r=client.existe(nom_file_a_rc);
if (!r){
out.println("ok");
client.file_recive(nom_file_a_rc,in);
rapport1.write(nom_file_a_rc+"\r\n");
rapport1.flush();
System.out.println("file :"+nom_file_a_rc+" recived");
}
else{
//nom_file_a_rc=in.readLine();
out.println("ko");
}

nom_file_a_rc=in.readLine();
}
cl_sock.close();
}


catch (Exception e)
{ e.printStackTrace();}
}
}
0