• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 744
  • Last Modified:

VB6 Winsock Telnet Recieve Problem

Using VB6 and winsock to connect to a router and issue a command, currently I can -

1) Connect to a router
2) Issue a command
3) Disconnect

But what I would like to do it -

1) Connect to a router
2) Issue a command
3) Output the routers response to textfile1
4) Issue another command
5) Output the routers response to textfile2
6) Disconnect

Any ideas???????????
0
souldigital
Asked:
souldigital
  • 3
  • 2
1 Solution
 
ZhaolaiCommented:
Try this code (not tested), but logic should work:


Private bSecond As Boolean

Private Sub Command1_Click()
    bSecond = False
    Winsock1.Close
    Winsock1.Connect "10.10.0.60", 23
End Sub

Private Sub Winsock1_Connect()
    If bSecond = False Then
        Winsock1.SendData "strdata1"
    End If
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String, strFile As String
    strData = String(bytesTotal, Chr$(0))
    Winsock1.GetData strData, vbString, bytesTotal
    If bSecond = False Then
        strFile = "c:\test1.txt"
    Else
        strFile = "c:\test2.txt"
    End If
    WriteToFile strFile, strData
End Sub

Private Sub WriteToFile(strFile As String, strData As String)
    Dim oFSO As New Scripting.FileSystemObject
    Dim oStream As TextStream
    Set oStream = oFSO.OpenTextFile(strFile, ForWriting)
    oStream.Write strData
    oStream.Close
    If bSecond = False Then
        Winsock1.SendData "strdata2"
        bSecond = True
    Else
        Winsock1.Close
    End If
    Set oFSO = Nothing
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
 
souldigitalAuthor Commented:
No does not seem to work, I get the error sub or function not defined.
0
 
ZhaolaiCommented:
Which sub or function not defined?
0
 
souldigitalAuthor Commented:
Private Sub WriteToFile(strFile As String, strData As String)
   
0
 
ZhaolaiCommented:
Try this:

Private Sub WriteToFile(strFile As String, strData As String)
    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Dim oStream As Object
    Set oStream = oFSO.OpenTextFile(strFile, 2)
    oStream.Write strData
    oStream.Close
    If bSecond = False Then
        Winsock1.SendData "strdata2"
        bSecond = True
    Else
        Winsock1.Close
    End If
    Set oFSO = Nothing
End Sub

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now