frogatto
frogatto

Reputation: 29287

Listen() function for Winsock

I am using VB6, and I want to create a chat application that works on a LAN. I used the WinSock control, but when I run the Listen() function my socket just listens on 127.0.0.1 and not my computer's IP on the LAN.

Why? Is there any way to listen on my IP on the LAN?

Upvotes: 2

Views: 10666

Answers (3)

Bob77
Bob77

Reputation: 13267

Normally you would call the Bind method to set the local port and optionally specify the local IP address of the adapter to use. It should default to your system's primary adapter. Then you call Listen with no arguments after that.

You can skip Bind and just set LocalPort then Listen but it isn't advisable except in simple single-connection server scenarios.

None of that explains why your loopback address is being selected by default though. Sounds like some sort of network configuration problem on the box.

Upvotes: 2

Hrqls
Hrqls

Reputation: 2951

you need to set the localport property (and the client needs to connect to that port)

'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

Upvotes: 1

DMI
DMI

Reputation: 7191

I believe you can set the RemoteHost property on the control when listening to determine which network address the server will listen on. To listen on all network interfaces, you may be able to use:

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

Upvotes: 1

Related Questions