[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Reconnection issue between Winsock Server(VB6) and TCP Client (VB.NET

albertleng

8/4/2010 5:21:00 PM

I have a program (Winsock Server) written with VB6 which is connected
by a TCP Client (written in VB.NET).

The developer who writes the TCP Client told me that the TCP Client
can't reconnect to my Program (Winsock Server) after TCP Client
crashes or restarts. It can only reconnect to my program after my
program's restarted.

I did the same testing with a simple Winsock Client i wrote and
everything works fine.
What i notice is that when TCP Client tries to reconnect, Winsock
ConnectionRequest event is not being fired anymore.

Having no working knowledge about VB.NET, please advise the workaround
of this issue.

Below is my Winsock Server code (I removed other part of the program
which is unrelated to this issue).

Private Sub Form_Load()
On Error GoTo fixerr

' Set the LocalPort property to an integer.
' Then invoke the Listen method.
tcpServer.LocalPort = 500
tcpServer.Listen

Text1.Text = Text1.Text & " Listening via port " & tcpServer.LocalPort
& " at " & Now & vbCr

fixerr:
If Err.Number <> 0 Then
Text1.Text = Text1.Text & "error " & Err.Number & " " &
Err.Description & vbCr

End If


End Sub

Private Sub tcpServer_Close()
On Error GoTo fixerr
tcpServer.Close

Text1.Text = Text1.Text & " Client Disconnected via port " &
tcpServer.LocalPort & " at " & Now & vbCr

tcpServer.LocalPort = 500
tcpServer.Listen
Text1.Text = Text1.Text & " Listening via port " & tcpServer.LocalPort
& " at " & Now & vbCr

fixerr:
If Err.Number <> 0 Then
Text1.Text = Text1.Text & "error " & Err.Number & " " &
Err.Description & vbCr

End If
End Sub

Private Sub tcpServer_ConnectionRequest _
(ByVal requestID As Long)
On Error GoTo fixerr
' Check if the control's State is closed. If not,
' close the connection before accepting the new
' connection.

Text1.Text = Text1.Text & " Client attempts to connect via port " &
tcpServer.LocalPort & " at " & Now & vbCr

If tcpServer.State <> sckClosed Then
tcpServer.Close
End If
' Accept the request with the requestID
' parameter.
tcpServer.Accept requestID

If tcpServer.State = sckConnected Then
Text1.Text = Text1.Text & " Connected by client via port " &
tcpServer.LocalPort & " at " & Now & vbCr
End If

fixerr:
If Err.Number <> 0 Then
Text1.Text = Text1.Text & "error " & Err.Number & " " &
Err.Description & vbCr

End If
End Sub



Private Sub tcpServer_DataArrival _
(ByVal bytesTotal As Long)
On Error GoTo fixerr

' Declare a variable for the incoming data.
' Invoke the GetData method and set the Text
' property of a TextBox named txtOutput to
' the data.
Dim strData As String
tcpServer.GetData strData
txtOutput.Text = strData

fixerr:
If Err.Number <> 0 Then
Text1.Text = Text1.Text & "error " & Err.Number & " " &
Err.Description & vbCr
End If
End Sub
4 Answers

(nobody)

8/4/2010 6:01:00 PM

0

"albertleng" <albertleng@gmail.com> wrote in message
news:8a5f7303-cba8-4fda-aa82-758b89e0d318@g6g2000pro.googlegroups.com...
>I have a program (Winsock Server) written with VB6 which is connected
> by a TCP Client (written in VB.NET).
>
> The developer who writes the TCP Client told me that the TCP Client
> can't reconnect to my Program (Winsock Server) after TCP Client
> crashes or restarts. It can only reconnect to my program after my
> program's restarted.
>
> I did the same testing with a simple Winsock Client i wrote and
> everything works fine.
> What i notice is that when TCP Client tries to reconnect, Winsock
> ConnectionRequest event is not being fired anymore.
>
> Having no working knowledge about VB.NET, please advise the workaround
> of this issue.
>
> Below is my Winsock Server code (I removed other part of the program
> which is unrelated to this issue).


I think that the problem with your code is that Close event is not fired
when a client crashes(regardless of language). In that case, Error event
fires instead with error codes 11053, or 10054. So a quick fix is to close
the connection manually, then call tcpServer_Close() when you see these
errors. Note that calling the Close method on the server to close a client
doesn't fire Close event on the server(At fires only at the other side).

However, your code doesn't support more than one client. If you want that,
go to "Winsock Control" in MSDN, and click on "See Also", then "Using the
Winsock Control", then scroll down to "Accepting More than One Connection
Request".




albertleng

8/5/2010 1:20:00 AM

0

based on my testing, there is no error generated when the tcp client
crashes. basically, my program doesn't detect anything when tcp client
crashes.

this does not happen with the winsock client I wrote though.

thanks
On Aug 5, 2:00 am, "Nobody" <nob...@nobody.com> wrote:
> "albertleng" <albertl...@gmail.com> wrote in message
>
> news:8a5f7303-cba8-4fda-aa82-758b89e0d318@g6g2000pro.googlegroups.com...
>
>
>
>
>
> >I have a program (Winsock Server) written with VB6 which is connected
> > by a TCP Client (written in VB.NET).
>
> > The developer who writes the TCP Client told me that the TCP Client
> > can't reconnect to my Program (Winsock Server) after TCP Client
> > crashes or restarts. It can only reconnect to my program after my
> > program's restarted.
>
> > I did the same testing with a simple Winsock Client i wrote and
> > everything works fine.
> > What i notice is that when TCP Client tries to reconnect, Winsock
> > ConnectionRequest event is not being fired anymore.
>
> > Having no working knowledge about VB.NET, please advise the workaround
> > of this issue.
>
> > Below is my Winsock Server code (I removed other part of the program
> > which is unrelated to this issue).
>
> I think that the problem with your code is that Close event is not fired
> when a client crashes(regardless of language). In that case, Error event
> fires instead with error codes 11053, or 10054. So a quick fix is to close
> the connection manually, then call tcpServer_Close() when you see these
> errors. Note that calling the Close method on the server to close a client
> doesn't fire Close event on the server(At fires only at the other side).
>
> However, your code doesn't support more than one client. If you want that,
> go to "Winsock Control" in MSDN, and click on "See Also", then "Using the
> Winsock Control", then scroll down to "Accepting More than One Connection
> Request".

(nobody)

8/5/2010 2:53:00 PM

0

There is no socket listening for incoming connections all the time in your
software, so if something goes wrong with the current connection, there is
nothing waiting for the client to reconnect to. You have to implement
support for more than one client. At least, you have to add tcpListener
control, then keep that in Listen mode all the time. Example:

Private Sub Form_Load()
tcpServer.LocalPort = 500
tcpListener.LocalPort = 500
tcpListener.Listen
End Sub

Private Sub tcpListener_ConnectionRequest(ByVal requestID As Long)

If tcpServer.State <> sckClosed Then
tcpServer.Close
End If
' Accept the request with the requestID
' parameter.
tcpServer.Accept requestID

End Sub

Note how I moved the code from tcpServer_ConnectionRequest() to
tcpListener_ConnectionRequest(). "tcpListener" will remain in listening mode
forever. There is no need to call "Listen" anywhere again, including in
Close event.

The code above will allow one client to connect, even after crashing, but if
there is a second client trying to connect, the first client is
disconnected. To really support more than one client, you have to turn
tcpServer into a control array, like the MSDN article shows. The MSDN
article has a little problem, it doesn't reuse existing closed sockets, so
after 32767 connections, your software would show an error message and stops
accepting new connections. To fix this, you need to loop through the control
array, and reuse sockets. Example:

Found = False
For i = tcpServer.LBound To tcpServer.UBound
If tcpServer(i).State = sckClosed Then
' Reuse this one
Found = True
Exit For
End If
Next
If Found Then
' Accept the request with the requestID
' parameter.
tcpServer(i).Accept requestID
Else
i = tcpServer.UBound+1
Load tcpServer(i)
tcpServer(i).Accept requestID
End If


albertleng

8/6/2010 3:48:00 PM

0

Great!!! By having a Winsock control which solely listens for
connection request and pass the request to another Winsock control
works!

The next thing i'll try to implement is to allow multiple clients
connections.

Thanks a lot! :)


On Aug 5, 10:53 pm, "Nobody" <nob...@nobody.com> wrote:
> There is no socket listening for incoming connections all the time in your
> software, so if something goes wrong with the current connection, there is
> nothing waiting for the client to reconnect to. You have to implement
> support for more than one client. At least, you have to add tcpListener
> control, then keep that in Listen mode all the time. Example:
>
> Private Sub Form_Load()
>     tcpServer.LocalPort = 500
>     tcpListener.LocalPort = 500
>     tcpListener.Listen
> End Sub
>
> Private Sub tcpListener_ConnectionRequest(ByVal requestID As Long)
>
> If tcpServer.State <> sckClosed Then
>     tcpServer.Close
> End If
> ' Accept the request with the requestID
> ' parameter.
> tcpServer.Accept requestID
>
> End Sub
>
> Note how I moved the code from tcpServer_ConnectionRequest() to
> tcpListener_ConnectionRequest(). "tcpListener" will remain in listening mode
> forever. There is no need to call "Listen" anywhere again, including in
> Close event.
>
> The code above will allow one client to connect, even after crashing, but if
> there is a second client trying to connect, the first client is
> disconnected. To really support more than one client, you have to turn
> tcpServer into a control array, like the MSDN article shows. The MSDN
> article has a little problem, it doesn't reuse existing closed sockets, so
> after 32767 connections, your software would show an error message and stops
> accepting new connections. To fix this, you need to loop through the control
> array, and reuse sockets. Example:
>
> Found = False
> For i = tcpServer.LBound To tcpServer.UBound
>     If tcpServer(i).State = sckClosed Then
>         ' Reuse this one
>         Found = True
>         Exit For
>     End If
> Next
> If Found Then
>     ' Accept the request with the requestID
>     ' parameter.
>     tcpServer(i).Accept requestID
> Else
>     i = tcpServer.UBound+1
>     Load tcpServer(i)
>     tcpServer(i).Accept requestID
> End If