I have two simple programs, a server and a client, both on localhost. What I want to do is stream video from the server to the client via a socket, and the client can play it using the filediscriptor socket. First I try to send a message and the client can receive it. After that, I send the client a few bytes of video from the server card. The client can receive these bytes, but cannot reproduce it. Does anyone know how to solve a problem?
Here are my snippets of server and client code:
Server:
//Receive request from client. Socket client=serversocket.accept(); System.out.println("accept"); //Receive client message. BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); String str=in.readLine(); System.out.println("read:"+str); //Send message to client. //PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true); //out.println("server message"); FileInputStream fis=new FileInputStream("/sdcard/toystory3.3gp"); byte buffer[]=new byte[2000]; fis.read(buffer,0,20); DataOutputStream out=new DataOutputStream(client.getOutputStream()); out.write(buffer,0,20); in.close(); out.close(); client.close(); System.out.println("close");
Client:
Socket socket=new Socket("127.0.0.1",4444); String message="Initial"+"\r\n"; //Send message to server. PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); out.println(message); //Receive message from server. BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream())); String msg=br.readLine(); //ParcelFileDescriptor pfd=ParcelFileDescriptor.fromSocket(socket); //MediaPlayer m=new MediaPlayer(); //m.setDataSource(pfd.getFileDescriptor()); //m.prepare(); //m.start(); if(msg!=null) { System.out.println("Data received."); System.out.println(msg); } else { System.out.println("Data not received."); } out.close(); br.close(); socket.close();
android
charlie
source share