我正在使用VB6,我想创建一个在局域网上工作的聊天应用程序。我使用了WinSock控件,但当我运行Listen()函数时,套接字只监听127.0.0.1,而不在局域网上侦听计算机的IP。
为什么?有没有办法在局域网上收听我的IP?
发布于 2012-11-21 22:53:21
通常,您将调用Bind方法来设置本地端口,并可选择指定要使用的适配器的本地IP地址。它应该默认为系统的主适配器。然后调用Listen,之后不带任何参数。
您可以跳过Bind,只需设置LocalPort然后设置Listen,但这是不可取的,除非是在简单的单连接服务器场景中。
但是,所有这些都不能解释为什么默认情况下要选择回送地址。听起来像是盒子上的某种网络配置问题。
发布于 2012-11-21 08:21:43
我相信您可以在侦听时在控件上设置RemoteHost属性,以确定服务器将侦听哪个网络地址。若要在所有网络接口上侦听,您可以使用:
WinSock1.RemoteHost = "0.0.0.0"
WinSock1.Lsten()发布于 2012-11-21 09:09:48
您需要设置localport属性(客户端需要连接到该端口)
'1 form with :
' 1 textbox : name=Text1
' 1 winsock control : name=Winsock1
Option Explicit
Private Sub Form_Load()
Text1.Move 0, 0, ScaleWidth, ScaleHeight 'position the textbox
With Winsock1
.LocalPort = 5001 'set the port to listen on
.Listen 'start listening
End With 'Winsock1
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
With Winsock1
If .State <> sckClosed Then .Close 'close the port when not closed (you could also use another winsock control to accept the connection)
.Accept requestID 'accept the connection request
End With 'Winsock1
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData 'get the data
ProcessData strData 'process the data
End Sub
Private Sub Winsock1_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 Description, vbCritical, "Error " & CStr(Number)
End Sub
Private Sub ProcessData(strData As String)
Text1.SelText = strData 'show the data
End Subhttps://stackoverflow.com/questions/13488925
复制相似问题