Microsoft KB Archive/244751

= PRB: Winsock Control does not Fire Error Event in all Cases =

Article ID: 244751

Article Last Modified on 2/11/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q244751



SYMPTOMS
When using the Winsock Control in Visual Basic, setting the LocalPort property to a port value that is already in use on the local computer raises a Visual Basic Run-time error when the Connect or Bind method is called. The Run-time error is as follows:

'10048' - "Address in use".

Normally this could be captured using the Error Event of the Winsock Control, but even when the Error event is present for the control, the Error event is never fired and the Run-time error message box is displayed.



RESOLUTION
A valid workaround is to use Visual Basic's standard Error Handling mechanism to catch the error as illustrated in the following code snippet: On Error Resume Next Winsock1.Connect If Err then ' handle the error here End if Simply use the On Error method around the Connect or Bind method of the Winsock Control to capture this error.



Steps to Reproduce Behavior

 * Create a New Project.
 * Add the Winsock Control to the Project.
 * Place the Winsock Control on the Form.
 * Add the following code:

Private Sub Form_Load Winsock1.RemoteHost = "localhost" Winsock1.RemotePort = 7  ' connect to echo server from Simple TCP/IP Services available on NT  Winsock1.LocalPort = 80   ' pick a port that is in use... 80 is IIS (http) Winsock1.Connect ' this line of code will force the run-time error even the error event is included below 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) CancelDisplay = True MsgBox "Error" End Sub When this code is run the Winsock1_Error handler is never called even though a Winsock Control error has occurred. Using the work-around mentioned earlier causes Visual Basic's normal error handling to handle the error.

Keywords: kbapi kbnetwork kbprb kbctrl kbdswnet2003swept KB244751

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.