I am developing a Windows Phone application that will connect to my server. It does this using ConnectAsync when you press the login button. But if the server is turned off and you want to cancel the connection attempt, what should I do?
Here is the current client code with my last attempt to close the socket connection. It is suggested that you can easily implement a timeout as soon as you learn how to disconnect a connection.
private IPAddress ServerAddress = new IPAddress(0xff00ff00); //Censored my IP private int ServerPort = 13000; private Socket CurrentSocket; private SocketAsyncEventArgs CurrentSocketEventArgs; private bool Connecting = false; private void Button_Click(object sender, RoutedEventArgs e) { try { if (Connecting) { CurrentSocket.Close(); CurrentSocket.Dispose(); CurrentSocketEventArgs.Dispose(); CurrentSocket = null; CurrentSocketEventArgs = null; } UserData userdata = new UserData(); userdata.Username = usernameBox.Text; userdata.Password = passwordBox.Password; Connecting = ConnectToServer(userdata); } catch (Exception exception) { Dispatcher.BeginInvoke(() => MessageBox.Show("Error: " + exception.Message)); } } private bool ConnectToServer(UserData userdata) { CurrentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Create a new SocketAsyncEventArgs CurrentSocketEventArgs = new SocketAsyncEventArgs(); CurrentSocketEventArgs.RemoteEndPoint = new IPEndPoint(ServerAddress, ServerPort); CurrentSocketEventArgs.Completed += ConnectionCompleted; CurrentSocketEventArgs.UserToken = userdata; CurrentSocketEventArgs.SetBuffer(new byte[1024], 0, 1024); CurrentSocket.ConnectAsync(CurrentSocketEventArgs); return true; }
Edit: The thought that struck me was that perhaps this is a server computer that overlaps requests, even if the server software is not included? Is it possible?
source share