문제

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