I have two simple programmes, server and client, both at localhost. What I want to do is to streaming the video from the server to the client through socket and the client can play it by using filediscriptor of the socket. First I try to send some message and the client can receive it. After that I send a few bytes of the video from the server'sd card to the client. The client can receive those bytes but cannot play it. Anyone know how to solve the problem?
Here is my server and client code snippets:
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();