Socket serveur client

cs_valour Messages postés 27 Date d'inscription vendredi 13 août 2004 Statut Membre Dernière intervention 8 décembre 2012 - 5 mai 2012 à 20:56
 Utilisateur anonyme - 29 mai 2012 à 12:22
Bonjour tout le monde j'envoi les fichiers sans probléme autant que client mais quand je clique sur annuler sur le jfilechooser les fichiers ne s'envoient plus et le serveur se déconect voici le code source du client et serveur

---------Client----------
public void Cliente()
{

int in;
byte[] byteArray;
File archivo = null;
String direccionArchivo = null;
String IP = "127.0.0.1";

try
{
// IP = JOptionPane.showInputDialog(null,"Entrez Lip du pc distant","Cliente",JOptionPane.CANCEL_OPTION);
Socket via = new Socket(IP, 1234);
if(via.isConnected())
{
JOptionPane.showMessageDialog(null, "Connecté au Serveur: " + IP);


JFileChooser selector = new JFileChooser();
selector.setDialogTitle("Selecciona el archivo a enviar");
int resultado = selector.showOpenDialog(null);
if (resultado == JFileChooser.APPROVE_OPTION)
{
archivo = selector.getSelectedFile();
direccionArchivo = archivo.getPath().toString();



BufferedInputStream bis = new BufferedInputStream(new FileInputStream(direccionArchivo));
BufferedOutputStream bos = new BufferedOutputStream(via.getOutputStream());

//Enviamos el nombre del archivo
DataOutputStream dos = new DataOutputStream(via.getOutputStream());
dos.writeUTF(archivo.getName());


byteArray = new byte[8192];
while ((in = bis.read(byteArray)) != -1)
{
bos.write(byteArray,0,in);
}
bis.close();
bos.close();

JOptionPane.showMessageDialog(null, "Le Fichier " + archivo.getName().toString() +
" a bien été envoyé");
String oups=archivo.getName();
String nom=("/IREG/");
String gogle="//"+IP+nom+"""+oups+""";

jTextField6.setText(pik);
System.out.println(gogle);
if (resultado == JFileChooser.CANCEL_OPTION)
{dispose();
}
}
}


}catch (Exception e) {JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);}

}

---Serveur----





import java.net.*;
import java.io.*;
import javax.swing.*;

class serveur
{

public String user = System.getProperty("user.name");
public static String nom = System.getProperty("user.home");
public static void main (String[] args)
{
ServerSocket server;
Socket connection = null;

DataOutputStream output;
BufferedInputStream bis;
BufferedOutputStream bos;

byte[] receivedData;
int in;
String file;

try{
server = new ServerSocket(1234);
while ( true ) {
connection = server.accept();

receivedData = new byte[8192];

bis = new BufferedInputStream(connection.getInputStream());
DataInputStream dis = new DataInputStream(connection.getInputStream());

//recibimos el nombre del fichero
file = dis.readUTF();
file = file.substring(file.indexOf('\\')+1,file.length());

//aqui se escribe el archivo en la ubicacion donde este el Servidor.jar
bos = new BufferedOutputStream(new FileOutputStream("C:\\Fichier\"+ file));

while ((in = bis.read(receivedData)) != -1){
bos.write(receivedData,0,in);
}
bos.close();
dis.close();

JOptionPane.showMessageDialog(null, "Fichier (" + file.toString() + ") Reçu");
// System.exit(0);
}
}catch (Exception e ) {
JOptionPane.showMessageDialog(null,e, "Errror",JOptionPane.ERROR_MESSAGE);
}
}
}


Merci.

7 réponses

Utilisateur anonyme
5 mai 2012 à 22:07
Salut.

Utilise stp les balises d'affichage du code la prochaine fois.
Quel est le problème exactement ? Que ton serveur se déconnecte alors qu'il ne devrait pas ?


--
Pylouq
(Lire le Réglement n'a jamais tué personne, au pire ça a instruit des gens.)
0
cs_valour Messages postés 27 Date d'inscription vendredi 13 août 2004 Statut Membre Dernière intervention 8 décembre 2012
5 mai 2012 à 22:22
Bonsoir le probléme c'est que quand je me connect au serveur le jfilechooser souvre chez moi pour choisir le fichier a envoyer mais si par exemple je clique sur annuler du jfilechooser aprés si je veux envoyé un autre fichier je me connect au serveur mais le fichier ne par plus.
0
Utilisateur anonyme
5 mai 2012 à 22:45
Ton code n'est pas très lisible, reposte-le en utilisant les balises d'affichage de code.
Le fichier ne part plus même en réutilisant un JFileChooser ?


--
Pylouq
(Lire le Réglement n'a jamais tué personne, au pire ça a instruit des gens.)
0
cs_valour Messages postés 27 Date d'inscription vendredi 13 août 2004 Statut Membre Dernière intervention 8 décembre 2012
6 mai 2012 à 00:21
oui voilà il ne part plus méme en utilisant jfilechooser jai pas compri comment faire pour les balises d'affichage du code
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
6 mai 2012 à 15:55
Salut,

Je repostes juste le code initial avec les balises:

Client:
public void Cliente() 
{ 

  int in; 
  byte[] byteArray; 
  File archivo = null; 
  String direccionArchivo = null; 
  String IP = "127.0.0.1"; 

  try 
  { 
    // IP = JOptionPane.showInputDialog(null,"Entrez Lip du pc distant","Cliente",JOptionPane.CANCEL_OPTION);
    Socket via = new Socket(IP, 1234); 
    if(via.isConnected()) 
    { 
      JOptionPane.showMessageDialog(null, "Connecté au Serveur: " + IP); 


      JFileChooser selector = new JFileChooser(); 
      selector.setDialogTitle("Selecciona el archivo a enviar"); 
      int resultado = selector.showOpenDialog(null); 
      if (resultado == JFileChooser.APPROVE_OPTION) 
      { 
        archivo = selector.getSelectedFile(); 
        direccionArchivo = archivo.getPath().toString(); 



        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(direccionArchivo)); 
        BufferedOutputStream bos = new BufferedOutputStream(via.getOutputStream()); 

        //Enviamos el nombre del archivo 
        DataOutputStream dos = new DataOutputStream(via.getOutputStream()); 
        dos.writeUTF(archivo.getName()); 


        byteArray = new byte[8192]; 
        while ((in = bis.read(byteArray)) != -1) 
        { 
          bos.write(byteArray,0,in); 
        } 
        bis.close(); 
        bos.close(); 

        JOptionPane.showMessageDialog(null, "Le Fichier " + archivo.getName().toString() + 
" a bien été envoyé"); 
        String oups=archivo.getName(); 
        String nom=("/IREG/"); 
        String gogle="//"+IP+nom+"""+oups+"""; 

        jTextField6.setText(pik); 
        System.out.println(gogle); 
        if (resultado == JFileChooser.CANCEL_OPTION) 
        {
          dispose(); 
        } 
      } 
    } 


  }catch (Exception e) 
  {
    JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
  }  
}



Serveur:
import java.net.*; 
import java.io.*; 
import javax.swing.*; 

class serveur 
{ 

  public String user = System.getProperty("user.name"); 
  public static String nom = System.getProperty("user.home"); 
  public static void main (String[] args) 
  { 
    ServerSocket server; 
    Socket connection = null; 

    DataOutputStream output; 
    BufferedInputStream bis; 
    BufferedOutputStream bos; 

    byte[] receivedData; 
    int in; 
    String file; 

    try{ 
      server = new ServerSocket(1234); 
      while ( true ) { 
        connection = server.accept(); 

        receivedData = new byte[8192]; 

        bis = new BufferedInputStream(connection.getInputStream()); 
        DataInputStream dis = new DataInputStream(connection.getInputStream()); 

        //recibimos el nombre del fichero 
        file = dis.readUTF(); 
        file = file.substring(file.indexOf('\\')+1,file.length()); 

        //aqui se escribe el archivo en la ubicacion donde este el Servidor.jar 
        bos = new BufferedOutputStream(new FileOutputStream("C:\\Fichier\"+ file)); 

        while ((in = bis.read(receivedData)) != -1){ 
          bos.write(receivedData,0,in); 
        } 
        bos.close(); 
        dis.close(); 

        JOptionPane.showMessageDialog(null, "Fichier (" + file.toString() + ") Reçu"); 
        // System.exit(0); 
      } 
    }catch (Exception e ) { 
      JOptionPane.showMessageDialog(null,e, "Errror",JOptionPane.ERROR_MESSAGE); 
    } 
  } 
} 


-----

"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
cs_valour Messages postés 27 Date d'inscription vendredi 13 août 2004 Statut Membre Dernière intervention 8 décembre 2012
6 mai 2012 à 18:11
salut Twinuts merci beaucoup
0
Utilisateur anonyme
29 mai 2012 à 12:22
Salut,

Où en es-tu sur ton problème ? As-tu pu avancer ou es-tu toujours bloqué ?

--
Pylouq
(Lire le Réglement n'a jamais tué personne, au pire ça a instruit des gens.)
0
Rejoignez-nous