public void CloseAbortively() { try { ClientSocket.AbortiveClose(); ClientSocket = null; ClientSocketState = SocketState.Closed; Console.WriteLine("Abortively closed socket"); } catch (Exception ex) { ResetSocket(); Console.WriteLine("Error aborting socket: [" + ex.GetType().Name + "] " + ex.Message); } finally { RefreshDisplay(); } }
public void Connect(string ip, int port) { try { // Read the IP address IPAddress serverIpAddress; if (!IPAddress.TryParse(ip, out serverIpAddress)) { Console.WriteLine("Invalid IP address: " + ip); return; } // Begin connecting to the remote IP ClientSocket = new SimpleClientTcpSocket(); ClientSocket.ConnectCompleted += ClientSocket_ConnectCompleted; ClientSocket.PacketArrived += ClientSocket_PacketArrived; ClientSocket.WriteCompleted += args => ClientSocket_WriteCompleted(ClientSocket, args); ClientSocket.ShutdownCompleted += ClientSocket_ShutdownCompleted; ClientSocket.ConnectAsync(serverIpAddress, port); ClientSocketState = SocketState.Connecting; Console.WriteLine("Connecting socket to " + (new IPEndPoint(serverIpAddress, port))); } catch (Exception ex) { ResetSocket(); Console.WriteLine("Error creating connecting socket: [" + ex.GetType().Name + "] " + ex.Message); } finally { RefreshDisplay(); } }
/// <summary> /// Closes and clears the socket, without causing exceptions. /// </summary> protected void ResetSocket() { // Close the socket ClientSocket.Close(); ClientSocket = null; // Indicate there is no socket connection ClientSocketState = SocketState.Closed; }