Frage

Ich versuche, von einer anderen Maschine mit Java und für den Anfang Video an einen Server zu streamen, ich versuche nur, eine Datei zu übertragen eine Raw-Socket-Verbindung und einen Byte-Strom verwendet wird. Allerdings sind die Dinge zeigen, die nicht die gleiche Art und Weise nach oben. Wenn ich eine Datei auf diese Weise, eine 3MB Datei auf meinem Rechner übertragen endet als 5 MB auf dem Server auf. Ich versuche, dies mit einer Video-Datei und die resultierenden Datei tatsächlich „spielt“, wenn ich es herunterladen und ist die richtige Länge, aber es gibt kein Bild. Code ist unter:

Client (Streamer):

public static void main(String[] args){
    Socket sock = null;

    try{
        System.out.println("Connecting...");
        sock = new Socket("server.com", 8080);
        InputStream is = new FileInputStream(new File("Bear.wmv"));
        byte[] bytes = new byte[1024];

        OutputStream stream = sock.getOutputStream();

        int count = is.read(bytes, 0, 1024);
        while (count != -1){
            stream.write(bytes, 0, 1024);
            count = is.read(bytes, 0, 1024);
        }

        is.close();
        stream.close();
        sock.close();

    }catch (Exception e){
        e.printStackTrace();
    }
}

Server (Empfänger):

public static void main(String[] args){
    ServerSocket sock = null;
    try {
        sock = new ServerSocket(8080);
    } catch (IOException e) {
        System.out.println("Could not instantiate socket:");
        e.printStackTrace();
        return;
    }

    Socket clientSock = null;
    while(true){

        try{

            System.out.println("Waiting for connection...");
            clientSock = sock.accept();
            final Socket fin = clientSock;
            System.out.println("Connection accepted");
            System.out.println("Spawning thread...");
            Thread trd = new Thread(new Runnable(){
                public void run(){
                    try {
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        System.out.println("Receiving video...");
                        File video = new File("test.wmv");
                        FileOutputStream fos = new FileOutputStream(video);
                        byte[] data = new byte[1024];
                        int count = fin.getInputStream().read(data, 0, 1024);
                        while (count != -1){
                            fos.write(data, 0, 1024);
                            count = fin.getInputStream().read(data, 0, 1024);
                        }
                        fos.close();
                        fin.close();
                        System.out.println("Done receiving");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }}
            });
            trd.start();

        }catch(IOException e){
            System.out.println("Could not accept");
            e.printStackTrace();
        }


    }



}

Alle Gedanken? Vielen Dank im Voraus!

Chris

War es hilfreich?

Lösung

Also habe ich das Problem aus.

Ich änderte der Server nur "count" schreiben Bytes

while (count != -1){
       fos.write(data, 0, count);
       count = fin.getInputStream().read(data, 0, 1024);
}

Und jetzt funktioniert es:)

Danke!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top