Because in server side in listen method when run reaches to tcpListener.AcceptTcpClient ; program UI blocks and closing socket reactivate the UI
.If you are interested here is the link of server which is open source project. It is the server of remote control collection app in google play.
https://github.com/Steppschuh/RemoteControlServer/releases
here is the listen method code:
Public Sub listen()
Try
If tcpListener.Pending = True Then
tcpClient = tcpListener.AcceptTcpClient
tcpClient.ReceiveBufferSize = buffer
Dim localEndPoint As Net.IPEndPoint = tcpClient.Client.LocalEndPoint
Dim remoteEndPoint As Net.IPEndPoint = tcpClient.Client.RemoteEndPoint
Dim Reader As New BinaryReader(tcpClient.GetStream)
Try
Dim message_data As Byte()
message_data = Reader.ReadBytes(tcpClient.ReceiveBufferSize)
Dim command As New Command
command.source = remoteEndPoint.Address.ToString
command.destination = localEndPoint.Address.ToString
command.data = message_data
command.process()
'Logger.add("LocalEndPoint: " & command.destination)
'Logger.add("RemoteEndPoint: " & command.source)
Reader.Close()
Catch ex As Exception
Logger.add("Error while parsing TCP data:")
Logger.add(ex.ToString)
End Try
End If
Catch ex As Exception
Logger.add("Error while receiving TCP data:")
Logger.add(ex.ToString)
End Try
End Sub
I wrote to socket.outputstream in socket_connected event instead of initializing an asyncstream and writing to that.
In every command after writing command to outputstream I flushed then closed the stream . then reconnect the client socket
and repeat this process in socket_connected event. Up to now it worked well but I have to test it more