Bonsoir à vous,
J'ai un code serveur qui permet d'envoyer un fichier vidéo mais j'aimerais que ce fichier s'envoi bit par bit (50 bits par 50 bits chaque seconde par exemple) et donc que le client reçoit aussi le fichier de la même façon.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class ServeurEssai { public final static int SOCKET_PORT = 13267; // public final static String FILE_TO_SEND = "fichier.avi"; public static void main (String [] args ) throws IOException { FileInputStream fis = null; BufferedInputStream bis = null; OutputStream os = null; ServerSocket servsock = null; Socket sock = null; try { servsock = new ServerSocket(SOCKET_PORT); while (true) { System.out.println("Waiting..."); try { sock = servsock.accept(); System.out.println("Accepted connection : " + sock); // send file File myFile = new File (FILE_TO_SEND); byte [] mybytearray = new byte [(int)myFile.length()]; fis = new FileInputStream(myFile); bis = new BufferedInputStream(fis); bis.read(mybytearray,0,mybytearray.length); os = sock.getOutputStream(); System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + "bytes)"); os.write(mybytearray,0,mybytearray.length); os.flush(); System.out.println("Done."); } finally { if (bis != null) bis.close(); if (os != null) os.close(); if (sock!=null) sock.close(); } } } finally { if (servsock != null) servsock.close(); } } }Merci à vous.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class Client { public final static int SOCKET_PORT = 13267; public final static String SERVER = "127.0.0.1"; public final static String FILE_TO_RECEIVED = "film.avi"; public final static int FILE_SIZE = 6022386; public static void main (String [] args ) throws IOException { int bytesRead; int current = 0; FileOutputStream fos = null; BufferedOutputStream bos = null; Socket sock = null; try { sock = new Socket(SERVER, SOCKET_PORT); System.out.println("Connecting..."); // receive file byte [] mybytearray = new byte [FILE_SIZE]; InputStream is = sock.getInputStream(); fos = new FileOutputStream(FILE_TO_RECEIVED); bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); bos.write(mybytearray, 0 , current); bos.flush(); System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)"); } finally { if (fos != null) fos.close(); if (bos != null) bos.close(); if (sock != null) sock.close(); } } }
Partager