Hola,

el siguiente código es para transferir archivos a través de Socket.

Código:
Enviar el archivo:

Código: Seleccionar todo

public static void escribir(byte[] command) {
        try {
            dos.writeInt(command.length);
            if (command.length > 0) {
                dos.write(command, 0, command.length);
            }
        } catch (Exception ex) {
            System.out.println("[escribir] Hubo un error: "+ex.getMessage());
        }
    }
Recibir el archivo

Código: Seleccionar todo

public static byte[] leer() {
        byte[] r = null;
        try {
            int len = dis.readInt();
            r = new byte[len];
            if (len > 0) {
                dis.readFully(r);
            }
        } catch (Exception ex) {
            System.out.println("[leer] Hubo un error: " + ex.getMessage());
        }
        return r;
    }
Uso:
Enviar archivo:

Código: Seleccionar todo

public class SocketServer {
    private static Socket sock;
    private static DataOutputStream dos;
    
    public static void main(String[] args) {
        try {
            ServerSocket srvSock = new ServerSocket(1234);
            sock = srvSock.accept();
            dos = new DataOutputStream(sock.getOutputStream());
            
            long comienzo = System.currentTimeMillis();
            Path path = Paths.get("archivo.exe");
            byte[] data = Files.readAllBytes(path);
            escribir(data);
            long fin = System.currentTimeMillis();
            
            System.out.println("Enviados "+data.length+" bytes en "+(fin-comienzo)+" ms");
        } catch (Exception ex) {
            System.out.println("[main] Hubo un error: "+ex.getMessage());
        }
    }
    
    public static void escribir(byte[] command) {
        try {
            dos.writeInt(command.length);
            if (command.length > 0) {
                dos.write(command, 0, command.length);
            }
        } catch (Exception ex) {
            System.out.println("[escribir] Hubo un error: "+ex.getMessage());
        }
    }
}
Recibir el archivo:

Código: Seleccionar todo

public class SocketClient {

    private static Socket sock;
    private static DataInputStream dis;

    public static void main(String[] args) {
        try {
            sock = new Socket("localhost", 1234);
            dis = new DataInputStream(sock.getInputStream());
            
            long comienzo = System.currentTimeMillis();
            byte[] archivo = leer();
            try (FileOutputStream fos = new FileOutputStream("archivo.exe")) {
                fos.write(archivo);
            }
            long fin = System.currentTimeMillis();
            
            System.out.println("Recibidos "+archivo.length+" bytes en "+(fin-comienzo)+" ms");
        } catch (Exception ex) {
            System.out.println("[main] Hubo un error: " + ex.getMessage());
        }

    }

    public static byte[] leer() {
        byte[] r = null;
        try {
            int len = dis.readInt();
            r = new byte[len];
            if (len > 0) {
                dis.readFully(r);
            }
        } catch (Exception ex) {
            System.out.println("[leer] Hubo un error: " + ex.getMessage());
        }
        return r;
    }
}
Imágenes:
Imagen
Imagen
Responder

Volver a “Fuentes”