How to (really) cancel a ConnectAsync request on a Windows Phone?

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?

+4
source share
3 answers

I believe

 socket.Close() 

you should cancel the asynchronous connection attempt. There may be some exceptions that need to be caught as a result.

+3
source

Your code looks fine. As Marc has already said, closing a socket cancels all pending operations.

Yes, sometimes it’s possible that you connect to OK and nothing happens. To check at the command prompt

telnet 192.168.1.44 31337 , where 192.168.1.44 is ServerAddress (also the name OK), and 31337 is ServerPort. First you can enable the “Telnet Client” using the functions “Control Panel / Programs and Features / Turn on Windows”. If you see “Could not open connection” = your WinForms application will not be able to connect. If you see a black screen with a blinking cursor = your WinForms application should connect to OK.

0
source

What happens here is that you specify a buffer in the ConnectAsync argument.

 CurrentSocketEventArgs.SetBuffer(new byte[1024], 0, 1024); 

The documentation says:

 Optionally, a buffer may be provided which will atomically be sent on the socket after the ConnectAsync method succeeds. 

Thus, your server will immediately see the connection and data. Your cancellation code is fine, it’s just that the data is sent before you get a chance to cancel something.

0
source

All Articles