VB6winsock控件:关闭连接并连接到另一台服务器
我正在开发一个旧版 VB6 应用程序。该应用程序使用 RAS API 建立调制解调器连接,然后使用 Winsock 控件连接到 IP 地址和 IP 地址。港口。
我现在正在向此应用程序添加“故障转移”功能,如果它无法连接到服务器,它会尝试连接到列表中的下一个服务器。
假设我有两台服务器,服务器应用程序 A 和服务器应用程序 B。(在测试期间,我交换了这些服务器以验证两台服务器均可访问并且不存在路由/防火墙问题。)
如果我停止服务器应用程序 A,则当应用程序尝试连接到服务器应用程序 A 然后连接到服务器应用程序 B 时,会发生一些非常奇怪的事情:
mySocket.close
mySocket.Connect serverA, portA
- mySocket.state = 6 sckConnecting
- 套接字连接事件被调用
- mySocket.state = 7 sckConnected
- mySocket.state = 8 sckClosing
(这似乎有点奇怪:我不确定为什么它似乎连接了一会儿。)
套接字保持在 sckClosing 状态。几百毫秒后,我继续尝试连接到服务器 B:
mySocket.close
- mySocket.state = 0 sckClosed
。
mySocket.Connect serverB, portB
- mySocket.state = 6 sckConnecting
- 套接字错误事件被调用,错误为 10060 (WSAETIMEDOUT)
- mySocket.state = 9 sckError
此时,如果我启动服务器应用程序 A,执行 RAShangup 和 RASdial,并尝试连接到服务器 A,一切正常好的。
否则序列
socket.connect ip, port
socket.close
socket.connect newIP, newPort
除非插入 RAShangup 和 RASdial, 无法正常工作。相反,它会因 WSAETIMEDOUT 而失败。
在 close 和 connect 调用之间我需要做些什么吗?
注意:我尝试确保关闭调用确实已关闭,但这没有帮助:
Private Sub closeSocket(ByRef w As Winsock)
w.Close
Do While (w.State <> sckClosed)
DoEvents
Loop
End Sub
I'm working on a legacy VB6 app. The app uses the RAS API to establish a modem connection, then uses a winsock control to connect to an IP address & port.
I'm now adding a "failover" feature to this app where if it can't connect to a server, it tries to connect to the next one in the list.
Let's say I have two servers, server app A and server app B. (During my tests I've swapped these around to verify that both servers are accessible and there are no routing / firewall issues.)
If I stop server app A, then something quite strange happens when the app tries to connect to server app A and then server app B:
mySocket.close
mySocket.Connect serverA, portA
- mySocket.state = 6 sckConnecting
- socket connect event is called
- mySocket.state = 7 sckConnected
- mySocket.state = 8 sckClosing
(which seems slightly odd: I'm not sure why it appears to connect for a few moments.)
The socket remains in sckClosing state. After a few hundred milliseconds I move on to try to connect to server B:
mySocket.close
- mySocket.state = 0 sckClosed
.
mySocket.Connect serverB, portB
- mySocket.state = 6 sckConnecting
- socket error event is called with error 10060 (WSAETIMEDOUT)
- mySocket.state = 9 sckError
At this point if I start server app A, do a RAShangup and a RASdial, and try to connect to server A, all works OK.
It's as if the sequence
socket.connect ip, port
socket.close
socket.connect newIP, newPort
doesn't work properly unless a RAShangup and RASdial is inserted. Instead it fails with WSAETIMEDOUT.
Is there anything I need to do between a close and connect call?
Note: I've tried making sure that the close call has really closed, but this doesn't help:
Private Sub closeSocket(ByRef w As Winsock)
w.Close
Do While (w.State <> sckClosed)
DoEvents
Loop
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我注意到关闭端口时出现一些不稳定的行为。我无法一致地重新打开端口。修复:在关闭命令后添加 DoEvents。
I noticed some erratic behavior when closing the port. I could not re-open the port with consistency. The fix: adding a DoEvents after the close command.
再次查看该项目后,发现以前的开发人员将winsock控件的LocalPort属性设置为非零值。这阻止了 winsock 控件的重用。
将其设置为零会导致选择随机本地端口,从而允许重用控件。
经验教训:在处理 VB6 时,我需要查看属性设置以及源代码。
After looking again at the project, it turns out that a previous developer set the LocalPort property of the winsock control to a nonzero value. This was preventing the reuse of the winsock control.
Setting this to zero causes a random local port to be selected, which allows the control to be reused.
Lesson learned: when dealing with VB6 I need to look at the property settings as well as the source code.