VB 6.0 - What causes Run time error 40006? - Asked By Eric Kong on 03-Feb-06 10:19 AM

The error message was Run time error "Wrong protocal or connection state for the requested transaction or request". I got this message when i try to retrieved some information from the client. I have manage to established connection to the targeted computer but each time i try to excecude any command to the client i get this message. Please help me. THis is the code to connect
Private Sub Form_Load()
Data1.DatabaseName = App.Path + "\control.mdb"
Dim a As String         'To show out the full name of the login user
a = frmLogin.txtUserid.Text
frmLogin.Data1.Recordset.FindFirst "ID_Pengguna = '" & a & "'"
If frmLogin.Data1.Recordset.NoMatch = False Then
   Text15.Text = frmLogin.Data1.Recordset.Fields("Nama_Penuh")
End If
On Error GoTo ConnectError
sckControlPanel.RemoteHost = RemoteForm.sckIdentifier.RemoteHost
    sckControlPanel.RemotePort = 8005
    sckControlPanel.Connect
    Dim x
    Dim PCName As String
    Dim Command As String
    Dim Msg As String
    Dim i As Integer
 ' Declarations
    Dim intIDX As Integer
    Dim RemoteHost As String
    ' Clear the ListBox
    lstComputers.Clear
    ' Get List of Computers on Network (Type All: Server and Workstation)
    ServerList = EnumServer(SRV_TYPE_ALL)
    ' Loop through all the computers and add them to the listbox
    If ServerList.Init Then
        For intIDX = 1 To UBound(ServerList.List)
            lstComputers.AddItem ServerList.List(intIDX).ServerName
        Next
    End If
Text1.Text = "Jumlah Komputer Aktif  "
ConnectError:
    sckIdentifier.Close
End Sub
And this is the code i try to excecude :-
Private Sub mkom_Click()                'To get the computer info
    If lstComputers.Selected(selectedPC_index) = True Then
        sckControlPanel.SendData "GetUserName#"
            End If
End Sub

Check connection state - Asked By drammer _ on 03-Feb-06 10:39 AM

as the error message implies, the problem lies on the connection, check the state if it opened/connected sucessfully. Also I noticed you have this 
sckControlPanel.RemoteHost = RemoteForm.sckIdentifier.RemoteHost 
are you sure "RemoteForm.sckIdentifier.RemoteHost" is a valid IP address?
Also see this link: http://www.codeguru.com/forum/archive/index.php/t-285208.html

Additionally - Asked By drammer _ on 03-Feb-06 10:43 AM

you should put your code inside the Connect event to make sure the connection is avaialble before issuing any command.
(To moderators, sorry for posting again, I just thought of this after submitting my first reply for this thread, kindly consider it as one. Thanks)

Please guide me - Asked By Eric Kong on 08-Feb-06 11:05 AM

Accually i 'm just a beginner in network programming since this is my final year project so i still don't know much. I got this sample program from VBcode.com. I also not very sure. The original program can run very well but after i try to adapt the coding with mine it just can't function. I'm so desperate now please help me. If anyone out there know what is wrong with this coding Please Help me ok Thanks alot.
Connection has been established - Asked By Eric Kong on 08-Feb-06 11:35 AM
I've manage to establish the connection but i can't seem to get any command out to my client computer. Please give me some guide. Is it because there is some error in the coding.Please give your suggestion. Thanks alot. 
Ps: with that coding i've manage to connect to the client computer. The original coding must enter the computer IP add but i've manage to change that by putting another coding to search the computers connected to the network and i just need to click on the computer name and it will establish the connection but i can't get it to function. An error message Run time error 40006. Please help me!!!
Are you using winsock? - Asked By drammer _ on 08-Feb-06 06:49 PM
If yes can you kindly post your entire code, just those that are needed for connecting and sending message, so I can test it. Regards.
Oh thank you so very much - Asked By Eric Kong on 09-Feb-06 06:16 AM
Option Explicit
Private Declare Function ShellExecuteA Lib "shell32" (ByVal hwnd As Long, ByVal Op As String, ByVal File As String, ByVal params As String, ByVal directory As String, ByVal ShowCmd As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
        x As Long
        Y As Long
End Type
Dim selectedPC_index As Integer
Dim ServerList As ListOfServer
Private Sub CmdSearch_Click()
 Data1.Recordset.MoveLast
    ' Declarations
    Dim intIDX As Integer
    Dim ServerList As ListOfServer
    ' Set MousePointer to Working Pointer
    MousePointer = vbHourglass
    ' Clear the ListBox
    lstComputers.Clear
    ' Get List of Computers on Network (Type All: Server and Workstation)
    ServerList = EnumServer(SRV_TYPE_ALL)
    ' Loop through all the computers and add them to the listbox
    If ServerList.Init Then
        For intIDX = 1 To UBound(ServerList.List)
            lstComputers.AddItem ServerList.List(intIDX).ServerName
        Next
    End If
    ' Restore the MousePointer
    MousePointer = vbDefault
End Sub
Private Sub control_Close()
   Unload Me
End Sub
Private Sub Command1_Click()
RemoteForm.Show
End Sub
Private Sub Command10_Click()
Dim RemoteHost As String
If lstComputers.Selected(selectedPC_index) = True Then
RemoteHost = lstComputers.List(selectedPC_index)
                sckIdentifier.RemoteHost = RemoteHost
                sckIdentifier.RemotePort = 8000
                sckIdentifier.Connect
    Form3.Show
  Else
      Command10.Enabled = False
End If
End Sub
Private Sub Command6_Click()
Refresh
End Sub
Private Sub Command9_Click()
Dim a
Dim cancel
    Beep
    a = MsgBox(" Adakah anda pasti untuk keluar dari sini ", vbQuestion + vbYesNo, "Confirm")
If a = vbYes Then
    Unload Me
    Form4.Show
Else
    cancel = True
End If
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
   If KeyAscii = 27 Then Unload Me
End Sub
Private Sub Form_Load()
Data1.DatabaseName = App.Path + "\control.mdb"
Dim a As String         'To show out the full name of the login user
a = frmLogin.txtUserid.Text
frmLogin.Data1.Recordset.FindFirst "ID_Pengguna = '" & a & "'"
If frmLogin.Data1.Recordset.NoMatch = False Then
   Text15.Text = frmLogin.Data1.Recordset.Fields("Nama_Penuh")
End If
On Error GoTo ConnectError
sckControlPanel.RemoteHost = RemoteForm.sckIdentifier.RemoteHost
    sckControlPanel.RemotePort = 8005
    sckControlPanel.Connect
    Dim x
    Dim PCName As String
    Dim Command As String
    Dim Msg As String
    Dim i As Integer
 ' Declarations
    Dim intIDX As Integer
    Dim RemoteHost As String
    ' Clear the ListBox
    lstComputers.Clear
    ' Get List of Computers on Network (Type All: Server and Workstation)
    ServerList = EnumServer(SRV_TYPE_ALL)
    ' Loop through all the computers and add them to the listbox
    If ServerList.Init Then
        For intIDX = 1 To UBound(ServerList.List)
            lstComputers.AddItem ServerList.List(intIDX).ServerName
        Next
    End If
Text1.Text = "Jumlah Komputer Aktif  "
ConnectError:
    sckIdentifier.Close
End Sub
Private Sub GetUserBttn_Click()
 sckControlPanel.SendData "SystemInfo#"
End Sub
Private Sub lstComputers_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
Dim i As Integer
Dim PCName As String
Dim RemoteHost As String
 If lstComputers.Selected(selectedPC_index) = True Then
    If Button = 2 Then            ' if the mousebutton 2 is pressed (right)
      PopupMenu Rclick     ' then it calls the menu which is hidden to be shown
                           ' at the cordinates of the mousepointer (x,y)
            RemoteHost = lstComputers.List(selectedPC_index)
                sckIdentifier.RemoteHost = RemoteHost
                sckIdentifier.RemotePort = 8000
                sckIdentifier.Connect
    Else
            If Button = 1 Then
                sckIdentifier.Close
                Call sckIdentifier_Close
            End If
    End If
  End If
  Data1.Refresh
End Sub
Private Sub lstComputers_Click()
'Data1.Recordset.AddNew
    selectedPC_index = lstComputers.ListIndex
End Sub
Private Sub mskdata_Click() 'Right clik Masukkan Data
If lstComputers.Selected(selectedPC_index) = True Then
    If mskdata.Caption = "Masukkan Data" Then
        Form3.Show                              ' This link to Form 3
        ksistem.Visible = True
    End If
 End If
End Sub
Private Sub ksistem_Click()  'Right Clik Keluar Sistem
If lstComputers.Selected(selectedPC_index) = True Then
    Form3.Data1.Recordset.MoveLast
    Form3.Data1.Recordset.Edit
    Form3.Data1.Recordset.Fields("Masa_Tamat") = " " & Time
 Dim a
 Dim cancel
    Beep
        a = MsgBox(" Penggunaan komputer oleh pelajar akan ditamatkan ", vbQuestion + vbYesNo, "Confirm")
            If a = vbYes Then
                sckIdentifier.Close
                Call sckIdentifier_Close
                Form3.Data1.UpdateRecord
                 ksistem.Visible = False
            Else
                cancel = True
            End If
End If
End Sub
Private Sub loff_Click()         'To log off
If lstComputers.Selected(selectedPC_index) = True Then
    sckControlPanel.SendData "Logoff#"
End If
End Sub
Private Sub sckControlPanel_Close()
    Unload Me
End Sub
Private Sub sckControlPanel_DataArrival(ByVal bytesTotal As Long)
Dim Command As String, txtData As String
Dim Position As Integer
    sckControlPanel.GetData txtData
    Position = InStr(1, txtData, "#")
    Command = Left(txtData, Position - 1)
    txtData = Mid(txtData, Position + 1)
    Select Case Command
        Case "GetUserName"
            MsgBox txtData
        Case "GetTickCount"
            MsgBox Format(txtData, "###")
        Case "SystemInfo"
            MsgBox txtData
    End Select
End Sub
Private Sub sckIdentifier_Close()
If sckIdentifier.State <> sckNotConnected Then
    StatusBar1.Panels(1).Text = "Not Connected   "
End If
End Sub
Private Sub sckIdentifier_Connect()
    Text2.Text = sckIdentifier.RemoteHostIP
    StatusBar1.Panels(1).Text = " Connected with '" & sckIdentifier.RemoteHostIP & "'   "
     Text3.Text = sckIdentifier.RemoteHost
End Sub
Private Sub Text3_Change()
If Form1.Data1.Recordset.NoMatch = True Then
    Data1.Recordset.AddNew
   Text3.Text = Form1.Data1.Recordset.Fields("N_Komputer")
   Text2.Text = Form1.Data1.Recordset.Fields("IP")
Else
End If
End Sub
Private Sub Timer1_Timer()  'To show out the date & time
Text13.Text = " " & Date
Text14.Text = " " & Time
End Sub
Private Sub tutup_Click()        'To shutdown the computer
If lstComputers.Selected(selectedPC_index) = True Then
    sckControlPanel.SendData "Shutdown#"
End If
End Sub
Private Sub kunci_Click()           'To lock the computer
If lstComputers.Selected(selectedPC_index) = True Then
    sckControlPanel.SendData "LockSystem#"
End If
End Sub
Private Sub restart_Click()         'To restart the computer
If lstComputers.Selected(selectedPC_index) = True Then
    sckControlPanel.SendData "Reboot#"
End If
End Sub
Private Sub mkom_Click()                'To get the computer info
    If lstComputers.Selected(selectedPC_index) = True Then
        sckControlPanel.SendData "GetUserName#"
        ' Shutdown your particular pc here
        'MsgBox ("Your PC named " & lstComputers.List(selectedPC_index) & " is shutdown!")
    End If
End Sub
Private Sub MDIForm_Load()
    If App.PrevInstance = True Then
        Unload Me
        Exit Sub
    End If
End Sub
This is my coding. Yes i'm using winsock. One name sckControlPanel and another name sckIdentifier. I have been stuck with this for almost 1 week. If u really can help me then i'm really grateful. Thanks alot.
To James Von - Asked By Eric Kong on 10-Feb-06 10:15 PM
I have post up my entire coding  2 days ago. I hope you can help me with my problems. Thanks alot. Have a good day.