The problem of sending data over the socket

ahmedAlie 161 Reputation points
2022-01-24T01:20:39.627+00:00

hi

Send data via socket from Android to computer. The code works without problems. The problem comes if the computer is disconnected from the Internet during transmission. The program stops in Android and stops the interface Is there a way to use the Thread or something that prevents the interface from stopping and bypassing the problem

    try
            {

                byte[] bytes = new byte[1024];

                IPAddress ipAddress = IPAddress.Parse(ip);
                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);


                  Socket sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                    sender.Connect(remoteEP);


                    byte[] msg = System.Text.Encoding.UTF8.GetBytes(ms);

                    // Send the data through the socket.  
                    int bytesSent = sender.Send(msg);

                    // Receive the response from the remote device.  
                    int bytesRec = sender.Receive(bytes);


                     sender.Close();
                    sender.Dispose();

                catch (Exception e)
                {
                    Toast.MakeText(this, "Unexpected exception : " + e.ToString(), ToastLength.Short).Show();
                    return;
                }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
{count} votes