Winsock - Connection is forcefully rejected (10061)
Hi there,
I made a simple remote Server / Client application and it's working fine, but the Client wont reconnect at the second time, it will only work if I exit the Server and rerun it.
I'm getting error on second reconnect attempt "10061 - Connection is forcefully rejected".
Server code:
Code:
Private Sub Form_Load()
With wskServer
.Protocol = sckTCPProtocol
.RemotePort = 0
.LocalPort = 51393
.Listen
End With
Debug.Print "TCP Server started..."
Debug.Print "- Host: " & wskServer.LocalHostName
Debug.Print "- IP: " & wskServer.LocalIP
Debug.Print "- Port: " & wskServer.LocalPort
Debug.Print "============================="
Exit Sub
Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)
If wskServer.State <> sckClosed Then wskServer.Close
wskServer.Accept requestID
Debug.Print "Client connected from IP: " & wskServer.RemoteHostIP & ", Port: " & wskServer.RemotePort
End Sub
Private Sub wskServer_Close()
Debug.Print "Client disconnected from IP: " & wskServer.RemoteHostIP & ", Port: " & wskServer.RemotePort
End Sub
Private Sub wskServer_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Error " & Number & ": " & Description
End Sub
I really don't know where would be the issue.
Any guidance is highly appreciated! ;)
Re: Winsock - Connection is forcefully rejected (10061)
As previously mentioned (in another thread), you don't accept a request on the same port as the listening port...you were essentially given the answer to your question right there.
https://www.vbforums.com/showthread....=1#post3927745 MIGHT help you work out how to do multiple winsock connections...but, to keep it simple, you have more than one winsock control and you accept the request on a different one to the one you receive the request from (as you might be able to work out from this link). This has the added bonus that you can have multiple connections at once (which you might want) and you can load new winsocks on demand as needed. If you only need one connection, the listening port still needs to be open to listen...what you're essentially doing is closing the winsock when you disconnect so assumedly it isn't listening any more...hence why you hand off the connection to another winsock, so the listening port is still open.
If you need more explanation than this, is there ANY way you can avoid using winsock? Sorry for being blunt, but it is IN NO WAY a forgiving and friendly thing to try to code with...I avoid it where I can as it gives me headaches just trying to follow the logic at times.
Re: Winsock - Connection is forcefully rejected (10061)
Quote:
Originally Posted by
SmUX2k
As previously mentioned (
in another thread), you don't accept a request on the same port as the listening port...you were essentially given the answer to your question right there.
https://www.vbforums.com/showthread....=1#post3927745 MIGHT help you work out how to do multiple winsock connections...but, to keep it simple, you have more than one winsock control and you accept the request on a different one to the one you receive the request from (as you might be able to work out from this link). This has the added bonus that you can have multiple connections at once (which you might want) and you can load new winsocks on demand as needed. If you only need one connection, the listening port still needs to be open to listen...what you're essentially doing is closing the winsock when you disconnect so assumedly it isn't listening any more...hence why you hand off the connection to another winsock, so the listening port is still open.
If you need more explanation than this, is there ANY way you can avoid using winsock? Sorry for being blunt, but it is IN NO WAY a forgiving and friendly thing to try to code with...I avoid it where I can as it gives me headaches just trying to follow the logic at times.
I don't want a multiple connections, basically I just want two application to talk between each other.
Re: Winsock - Connection is forcefully rejected (10061)
it was ages ago I used winsock.
of what I can remember u use 1 listen port.
when u establish a connection, u create a new winsock and a new port and connect with the guest.
that way the listen port is always open. surely u can add a variable that tells if u have a connection and if so u don't allow a new one.
another thing. ports can be locked for a bit, even if u close it, windows can lock it for a bit of time.
its also important to be sure the port is closed properly.
Re: Winsock - Connection is forcefully rejected (10061)
Quote:
Originally Posted by
beic
I don't want a multiple connections, basically I just want two application to talk between each other.
No problem. Just use two Winsock controls for your server -- wskServer and wskAccepted -- and do this
Code:
Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)
If wskAccepted.State <> sckClosed Then wskAccepted.Close
wskAccepted.Accept requestID
...
End Sub
The idea is to listen on wskServer but accept client connection on (single) wskAccepted and receive/send data on this wskAccepted. This way no two active client connection are possible as you explicitly close first one in ConnectionRequest event, just before accepting the second one.
The behavior will be something like this:
- first client connects and starts communicating with the server
- the moment a hypothetical second client tries to connect it knocks out the first one and "overtakes" server attention which looks bizarre but will get the job done in your case.
cheers,
</wqw>
Re: Winsock - Connection is forcefully rejected (10061)
Quote:
Originally Posted by
wqweto
No problem. Just use
two Winsock controls for your server -- wskServer and wskAccepted -- and do this
Code:
Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)
If wskAccepted.State <> sckClosed Then wskAccepted.Close
wskAccepted.Accept requestID
...
End Sub
The idea is to listen on wskServer but accept client connection on (single) wskAccepted and receive/send data on this wskAccepted. This way no two active client connection are possible as you explicitly close first one in ConnectionRequest event, just before accepting the second one.
The behavior will be something like this:
- first client connects and starts communicating with the server
- the moment a hypothetical second client tries to connect it knocks out the first one and "overtakes" server attention which looks bizarre but will get the job done in your case.
cheers,
</wqw>
Ok, I understood the concept and the description, but I don't get the full implementation of it.
Now, how would I configure both Winsock control in this case at Form Load and beyond?, because now I would have two of them.
Code:
1. wskServer
2. wskAccepted
In meant regarding for Host and Ports.
Re: Winsock - Connection is forcefully rejected (10061)
Add both controls, don't set any property at design-time and try this echo server i.e. just repeats what is sent to it
Code:
Option Explicit
Private Sub Form_Load()
With wskServer
.Protocol = sckTCPProtocol
.RemotePort = 0
.LocalPort = 51393
.Listen
End With
Debug.Print "TCP Server started..."
Debug.Print "- Host: " & wskServer.LocalHostName
Debug.Print "- IP: " & wskServer.LocalIP
Debug.Print "- Port: " & wskServer.LocalPort
Debug.Print "============================="
End Sub
Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)
If wskAccepted.State <> sckClosed Then
wskAccepted.Close
wskAccepted_Close
End If
wskAccepted.Accept requestID
wskAccepted_Connect
End Sub
Private Sub wskAccepted_Connect()
Debug.Print "Client connected from IP: " & wskAccepted.RemoteHostIP & ", Port: " & wskAccepted.RemotePort
End Sub
Private Sub wskAccepted_Close()
Debug.Print "Client disconnected from IP: " & wskAccepted.RemoteHostIP & ", Port: " & wskAccepted.RemotePort
End Sub
Private Sub wskAccepted_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Error " & Number & ": " & Description
End Sub
Private Sub wskAccepted_DataArrival(ByVal bytesTotal As Long)
Dim baBuffer() As Byte
wskAccepted.GetData baBuffer
wskAccepted.SendData baBuffer
End Sub
cheers,
</wqw>