Télcharger des fichiers avec les sockets-java
Fermé
potter1111
Messages postés
4
Date d'inscription
mardi 19 avril 2011
Statut
Membre
Dernière intervention
30 mai 2012
-
19 avril 2011 à 23:06
potter1111 Messages postés 4 Date d'inscription mardi 19 avril 2011 Statut Membre Dernière intervention 30 mai 2012 - 20 avril 2011 à 01:03
potter1111 Messages postés 4 Date d'inscription mardi 19 avril 2011 Statut Membre Dernière intervention 30 mai 2012 - 20 avril 2011 à 01:03
A voir également:
- Télcharger des fichiers avec les sockets-java
- Wetransfer gratuit fichiers lourd - Guide
- Téléchargez cette archive (dossier compressé). en extraire tous les fichiers dans un dossier local. quel fichier contient l’expression trouverpix ? ✓ - Forum Windows
- Jeux java itel ✓ - Forum Jeux vidéo
- Telecharger java - Télécharger - Langages
- Java apk - Télécharger - Langages
1 réponse
potter1111
Messages postés
4
Date d'inscription
mardi 19 avril 2011
Statut
Membre
Dernière intervention
30 mai 2012
20 avril 2011 à 01:03
20 avril 2011 à 01:03
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();}
}
}
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();}
}
}