N
Nickneem
I hope there's someone out there who knows what's wrong with my code.
I'm writing an app to connect with a TCPClient and send orders back and
forth..
Problem is that when I exit the application with me.close not all
threads are being closed, so I can't restart my app...
Same thing when I send a disconnect to the server, I can't reconnect
so I guess it must have something to do with blocking / endread?
Private Function ConnectToServer() As Boolean
Try
'Creates a TCPClient using a localend point.
'connect to the server
Dim ip As System.Net.IPAddress =
IPAddress.Parse(Global.Settings.GetString(SettingKeys.IpAddress))
client = New TcpClient
client.Connect(ip, PORT_NUM)
Threading.Thread.Sleep(MySleep) 'Misschien te weinig tijd
tussen connect
'en(beginread)
' Start an asynchronous read invoking DoRead to avoid
lagging the user
' interface.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,
AddressOf DoRead, Nothing)
' Make sure the window is showing before popping up
connection dialog.
Me.Show()
SendData("CONNECT|" & lblEmployee.Text)
Log("After senddata", LogWriter)
Catch Ex As SocketException
Select Case Ex.ErrorCode
Case 10060
MsgBox("Server with IP: " &
Global.Settings.GetString(SettingKeys.IpAddress) & " on port: " &
Global.Settings.GetString(SettingKeys.PortNumber) & " unreachable!", _
MsgBoxStyle.Exclamation,
Me.Text)
Case 10061
MsgBox("Server's down. Restart server and try
again.", _
MsgBoxStyle.Exclamation,
Me.Text)
Case 11001
MsgBox("No server known as: ", _
MsgBoxStyle.Exclamation,
Me.Text)
Case 10065
MsgBox("No wireless connection?", _
MsgBoxStyle.Exclamation,
Me.Text)
Case Else
MsgBox("Unknown error: " & Ex.ErrorCode)
client.Close()
ShowOptionsForm()
End Select
ClientConnected = False 'zet vlag connection is uit
If IsDBNull(client) Then Exit Function
If Not IsNothing(client) Then
'Close stream so that our AsyncReader are closed
'01-01-05 even uitgezet
' client.GetStream.Close()
'Close tcpClient
client.Close()
End If
End Try
Return True
End Function
'['CLOSE' PART]
Private Sub MainForm_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Try
If ClientConnected = True Then
SendData("DISCONNECT")
End If
Threading.Thread.Sleep(MySleep)
' Send only if server is still running.
Call ResetSocket()
Threading.Thread.Sleep(MySleep)
Catch ex As Exception
End Try
End Sub
'[CLEAN UP]
Private Sub ResetSocket()
Try
If Not client.GetStream Is Nothing Then
client.GetStream.Close()
End If
client.Close()
streamRead.Close()
streamRead = Nothing
client = Nothing
Catch ex As Exception
End Try
End Sub
I saw some other 'similar' postings:
http://groups.google.nl/group/micro...854ab5?q=close+&rnum=4&hl=nl#e3480a4eeb854ab5
but I am unable / incapable to solve this myself...
Thanks in advance,
Mike Dole
I'm writing an app to connect with a TCPClient and send orders back and
forth..
Problem is that when I exit the application with me.close not all
threads are being closed, so I can't restart my app...
Same thing when I send a disconnect to the server, I can't reconnect
so I guess it must have something to do with blocking / endread?
Private Function ConnectToServer() As Boolean
Try
'Creates a TCPClient using a localend point.
'connect to the server
Dim ip As System.Net.IPAddress =
IPAddress.Parse(Global.Settings.GetString(SettingKeys.IpAddress))
client = New TcpClient
client.Connect(ip, PORT_NUM)
Threading.Thread.Sleep(MySleep) 'Misschien te weinig tijd
tussen connect
'en(beginread)
' Start an asynchronous read invoking DoRead to avoid
lagging the user
' interface.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE,
AddressOf DoRead, Nothing)
' Make sure the window is showing before popping up
connection dialog.
Me.Show()
SendData("CONNECT|" & lblEmployee.Text)
Log("After senddata", LogWriter)
Catch Ex As SocketException
Select Case Ex.ErrorCode
Case 10060
MsgBox("Server with IP: " &
Global.Settings.GetString(SettingKeys.IpAddress) & " on port: " &
Global.Settings.GetString(SettingKeys.PortNumber) & " unreachable!", _
MsgBoxStyle.Exclamation,
Me.Text)
Case 10061
MsgBox("Server's down. Restart server and try
again.", _
MsgBoxStyle.Exclamation,
Me.Text)
Case 11001
MsgBox("No server known as: ", _
MsgBoxStyle.Exclamation,
Me.Text)
Case 10065
MsgBox("No wireless connection?", _
MsgBoxStyle.Exclamation,
Me.Text)
Case Else
MsgBox("Unknown error: " & Ex.ErrorCode)
client.Close()
ShowOptionsForm()
End Select
ClientConnected = False 'zet vlag connection is uit
If IsDBNull(client) Then Exit Function
If Not IsNothing(client) Then
'Close stream so that our AsyncReader are closed
'01-01-05 even uitgezet
' client.GetStream.Close()
'Close tcpClient
client.Close()
End If
End Try
Return True
End Function
'['CLOSE' PART]
Private Sub MainForm_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Try
If ClientConnected = True Then
SendData("DISCONNECT")
End If
Threading.Thread.Sleep(MySleep)
' Send only if server is still running.
Call ResetSocket()
Threading.Thread.Sleep(MySleep)
Catch ex As Exception
End Try
End Sub
'[CLEAN UP]
Private Sub ResetSocket()
Try
If Not client.GetStream Is Nothing Then
client.GetStream.Close()
End If
client.Close()
streamRead.Close()
streamRead = Nothing
client = Nothing
Catch ex As Exception
End Try
End Sub
I saw some other 'similar' postings:
http://groups.google.nl/group/micro...854ab5?q=close+&rnum=4&hl=nl#e3480a4eeb854ab5
but I am unable / incapable to solve this myself...
Thanks in advance,
Mike Dole