Microsoft KB Archive/260018

= BUG: Winsock Control run-time error 10054 in DataArrival event for UDP =

Article ID: 260018

Article Last Modified on 2/12/2007

-

APPLIES TO


 * 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
 * Microsoft Windows 2000 Standard Edition

-



This article was previously published under Q260018



SYMPTOMS
A run-time error occurs when you use network communications on a Windows 2000-based computer with a Winsock Control that uses User Datagram Protocol (UDP), and the Protocol property of the control is set as sckUDPProtocol.

If a SendData request is sent to a RemoteHost that is not listening on the RemotePort, the DataArrival event fires and indicates that 1 byte of data is available. When you call the GetData method to retrieve that data, the following Microsoft Visual Basic run-time error occurs:

'10054' - &quot;The connection is reset by remote side&quot;.

Normally, you can capture the error by using the Error event of the Winsock Control. However, even when the Error event is present for the control, the Error event never fires and a run-time error message box displays.



CAUSE
When a UDP packet is sent to a remote computer and that computer is not listening on the port specified, the Internet Control Message Protocol (ICMP) reports this as an ICMP Port Unreachable error, which is then translated to the Windows 2000 Winsock control as error 10054.

The Winsock Control incorrectly handles the error, causing it to pass to the Visual Basic run-time where the error is subsequently caught and displayed.



RESOLUTION
To work around this problem, use the Microsoft Visual Basic Standard Error Handling mechanism to catch the error. This workaround is illustrated in the following code snippet: Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim szData As String

On Error Resume Next Winsock1.GetData szData If Err Then ' handle the error here End if End Sub Use the On Error method around the GetData method of the Winsock Control to capture the error.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to reproduce the behavior
 Create a new project. Add the Winsock Control to the project. Place a Winsock Control on the form.  Add the following code in the Load event of the form: Private Sub Form_Load Winsock1.Protocol = sckUDPProtocol Winsock1.RemoteHost = &quot;localhost&quot; Winsock1.RemotePort = 8377   ' pick port that is not listening Winsock1.SendData &quot;Hello World!&quot; End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim szData As String Winsock1.GetData szData      ' error occurs here. 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 &quot;Error&quot; End Sub 

If you run this code on a Windows 2000-based computer, the Winsock1_Error handler is never called even though a Winsock Control error occurred.

The Visual Basic error handler is able to trap and handle the error if you use the work around described in the &quot;Resolution&quot; section.

NOTE: The run-time error does not occur if you run the same code on a Microsoft Windows 98-based computer or a Microsoft Windows NT-based computer.

<div class="references_section">