質問

Javaを使用して別のマシンからサーバーにビデオをストリーミングしようとしています。まず、生のソケット接続とバイトストリームを使用してファイルを転送しようとしています。しかし、物事は同じように現れていません。この方法でファイルを転送すると、マシン上の3MBファイルはサーバー上で5MBになります。私はビデオファイルでこれを試していますが、結果のファイルは実際に「再生」します私がそれをダウンロードし、正しい長さであるが、画像がない場合コードは以下のとおりです。

クライアント(ストリーマー):

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();
    }
}

サーバー(受信者):

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();
        }


    }



}

考えはありますか?事前に感謝します!

クリス

役に立ちましたか?

解決

それで問題を見つけました。

" count"のみを書き込むようにサーバーを変更しました。バイト

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

そして、今では動作します:)

ありがとう!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top