libimobiledevice-win32 / imobiledevice-net

.NET (C#, VB.NET,...) bindings for libimobiledevice
GNU Lesser General Public License v2.1
297 stars 77 forks source link

Trying to stream data from phone to PC, getting constant Connection Reset #163

Closed TerabyteTim closed 3 years ago

TerabyteTim commented 3 years ago

I am using the latest imobiledevice-net on Nuget.

I am trying to stream simple data from my phone to my PC over a TCP socket. This sometimes works, but sometimes as soon as I call deviceApi.idevice_connection_receive() I get error code 108 for USBMuxD - Connection reset

I have tried changing how I'm opening the TCP listener on the phone, changing how I'm starting the device on the PC, but nothing has worked. I am running out of ideas and was hoping someone could provide guidance as to what I may be doing wrong to get this intermittent connection reset issue.

Here is how I'm opening the TCP listener and accepting clients on the iphone:

 private void StartListen()
        {
                    listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    listenerSocket.Bind(new IPEndPoint(IPAddress.Loopback, SocketPort));
                    listenerSocket.Listen(10);
                    listenerSocket.BeginAccept(OnClientConnected, listenerSocket);
       }

  private void OnClientConnected(IAsyncResult ar)
        {
                    pcSocket = listenerSocket.EndAccept(ar);
                    //Begin sending/receiving data on pcSocket
        }

Here is how I'm connecting to the device on the PC:

private static void Initialize()
        {
            NativeLibraries.Load();
            deviceApi = LibiMobileDevice.Instance.iDevice;
            deviceApi.idevice_set_debug_callback(OnDeviceDebug);
            lockdownApi = LibiMobileDevice.Instance.Lockdown;
        }

private static void OnDeviceAddRemove(ref iDeviceEvent devEvent, IntPtr data)
        {
            switch (devEvent.@event)
            {
                case iDeviceEventType.DeviceAdd:
                    if (curConnectionHandle == null && devEvent.conn_type == iDeviceConnectionType.Usbmuxd)
                        Connect(devEvent.udidString);
                    break;
           }
       }

private static void Connect(string newUdid)
        {
                deviceApi.idevice_new(out curDeviceHandle, newUdid).ThrowOnError();
                curConnectionUdidString = newUdid;

                lockdownApi.lockdownd_client_new_with_handshake(curDeviceHandle, out curLockdownHandle,
                                                                "GeppettoLipsyncStreamer");

                 deviceApi.idevice_connect(curDeviceHandle, serverPort, out curConnectionHandle).ThrowOnError();

                  BeginReceiveData(curConnectionHandle);
        }

private static void BeginReceiveData()
        {
            receiverCancelTokenSrc?.Cancel();
            receiverCancelTokenSrc = new CancellationTokenSource();

            Task.Run(() => ReceiveDataAsync(receiverCancelTokenSrc.Token));
        }

private static async void ReceiveDataAsync(CancellationToken cancelToken)
        {
                while (true)
                {
                    uint receivedBytes = 0;

                    iDeviceError error = deviceApi.idevice_connection_receive(connection, dataBuffer, (uint) dataBuffer.Length,
                                                         ref receivedBytes);

                    if (error != iDeviceError.Success)
                    {
                        //This is returning error code -108 connection reset from USBmux above.
                        LogError("Failed to receive data from device: " + error);
                        continue;
                    }
      }

Any suggestions would be appreciated. I'm stumped as to what I'm doing wrong.

TerabyteTim commented 3 years ago

I have solved the issue - It was a problem with how I was initializing the TCP socket on the phone.