Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2413 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Server multithread java socket

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Server multithread java socket

n°2142161
poolman123​4
Posté le 13-05-2012 à 12:28:56  profilanswer
 

Bonjour à tous j'ai un probléme c'est que quand il ya un seul client le serveur marche trés bien jenvoi des fichier, une fois qu'il ya un autre client qui s'ajoute le nouveau client envoi des fichiers mais il n'arrivent pas. Voici mon code java merci.
 
Serveur
 

Code :
  1. public class server implements Runnable {
  2.     Socket socket;
  3. server(Socket csocket) {
  4.         this.socket = csocket;
  5.     }
  6.     public static void main(String args[]) throws Exception {
  7.         ServerSocket ssock = new ServerSocket(1234);
  8.         System.out.println("Listening" );
  9.         int a = 0;
  10.         while (true) {
  11.             Socket sock = ssock.accept();
  12.             a++;
  13.             InetAddress addr = sock.getInetAddress();
  14.             System.out.println("Connection made to " + addr.getHostName() + " (" + addr.getHostAddress() + " ) ID: " + a);
  15.             new Thread(new bokal(sock)).start();
  16.         }
  17.     }
  18.     public void run() {
  19.         try {
  20.             InputStream in =socket.getInputStream();
  21.             DataInputStream dis = new DataInputStream(socket.getInputStream());
  22.             String file = dis.readUTF();
  23.             String file1 = dis.readUTF();
  24.             String file2 = dis.readUTF();
  25.             int i=dis.readInt();
  26.             if(i==1)
  27.             {
  28.             File directorio = new File("c:\\dossier\\"+file1+"\\"+file2+"\\" );
  29.             directorio.mkdirs();
  30.             }
  31.             if(file != null)
  32.             {
  33.                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\dossier\\"+file1+"\\"+file2+"\\"+ file));
  34.                 byte buf[] = new byte[1024];
  35.                 int len;
  36.                 while((len = in.read(buf)) > 0)
  37.                 {
  38.                     bos.write(buf, 0, len);
  39.                 }
  40.               in.close();
  41.           //    bos.flush();
  42.              
  43.             bos.close();
  44.             }
  45.        
  46.             socket.close();
  47.         } catch (IOException e) {
  48.             System.out.println(e);
  49.         }
  50.     }
  51. }


 
Client
 
 

Code :
  1. public  void Client()
  2. {
  3.  
  4.       int in;
  5.       byte[] byteArray;
  6.       File archivo = null;
  7.       String direccionArchivo = null;
  8.       String IP = null;
  9.       int i=0;
  10.       try
  11.       {
  12.           IP = JOptionPane.showInputDialog(null,"Entrez Lip du pc distant","Cliente",JOptionPane.CANCEL_OPTION);
  13.           Socket via = new Socket(IP, 1234);
  14.           if(via.isConnected())
  15.           {
  16.             JOptionPane.showMessageDialog(null, "Conectado al servidor: " + IP);
  17.        
  18.             JFileChooser selector = new JFileChooser();
  19.             selector.setDialogTitle("Selecciona el archivo a enviar" );
  20.             int resultado = selector.showOpenDialog(null);
  21.             if (resultado == JFileChooser.APPROVE_OPTION)
  22.             {
  23.                 archivo = selector.getSelectedFile();
  24.                 direccionArchivo = archivo.getPath().toString();
  25.             }else{
  26.          
  27.              via.close();
  28.            
  29.             }
  30.             BufferedInputStream bis = new BufferedInputStream(new FileInputStream(direccionArchivo));
  31.             BufferedOutputStream bos = new BufferedOutputStream(via.getOutputStream());
  32.             //Enviamos el nombre del archivo
  33.             DataOutputStream dos = new DataOutputStream(via.getOutputStream());
  34.             dos.writeUTF(archivo.getName());
  35.             dos.writeUTF(s);
  36.             dos.writeUTF(s1);
  37.             dos.writeInt(1);
  38.             dos.writeInt(2);
  39.            
  40.             byteArray = new byte[8192];
  41.             while ((in = bis.read(byteArray)) != -1)
  42.             {
  43.                 bos.write(byteArray,0,in);
  44.             }
  45.  via.close();       
  46.  bis.close();
  47. bos.close();
  48.      
  49.         }
  50.       }catch (Exception e) {//JOptionPane.showMessageDialog(null,e,"Error",JOptionPane.ERROR_MESSAGE);
  51.        }
  52.       }


 
 
Merci à tous.

mood
Publicité
Posté le 13-05-2012 à 12:28:56  profilanswer
 

n°2142190
Profil sup​primé
Posté le 13-05-2012 à 16:56:29  answer
 

balise "code=java".


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Server multithread java socket

 

Sujets relatifs
récupérer une image sur un socket et l'afficher en plein écran[Java] Coder le Jeux Pengo/Packman
[Java]- Windows installer, what else?Comment se connecter à la base de donnée windows sql server
Créer une base de données oracle pour Java[JAVA] Arbre lexicographique, jeu Pendu
Compilateur C++ en javaLANCER C++ A PARTIR DE JAVA
[Résolu] SQL Server / Procédure stockée avec paramètres optionnelsun script pour vérifier un code java avant de faire commit
Plus de sujets relatifs à : Server multithread java socket


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR