Streaming video to the server

I'm trying to stream a video to a server from another computer using Java and to start with, I'm just trying to stream a file using a raw socket connection and a byte stream. However, things don't appear the same way. If I transfer the file this way, the 3MB file on my computer ends up being 5MB on the server. I am trying to do this with a video file, and the resulting file actually "plays" when I load it and has the correct length, but no image. Code below:

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 (receiver):

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


    }



}

      

Any thoughts? Thanks in advance!

Chris

+2


source to share


1 answer


So I figured out the problem.

I changed the server to only write "count" bytes

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

      



And now it works :)

Thank!

+4


source







All Articles