VLC rc接口套接字 - 连接被拒绝

时间:2017-10-20 12:56:05

标签: c# sockets vlc

我正在尝试在c#中为VLC编写一个简单的远程控制接口。 libVLC周围的.net包装似乎有点过时,但是another post详细说明了如何通过套接字连接控制vlc(我已经在下面包含了我的代码,此时几乎完全从其他帖子中删除了)

internal class Program
{
    private static void Main()
    {
        var socketAddress = new IPEndPoint(IPAddress.Loopback, 35913);
        var vlcServerProcess = Process.Start(@"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe", $"-I rc --rc-host {socketAddress}");

        try
        {
            var vlcRcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            vlcRcSocket.Connect(socketAddress);
            // start another thread to look for responses and display them
            Task.Factory.StartNew(() => Receive(vlcRcSocket));

            Console.WriteLine("Connected. Enter VLC commands.");

            while (true)
            {
                string command = Console.ReadLine();
                if (command.Equals("quit")) break;
                Send(vlcRcSocket, command);
            }

            Send(vlcRcSocket, "quit"); // close vlc rc interface and disconnect
            vlcRcSocket.Dispose();
        }
        finally
        {
            vlcServerProcess.Kill();
        }
    }

    private static void Send(Socket socket, string command)
    {
        // send command to vlc socket, note \n is important
        byte[] commandData = Encoding.UTF8.GetBytes(String.Format("{0}\n", command));
        int sent = socket.Send(commandData);
    }

    private static void Receive(Socket socket)
    {
        do
        {
            if (socket.Connected == false)
                break;
            // check if there is any data
            bool haveData = socket.Poll(1000000, SelectMode.SelectRead);

            if (haveData == false) continue;
            byte[] buffer = new byte[socket.ReceiveBufferSize];
            using (var mem = new MemoryStream())
            {
                while (haveData)
                {
                    int received = socket.Receive(buffer);
                    mem.Write(buffer, 0, received);
                    haveData = socket.Poll(1000000, SelectMode.SelectRead);
                }

                Console.WriteLine(Encoding.UTF8.GetString(mem.ToArray()));
            }

        } while (true);
    }
}

但是,我的应用程序总是在行vlcRcSocket.Connect(socketAddress);上引发异常,抛出的异常是

  

System.Net.Internals.SocketExceptionFactory.ExtendedSocketException:   '由于目标机器是主动的,因此无法建立连接   拒绝了它'

我尝试过不同的端口号,甚至禁用我的防火墙,但它似乎没有帮助。我可以看到VLC的实例在任务管理器中运行,但我看不到netstat中的绑定,所以我不确定VLC是否在监听?我做错了,还是有更好的方法来控制VLC的实例? (我只想要一些基本的控制,启动,停止,跳过和音量)

0 个答案:

没有答案