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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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.
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 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