rtsp客户端代码无法正常工作

时间:2014-01-21 14:14:16

标签: java sockets

我在Java套接字通信中遇到问题,我在一台机器上运行Live555 Media Server和一个小应用程序(一些类似于代理服务器代码,引用一个在线代码片段),还创建了一个客户端代码并运行在我的笔记本电脑。所有机器都在同一个网络中。当我从客户端向代理发送RTSP命令时,它正在正确接收,并且它正在将接收到的命令重定向到Live555服务器并且它返回正确的响应但是响应未在客户端中接收。帮助我解决这个问题,并建议我一些文档链接来了解问题。

这是我的代理代码,

public class TestProxyServer {

    public static void main(String[] args) {

    try {

        String host = "127.0.0.1";
        int serverPort = 8554;
        int proxyPort = 5555;

        System.out.println("Starting proxy for " + host + ":" + serverPort
                + " on port " + proxyPort);

        runServer(host, serverPort, proxyPort);
        } catch(Exception e) {
            e.printStackTrace();
        }       
    }

public static void runServer(String host, int sProt, int pPort) throws IOException {

    ServerSocket ss = new ServerSocket(pPort);

    final byte[] request = new byte[1024];
    byte[] replay = new byte[1024];

    while(true) {

        Socket server = null, client = null;

        try {

            client = ss.accept();

            final InputStream fromClient = client.getInputStream();
            final OutputStream toClient = client.getOutputStream();

            // for Live555
            try {
                server = new Socket(host, sProt);
            } catch(Exception e) {
                PrintWriter out = new PrintWriter(toClient);
                out.print("Proxy can not connect to Live555 on host " + host + " with the port " + sProt + "\n");
                out.flush();
                client.close();
                continue;
            }

            final InputStream fromServer = server.getInputStream();
            final OutputStream toServer = server.getOutputStream();

            Thread t = new Thread() {

                public void run() {

                    int bytesRead;

                    try {

                        String ClientMSG;
                        while((bytesRead = fromClient.read(request)) != -1) {
                            toServer.write(request, 0, bytesRead);
                            ClientMSG = new String(request, 0, bytesRead);
                            System.err.println("From Client : " + ClientMSG);
                            toServer.flush();
                        }

                    } catch(Exception e) {
                        try {toServer.close();} catch(IOException ioe){}
                    }                       
                }
            };

            t.start();

            int bytesRead;

            try {
                String toCMSG;
                while((bytesRead = fromServer.read(replay)) != -1) {

                    toClient.write(replay, 0, bytesRead);
                    toCMSG = new String(replay, 0, bytesRead);
                    System.err.println("Response from Live555 " + toCMSG);
                    toClient.flush();
                }

            } catch(Exception e) {
                toClient.close();
//                  e.printStackTrace();
            }

        } catch(Exception e) {
            e.printStackTrace();
        } finally {

            if(server != null)
                server.close();

            if(client != null)
                client.close();
        }
    }

}
}

和客户端代码在这里,

    public class Test {

public static void main(String[] args) {

    try {

        String host = "192.168.1.9";
        // int serverPort = 8554;
        int proxyPort = 5555;

        System.out.println("Starting Client to connect proxy on " + host + ":" + " with port " + proxyPort);

        runServer(host, proxyPort);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void runServer(String host, int sPort) throws IOException {

    final byte[] request = new byte[1024];
    final byte[] reply = new byte[1024];

    while (true) {

        Socket server = null;

        try {

            // for Proxy
            try {
                server = new Socket(host, sPort);
            } catch (Exception e) {
                System.out.println("Proxy can not connect to ProxyServer on host " + host + " with the port " + sPort + "\n");
                continue;
            }

            final InputStream fromServer = server.getInputStream();
            final OutputStream toServer = server.getOutputStream();

            int bytesRead;

            try {

                String RtspMSG = "DESCRIBE rtsp://192.168.1.9:8554/free.ts RTSP/1.0\r\nCSeq: 2\r\n\r\n";
                // while((bytesRead = fromClient.read(RtspMSG.getBytes()))
                // != -1) {
                toServer.write(RtspMSG.getBytes(), 0, RtspMSG.length());
                System.err.println("Sent : " + RtspMSG);
                toServer.flush();
                // }

            } catch (Exception e) {
                try {
                    toServer.close();
                } catch (IOException ioe) {
                }
            }

            Thread t = new Thread() {
                public void run() {

                    int bytesRead;

                    try {
                        String response = null;
                        System.err.println("----------------------------------");
                        while ((bytesRead = fromServer.read(reply)) != -1) {

                            response = new String(reply, 0, bytesRead);
                        }
                        System.err.println("Response from Proxy : "
                                + response);
                    } catch (Exception e) {
                        try {
                            fromServer.close();
                        } catch (Exception e1) {
                        }
                        // e.printStackTrace();
                    }
                }
            };
            t.start();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (server != null)
                server.close();
        }
    }

}
}

谢谢

0 个答案:

没有答案