private void AfterReceive(IAsyncResult ar) { byte[] received; string receivedStr; ThisDevice clObject = (ThisDevice)ar.AsyncState; try { received = udpCli.EndReceive(ar, ref iPep); receivedStr = Encoding.UTF8.GetString(received); clObject.ipEp = iPep; clObject.data = receivedStr; //Console.WriteLine(clObject.Data); if (receivedStr == "DISCOVER_XSTREAM_SERVER") { Console.WriteLine("RECV_" + receivedStr); data = Encoding.ASCII.GetBytes("DISCOVER_XSTREAM_CLIENT"); Console.WriteLine("SEND_DISCOVER_XSTREAM_CLIENT"); udpCli.Send(data, data.Length, iPep); } else if (receivedStr == "ADD_SUCCESS") { Console.WriteLine("RECEIVE_ADD_SUCCESS"); ConnectedDevices.ManipClientObject(clObject); } else if (receivedStr == "REMOVE_THIS_DEVICE") { Console.WriteLine("RECEIVE_REMOVE_THIS_CLIENT"); ConnectedDevices.ManipClientObject(clObject); } Console.WriteLine("True"); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("False"); } if (!bWorker.CancellationPending) { try { Console.WriteLine(iPep.Address.ToString()); //AvailableDevices.ManipThisDevice(clObject); bWorker.ReportProgress(0, clObject); ThisDevice cliObj = new ThisDevice(); data = new byte[1024]; iPep = new IPEndPoint(IPAddress.Any, 8810); cliObj.ipEp = iPep; Console.WriteLine("Beginning receive"); udpCli.BeginReceive(new AsyncCallback(AfterReceive), cliObj); } catch (Exception expc) { MessageBox.Show(expc.Message); } } }