socket servidor permitida operación error conectados cliente c# sockets timeout

c# - servidor - Cómo configurar el tiempo de espera de conexión del socket



error de socket (10)

Acabo de escribir una clase de extensión para permitir tiempos de espera en las conexiones. Úselo exactamente como usaría los métodos estándar de Connect() , con un parámetro extra llamado timeout .

using System; using System.Net; using System.Net.Sockets; /// <summary> /// Extensions to Socket class /// </summary> public static class SocketExtensions { /// <summary> /// Connects the specified socket. /// </summary> /// <param name="socket">The socket.</param> /// <param name="host">The host.</param> /// <param name="port">The port.</param> /// <param name="timeout">The timeout.</param> public static void Connect(this Socket socket, string host, int port, TimeSpan timeout) { AsyncConnect(socket, (s, a, o) => s.BeginConnect(host, port, a, o), timeout); } /// <summary> /// Connects the specified socket. /// </summary> /// <param name="socket">The socket.</param> /// <param name="addresses">The addresses.</param> /// <param name="port">The port.</param> /// <param name="timeout">The timeout.</param> public static void Connect(this Socket socket, IPAddress[] addresses, int port, TimeSpan timeout) { AsyncConnect(socket, (s, a, o) => s.BeginConnect(addresses, port, a, o), timeout); } /// <summary> /// Asyncs the connect. /// </summary> /// <param name="socket">The socket.</param> /// <param name="connect">The connect.</param> /// <param name="timeout">The timeout.</param> private static void AsyncConnect(Socket socket, Func<Socket, AsyncCallback, object, IAsyncResult> connect, TimeSpan timeout) { var asyncResult = connect(socket, null, null); if (!asyncResult.AsyncWaitHandle.WaitOne(timeout)) { try { socket.EndConnect(asyncResult); } catch (SocketException) { } catch (ObjectDisposedException) { } } }

Cuando el Cliente intenta conectarse a una dirección IP desconectada, hay un tiempo de espera prolongado de más de 15 segundos ... ¿Cómo podemos reducir este tiempo de espera? ¿Cuál es el método para configurarlo?

El código que estoy usando para configurar una conexión de socket es el siguiente:

try { m_clientSocket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(serverIp); int iPortNo = System.Convert.ToInt16(serverPort); IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo); m_clientSocket.Connect(ipEnd); if (m_clientSocket.Connected) { lb_connectStatus.Text = "Connection Established"; WaitForServerData(); } } catch (SocketException se) { lb_connectStatus.Text = "Connection Failed"; MessageBox.Show(se.Message); }



Encontré esto. Más simple que la respuesta aceptada, y funciona con .NET v2

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect using a timeout (5 seconds) IAsyncResult result = socket.BeginConnect( sIP, iPort, null, null ); bool success = result.AsyncWaitHandle.WaitOne( 5000, true ); if ( socket.Connected ) { socket.EndConnect( result ); } else { // NOTE, MUST CLOSE THE SOCKET socket.Close(); throw new ApplicationException("Failed to connect server."); } //...


Esto es como la respuesta de FlappySock, pero agregué una devolución de llamada porque no me gustó el diseño y cómo se devolvía el booleano. En los comentarios de esa respuesta de Nick Miller:

En mi experiencia, si se puede alcanzar el punto final, pero no hay un servidor en el punto final que pueda recibir la conexión, entonces AsyncWaitHandle.WaitOne se señalará, pero el socket permanecerá desconectado.

Entonces, para mí, parece depender de lo que se devuelve puede ser peligroso. Prefiero usar socket.Connected . Establecí un booleano que se puede anular y lo actualizo en la función de devolución de llamada. También descubrí que no siempre termina de informar el resultado antes de regresar a la función principal; también me ocupo de eso y lo hago esperar por el resultado usando el tiempo de espera:

private static bool? areWeConnected = null; private static bool checkSocket(string svrAddress, int port) { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(svrAddress), port); Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); int timeout = 5000; // int.Parse(ConfigurationManager.AppSettings["socketTimeout"].ToString()); int ctr = 0; IAsyncResult ar = socket.BeginConnect(endPoint, Connect_Callback, socket); ar.AsyncWaitHandle.WaitOne( timeout, true ); // Sometimes it returns here as null before it''s done checking the connection // No idea why, since .WaitOne() should block that, but it does happen while (areWeConnected == null && ctr < timeout) { Thread.Sleep(100); ctr += 100; } // Given 100ms between checks, it allows 50 checks // for a 5 second timeout before we give up and return false, below if (areWeConnected == true) { return true; } else { return false; } } private static void Connect_Callback(IAsyncResult ar) { areWeConnected = null; try { Socket socket = (Socket)ar.AsyncState; areWeConnected = socket.Connected; socket.EndConnect(ar); } catch (Exception ex) { areWeConnected = false; // log exception } }

Relacionado: ¿Cómo verificar si estoy conectado?


Mi toma:

public static class SocketExtensions { /// <summary> /// Connects the specified socket. /// </summary> /// <param name="socket">The socket.</param> /// <param name="endpoint">The IP endpoint.</param> /// <param name="timeout">The timeout.</param> public static void Connect(this Socket socket, EndPoint endpoint, TimeSpan timeout) { var result = socket.BeginConnect(endpoint, null, null); bool success = result.AsyncWaitHandle.WaitOne(timeout, true); if (success) { socket.EndConnect(result); } else { socket.Close(); throw new SocketException(10060); // Connection timed out. } } }


Mira esto en MSDN . No parece que pueda hacer esto con las propiedades implementadas en la clase Socket.

El póster en MSDN en realidad resolvió su problema usando el enhebrado. Tenía un hilo principal que llamaba a otros hilos que ejecutaban el código de conexión durante un par de segundos y luego verificaban la propiedad Connected del socket:

Creé otro método que en realidad conectaba el zócalo ... tenía el hilo principal en reposo durante 2 segundos y luego verificaba el método de conexión (que se ejecuta en una secuencia separada) si el zócalo estaba conectado bien o arrojaba una excepción "Tiempo agotado" y eso es todo. Gracias de nuevo por las repeticiones.

¿Qué estás tratando de hacer, y por qué no puede esperar entre 15 y 30 segundos antes de que se agote el tiempo?


No programo en C # pero en C, resolvemos el mismo problema haciendo que el socket no sea bloqueado y luego colocamos el fd en un ciclo select / poll con un valor de tiempo de espera igual al tiempo que estamos dispuestos a esperar para la conexión para triunfar.

Encontré this para Visual C ++ y la explicación también se inclina hacia el mecanismo select / poll que expliqué antes.

En mi experiencia, no puede cambiar los valores de tiempo de espera de conexión por socket. Usted lo cambia para todo (ajustando los parámetros del sistema operativo).


Resolví el problema usando el método Socket.ConnectAsync en lugar del método Socket.Connect. Después de invocar el Socket.ConnectAsync (SocketAsyncEventArgs), inicie un temporizador (timer_connection), si el tiempo está activo, verifique si la conexión del socket está conectada (if (m_clientSocket.Connected)), si no, aparece el error de tiempo de espera.

private void connect(string ipAdd,string port) { try { SocketAsyncEventArgs e=new SocketAsyncEventArgs(); m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(serverIp); int iPortNo = System.Convert.ToInt16(serverPort); IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo); //m_clientSocket. e.RemoteEndPoint = ipEnd; e.UserToken = m_clientSocket; e.Completed+=new EventHandler<SocketAsyncEventArgs>(e_Completed); m_clientSocket.ConnectAsync(e); if (timer_connection != null) { timer_connection.Dispose(); } else { timer_connection = new Timer(); } timer_connection.Interval = 2000; timer_connection.Tick+=new EventHandler(timer_connection_Tick); timer_connection.Start(); } catch (SocketException se) { lb_connectStatus.Text = "Connection Failed"; MessageBox.Show(se.Message); } } private void e_Completed(object sender,SocketAsyncEventArgs e) { lb_connectStatus.Text = "Connection Established"; WaitForServerData(); } private void timer_connection_Tick(object sender, EventArgs e) { if (!m_clientSocket.Connected) { MessageBox.Show("Connection Timeout"); //m_clientSocket = null; timer_connection.Stop(); } }


Trabajé con Unity y tuve algún problema con el BeginConnect y otros métodos asíncronos del socket.

Hay algo que no entiendo, pero las muestras de código anteriores no funcionan para mí.

Así que escribí este fragmento de código para que funcione. Lo pruebo en una red ad hoc con Android y PC, también en local en mi computadora. Espero que pueda ayudar.

using System.Net.Sockets; using System.Threading; using System.Net; using System; using System.Diagnostics; class ConnexionParameter : Guardian { public TcpClient client; public string address; public int port; public Thread principale; public Thread thisthread = null; public int timeout; private EventWaitHandle wh = new AutoResetEvent(false); public ConnexionParameter(TcpClient client, string address, int port, int timeout, Thread principale) { this.client = client; this.address = address; this.port = port; this.principale = principale; this.timeout = timeout; thisthread = new Thread(Connect); } public void Connect() { WatchDog.Start(timeout, this); try { client.Connect(IPAddress.Parse(address), port); } catch (Exception) { UnityEngine.Debug.LogWarning("Unable to connect service (Training mode? Or not running?)"); } OnTimeOver(); //principale.Resume(); } public bool IsConnected = true; public void OnTimeOver() { try { if (!client.Connected) { /*there is the trick. The abort method from thread doesn''t make the connection stop immediately(I think it''s because it rise an exception that make time to stop). Instead I close the socket while it''s trying to connect , that make the connection method return faster*/ IsConnected = false; client.Close(); } wh.Set(); } catch(Exception) { UnityEngine.Debug.LogWarning("Connexion already closed, or forcing connexion thread to end. Ignore."); } } public void Start() { thisthread.Start(); wh.WaitOne(); //principale.Suspend(); } public bool Get() { Start(); return IsConnected; } } public static class Connexion { public static bool Connect(this TcpClient client, string address, int port, int timeout) { ConnexionParameter cp = new ConnexionParameter(client, address, port, timeout, Thread.CurrentThread); return cp.Get(); } //http://.com/questions/19653588/timeout-at-acceptsocket public static Socket AcceptSocket(this TcpListener tcpListener, int timeoutms, int pollInterval = 10) { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutms); var stopWatch = new Stopwatch(); stopWatch.Start(); while (stopWatch.Elapsed < timeout) { if (tcpListener.Pending()) return tcpListener.AcceptSocket(); Thread.Sleep(pollInterval); } return null; } }

y hay un perro guardián muy simple en C # para que funcione:

using System.Threading; public interface Guardian { void OnTimeOver(); } public class WatchDog { int m_iMs; Guardian m_guardian; public WatchDog(int a_iMs, Guardian a_guardian) { m_iMs = a_iMs; m_guardian = a_guardian; Thread thread = new Thread(body); thread.Start(this); } private void body(object o) { WatchDog watchdog = (WatchDog)o; Thread.Sleep(watchdog.m_iMs); watchdog.m_guardian.OnTimeOver(); } public static void Start(int a_iMs, Guardian a_guardian) { new WatchDog(a_iMs, a_guardian); } }


Tuve el mismo problema al conectarme a un zócalo y se me ocurrió la siguiente solución: Funciona bien para mí. `

private bool CheckConnectivityForProxyHost(string hostName, int port) { if (string.IsNullOrEmpty(hostName)) return false; bool isUp = false; Socket testSocket = null; try { testSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = null; if (testSocket != null && NetworkingCollaboratorBase.GetResolvedConnecionIPAddress(hostName, out ip))//Use a method to resolve your IP { IPEndPoint ipEndPoint = new IPEndPoint(ip, port); isUp = false; //time out 5 Sec CallWithTimeout(ConnectToProxyServers, 5000, testSocket, ipEndPoint); if (testSocket != null && testSocket.Connected) { isUp = true; } } } } catch (Exception ex) { isUp = false; } finally { try { if (testSocket != null) { testSocket.Shutdown(SocketShutdown.Both); } } catch (Exception ex) { } finally { if (testSocket != null) testSocket.Close(); } } return isUp; } private void CallWithTimeout(Action<Socket, IPEndPoint> action, int timeoutMilliseconds, Socket socket, IPEndPoint ipendPoint) { try { Action wrappedAction = () => { action(socket, ipendPoint); }; IAsyncResult result = wrappedAction.BeginInvoke(null, null); if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds)) { wrappedAction.EndInvoke(result); } } catch (Exception ex) { } } private void ConnectToProxyServers(Socket testSocket, IPEndPoint ipEndPoint) { try { if (testSocket == null || ipEndPoint == null) return; testSocket.Connect(ipEndPoint); } catch (Exception ex) { } }