Advertisement
cesarcardinale

M4102C - TP4 Client

Mar 28th, 2019
1,250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. import java.net.Socket;
  3.  
  4. public class Client {
  5.     public static void main(String argv[]){
  6.         try {
  7.             String sentence;
  8.  
  9.             Socket clientSocket = new Socket("localhost", 2000);
  10.  
  11.             String fileName = "test.txt";
  12.             OutputStream outServeur = clientSocket.getOutputStream();
  13.             outServeur.write((fileName+"\n").getBytes(), 0, fileName.length()+1);
  14.  
  15.             InputStream inServeur = clientSocket.getInputStream();
  16.             FileOutputStream fos = new FileOutputStream(fileName);
  17.             int nbOctets;
  18.             byte buffer[] = new byte[1000];
  19.             while ((nbOctets = inServeur.read(buffer)) != -1) {
  20.                 String[] parts = new String(buffer).split("[END]", 2);
  21.                 System.out.println("Fichiers téléchargeables :");
  22.                 System.out.println(parts[0].substring(0,parts[0].length()-1));
  23.                 fos.write(parts[1].substring(4).getBytes(), 0, nbOctets);
  24.             }
  25.             fos.flush();
  26.             fos.close();
  27.  
  28.             outServeur.close();
  29.             inServeur.close();
  30.             clientSocket.close();
  31.         } catch (IOException e) {
  32.             // TODO Auto-generated catch block
  33.             e.printStackTrace();
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement