Solved

VB6 Telnet Winsock Run-Time Error '40006'

Posted on 2010-08-18
10
1,593 Views
Last Modified: 2012-05-10
I am using the following code -

Private Sub Connect_Click()
    Winsock1.Connect ("192.168.1.1"), 23
    Winsock1.SendData "admin" & vbCrLf
End Sub

and basically get the error - Run-Time Error '40006'

I can avoid this error by having two seperate buttons, one to connect and one to issue the SendData command, or by using the following function -

Private Sub Winsock1_Connect()
   Winsock1.SendData "admin" & vbCrLf
End Sub

But I do not want to use either of these methods, I just want everything to be included in the one button.

Please help!
0
Comment
Question by:souldigital
  • 5
  • 4
10 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33470184
You have to wait for the connection to be completed before sending data.
Try this:


Private Sub Connect_Click()

    Winsock1.Connect ("192.168.1.1"), 23

    Do While Winsock1.State = sckConnected

        Winsock1.SendData "admin" & vbCrLf

        Exit Do

    Loop

End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33470203
Oooop, a mistake in the loop.
Try this:


Private Sub Connect_Click()

    Winsock1.Connect ("192.168.1.1"), 23

    Do While True

        If Winsock1.State = sckConnected Then

            Winsock1.SendData "admin" & vbCrLf

            Exit Do

        End If

    Loop

End Sub

Open in new window

0
 

Author Comment

by:souldigital
ID: 33470241
This loop creates 100% cpu usage and crashes VB  :-s
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33470261
Aha, I don't think that the code in my above posts will work.
Please refer to the following link:

http://kbalertz.com/Feedback.aspx?kbNumber=183987

It looks like that you have to call the Connect event to send data.
Sorry for the confusion.
0
 

Author Comment

by:souldigital
ID: 33470303
This is the same as using 2 buttons, one to connect and one to execute the command, but instead of using a button to connect it does it on form load which sadly is no good.

??????????????????????????????
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:Zhaolai
ID: 33471513
No, you don't need to have two buttons. The Connect event will fire automatically once the connection is complete.

All you need is a button to initiate the connection. See below:




Private Sub Connect_Click()

    Winsock1.Connect ("192.168.1.1"), 23

End Sub



'This event fires automatically once the connection is complete.

Private Sub Winsock1_Connect()

   Winsock1.SendData "admin" & vbCrLf

End Sub



'You may need the following event to capture any connection error



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)

    MsgBox "Error occurred: " & Description, vbExclamation, "Connection Error"

End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 33472048
either follow above comment by using the default event _Connect

or

if you sure that the "server" you try to connect to always ready to accept connection.
you may put some delay after you call for connect

' this code, put on top at global variable declare
Private Declare Sub Sleep Lib "Kernel32.dll" (ByVal dwMilliseconds As Long)


Private Sub Connect_Click()
   Winsock1.Connect ("192.168.1.1"), 23

    for i = 1 to 100
       sleep 1   ' try adjust number of loop, or adjust number of sleep time to match your need
       doevents
   next

    Winsock1.SendData "admin" & vbCrLf
End Sub
0
 

Author Comment

by:souldigital
ID: 33473369
Yes it will fire when the connection is complete but I need to issue different commands depending on which button I click.

Button A - Winsock1.SendData "admin" & vbCrLf

Button B - Winsock1.SendData "tx power" & vbCrLf

Button C - Winsock1.SendData "help" & vbCrLf

and so on

So I need it to connect, send the data then disconnect all in one button without using any Sub Winsock1_Connect()

I have tried a timer before with a 10second pause and it still gives the same error. It is almost as if you have to End Sub before the connection is fully established?

I can literally click connect (Button 1) then send a command (Button 2) with no delay and it works fine.
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33474921
Now I see why you want to have all the code in one sub... But unfortunately that's not the way it designed.
Try the following code:


Private iButton As Integer



Private Sub Command1_Click()

    iButton = 1

    ConnectWinsock

End Sub



Private Sub Command2_Click()

    iButton = 2

    ConnectWinsock

End Sub



Private Sub Command3_Click()

    iButton = 3

    ConnectWinsock

End Sub



Private Sub ConnectWinsock()

    Winsock1.Close

    Winsock1.Connect "192.168.1.1", 23

End Sub



Private Sub Winsock1_Connect()

    Select Case iButton

        Case 1: Winsock1.SendData "admin" & vbCrLf

        Case 2: Winsock1.SendData "tx power" & vbCrLf

        Case 3: Winsock1.SendData "help" & vbCrLf

    End Select

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)

    MsgBox "Error occurred: " & Description

End Sub

Open in new window

0
 

Author Comment

by:souldigital
ID: 33478214
Perfect, thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now