Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 28th, 2003, 04:01 PM
Registered User
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Runtime error 40006 - After connection established

Hi

I have set up a client server application, where the client connects to a
server and the client application can shutdown the server application using
the Windows API.

However I have tried sending the API to the machine where the server program
is hosted, via a string, then allowing the server to receive the incoming
data and calling the API.

However i get the following error:

Run-time error '40006'
Wrong protocol or connection state for the requested transaction or request.

Does anyone know what this means and how i should correct my code?

Client Code:

Option Explicit

Private Sub cmdAction_Click()
Actions.Visible = True
End Sub

Private Sub cmdClose_Click()
wsRat.Close
cmdClose.Enabled = False
cmdConnect.Enabled = True
End Sub

Private Sub cmdConnect_Click()

If txtIP.Text = "" Then
MsgBox "Please Enter IP Address Before Clicking Connect", vbCritical,
"Error"

End If

wsRat.Close
wsRat.Connect txtIP.Text, 1234

cmdClose.Enabled = True

End Sub


Private Sub wsRat_Connect()
Do

DoEvents

Loop Until wsRat.State = sckConnected Or wsRat.State = sckError

If wsRat.State = sckConnected Then

'inform user that connection is made

MsgBox "Connection Established", vbInformation, "Client"

cmdClose.Enabled = True

Else

'inform user that connection failed

MsgBox "Connection Failed!", vbCritical, "Error!"


End If
End Sub

Private Sub wsRat_ConnectionRequest(ByVal requestID As Long)
wsRat.Close
wsRat.Accept requestID

'if the remote system requests a connection then accept it and connect

End Sub

Option Explicit


'********************
'* CD Tray Declares *
'********************

Dim strReturn As Long
Dim lngReturn As Long
Dim incoming As String

Private Declare Function mciSendString Lib "winmm.dll" Alias
"mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As
String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

'*********************
'* Lock Wks declares *
'*********************

Private Declare Function LockWorkStation Lib "user32.dll" () As Long
Dim lockwrk As String


'*************************
'* Exit Windows declares *
'*************************

Private Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As
Long, ByVal dwReserved As Long) As Long

    Private Const EXIT_LOGOFF = 0
    Private Const EXIT_SHUTDOWN = 1
    Private Const EXIT_REBOOT = 2
Private Sub cmdExitform_Click()
Actions.Visible = False
Client.Visible = True
End Sub

Private Sub cmdOpenCD_Click()

wsRat.SendData "[open]"
MsgBox "CD Opened", vbInformation, "Note:"

End Sub

Private Sub cmdCloseCD_Click()

wsRat.SendData "[close]"
MsgBox "CD Closed", vbInformation, "Note:"

End Sub

Private Sub cmdLW_Click()

wsRat.SendData "[lockwrk]"
MsgBox "Locked Host Computer", vbInformation, "please wait:"

End Sub

SERVER CODE:

Option Explicit

'********************
'*CD Tray Properties*
'********************

Dim strReturn As Long
Dim lngReturn As Long

Private Declare Function mciSendString Lib "winmm.dll" Alias
"mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As
String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

'********************
'*Lock Wks declares:*
'********************

Private Declare Function LockWorkStation Lib "user32.dll" () As Long


'***********************
'*Exit Windows declares*
'***********************

Private Declare Function ExitWindowsEx Lib "user32.dll" (ByVal uFlags As
Long, ByVal dwReserved As Long) As Long

    Private Const EXIT_LOGOFF = 0
    Private Const EXIT_SHUTDOWN = 1
    Private Const EXIT_REBOOT = 2

Private Sub cmdClose_Click()
wsRat.Close

MsgBox "Listening Disabled", vbExclamation, "Please Note:"

cmdClose.Enabled = False
cmdListen.Enabled = True

End Sub

Private Sub cmdListen_Click()

MsgBox "Awaiting Connection Attempts", vbInformation, "Please Wait!"

wsRat.Close
wsRat.LocalPort = 1234
wsRat.Listen
cmdClose.Enabled = True
cmdListen.Enabled = False

End Sub

Private Sub wsRat_Connect()
Do

DoEvents

Loop Until wsRat.State = sckConnected Or wsRat.State = sckError

If wsRat.State = sckConnected Then

'inform user that connection is made

MsgBox "Connection Established", vbInformation, "Client"

cmdClose.Enabled = True

Else

'inform user that connection failed

MsgBox "Connection Failed!", vbCritical, "Error!"



End If
End Sub
Private Sub wsRat_ConnectionRequest(ByVal requestID As Long)
wsRat.Close
wsRat.Accept requestID

'if the remote system requests a connection then accept it and connect

cmdListen.Enabled = False

End Sub

Private Sub wsRat_DataArrival(ByVal bytesTotal As Long)

Dim incoming As String

' tell winsock to place the incoming data as a string

wsRat.GetData incoming

If incoming = "open" Then

    Call openCD

Else

If incoming = "lockwrk" Then

    Call LockWS

Else

If incoming = "close" Then

Call closeCD

End If

End Sub

Private Sub LockWS()

LockWorkStation

End Sub

Private Sub openCD()

lngReturn = mciSendString("set CDAudio door open", strReturn, 127, 0)

End Sub

Private Sub closeCD()

lngReturn = mciSendString("set CDAudio door closed", strReturn, 127, 0)

End Sub


Thanks
B



 
Old November 29th, 2003, 05:05 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 336
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alyeng2000
Default

the error 40006 is generated due to socket is not connected yet so check for connection status before using senddata method

Ahmed Ali
Software Developer





Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection String setting at runtime TwoTrees BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 3 July 2nd, 2010 11:41 AM
changing connection string at runtime kscase VB Databases Basics 2 July 3rd, 2007 10:47 AM
Specify the connection string property at runtime Lawrence C. Zauberis BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 1 July 7th, 2006 09:40 AM
Setting Runtime DB connection from JSP ssivakumar76 BOOK: Beginning Java 2, JDK 5 Edition 0 June 16th, 2006 03:07 AM
Connection coudn't be established to the local hos rraguraman JSP Basics 1 February 13th, 2005 01:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.