2012-11-21 37 views
1

我正在使用VB6,我想创建一个适用于局域网的聊天应用程序。 我用WinSock控制,但是当我运行Listen()函数时,我的套接字只在127.0.0.1上监听,而不是在LAN上监听我的计算机的IP。Winsock的Listen()函数

为什么?有什么方法可以在局域网上监听我的IP吗?

回答

1

通常你会打电话用于设置本地端口的Bind方法以及可选的指定要使用的适配器的本地IP地址。它应该默认为系统的主适配器。然后,您在此之后调用Listen,不带任何参数。

您可以跳过Bind,只需设置LocalPort,然后Listen,但除简单的单连接服务器方案外,不建议这样做。

这些都不能解释为什么你的环回地址被默认选中。听起来像盒子上的某种网络配置问题。

0

我相信你可以在控件上设置RemoteHost属性,在侦听时确定服务器将侦听哪个网络地址。监听所有网络接口,你可以使用:

WinSock1.RemoteHost = "0.0.0.0" 
WinSock1.Lsten() 
+0

什么是LocalHost属性? –

+0

'LocalHost'是只读的。另外,正如其他评论者所说,请检查您的防火墙设置。 – DMI

0

您需要设置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 Sub 
+0

如果这不起作用,那么你可能需要改变你的防火墙设置 – Hrqls