我有两个简单的程序,一个是服务器,一个是客户端,都在本地主机上。我想做的是通过套接字将视频从服务器流式传输到客户端,并且客户端可以使用套接字的文件描述符来播放它。首先,我尝试发送一些消息,客户端可以接收它。之后,我从服务器的SD卡中发送了几个字节的视频到客户端。客户端可以接收这些字节,但无法播放它。有人知道如何解决这个问题吗?
下面是我的服务器和客户端代码片段:
服务器:
下面是我的服务器和客户端代码片段:
服务器:
//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");
客户端:
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();