Winsock error 40006 when sending data

Posted on 2005-03-14
Medium Priority
Last Modified: 2013-11-13

i've got a vb application that does chat. however when sending a message out from a remote client to remote client I get the error 40006.

when i chat from client running on the server PC to my remote client i can talk no worries, but when its me and another remote client talking together we get that error. we have both disabled our firewalls and forwarded the relevant port (even tried port forwarding all ports), but the same problem still occurs.

I noticed when debugging on my local client, the client thinks my friends client ip is 192.168.x.x. which is of course a local IP.  But I cant figure out why that is....

Any ideas?

heres the code in question, which is executed on the client after user types in message and clicks Send button.

    If (remote_window_closed = False) Then
        tx = mainform.Text1.Text & " : " & typeTxt.Text
        ChatWsk.SendData tx
        typeTxt.Text = ""
Question by:blueskies77

Expert Comment

ID: 13533275

Author Comment

ID: 13533745
will keep following it, nothing yet to help me though...

anyone else please help?
LVL 27

Accepted Solution

BigRat earned 2000 total points
ID: 13534077
Knowledge Base  

PRB: SendData Method Generates Error 40006PSS ID Number: 183987

Article Last Modified on 4/18/2003

The information in this article applies to:

Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Enterprise Edition for Windows 5.0


This article was previously published under Q183987
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."
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.
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.
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
      End Sub

      Private Sub Form_Unload(Cancel As Integer)
      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

Execute the program. You should get error 40006 after entering a valid server name in the input box. When it fails, click on END.
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."
Keywords: kbAPI kberrmsg kbnetwork kbprb kbWinsock KB183987
Technology: kbAudDeveloper kbVB500 kbVB500Search kbVBSearch kbZNotKeyword2 kbZNotKeyword6


Send feedback to Microsoft

© 2003 Microsoft Corporation. All rights reserved.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13535883
Hi blueskies77,

Show us the code that connects the client to the server.

Often times for developing purposes we hard code the IP and then forget to change it:

Private Sub Form_Load()
    Me.Caption = "Client - Connecting..."
    IP = ""
    port = 10001
    ' attemp to connect to the server
    Winsock1.Connect IP, port
End Sub
LVL 27

Expert Comment

ID: 13751661

Sorry, duly noted.
LVL 20

Expert Comment

ID: 13752443

It's ok:)

PS: Your recommendation here? Cheese for BigRat is clear, split probbably with Idle_Mind... Although I am still wondering for the split... Your thoughts?
LVL 27

Expert Comment

ID: 13752586
>>Your thoughts?

Well, that's the standard solution to the standard problem which I copied out of my Developer Search tool (I'm a Universal Subscriber to MSDN which is why I didn't have a web link).

This question is obviously a follow up to http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21348178.html which will also be classified as abandoned on Thursday (Rat's birthday so I won't be here!)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question