Microsoft KB Archive/183987

= PRB: SendData Method Generates Error 40006 =

Article ID: 183987

Article Last Modified on 10/28/2003

-

APPLIES TO


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

-



This article was previously published under Q183987



SYMPTOMS
If you call the SendData method in the WinSock control before the socket has been connected, you will receive the Microsoft Visual Basic Error:

Run-time error: 40006

"Wrong protocol or connection state for the requested transaction or request."



CAUSE
The code is trying to call the SendData method before the port has actually been connected. The Connect method in Visual Basic is asynchronous and is more like a request to connect to the Winsock port.



RESOLUTION
The code must wait until the Connect event is fired before attempting to call the SendData or GetData methods. The Connect event is a signal that the connect request has been accepted and the connection is established.



STATUS
This behavior is by design.



Steps to Reproduce and Work Around Behavior
 Create a new project in Visual Basic 5.0. From the Projects menu, choose Components. Make sure that Microsoft Winsock Control 5.0 is checked, and then click the OK button. Add a Winsock Control to the form and leave it named Winsock1. Add a CommandButton named Command1.  Add the following code to the form: Option Explicit

Private Sub Command1_Click Winsock1.SendData "Hello World." End Sub

Private Sub Form_Load Command1.Enabled = False Winsock1.Connect InputBox$("Enter Server name:"), 7 Command1_Click End Sub

Private Sub Form_Unload(Cancel As Integer) Winsock1.Close End Sub

Private Sub Winsock1_Connect Command1.Enabled = True End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim temp As String temp = String(bytesTotal, Chr$(0)) Winsock1.GetData temp, vbString, bytesTotal MsgBox temp End Sub

</li> Execute the program. You should get error 40006 after entering a valid server name in the input box. When it fails, click on END.</li> Remove the line Command1_click from the Form_Load sub, and run the program again. You should get a message box that says "Hello World."</li></ol>

Keywords: kberrmsg kbwinsock kbapi kbnetwork kbprb KB183987

-

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

© Microsoft Corporation. All rights reserved.