VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Winsock VBScript sample - connect to a remote vbscript socket program, exchange data and disconnect

by Anonymous (267 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 19th February 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Winsock VBScript sample - connect to a remote vbscript socket program, exchange data and disconnect.

Rate Winsock VBScript sample - connect to a remote vbscript socket program, exchange data and disconnect



' SERVER.VBS


' First start server.vbs on server, then start client.vbs on client

' You need the FREEWARE ASocket.dll to run the sample.
' Download it from http://www.vahland.com/pub/asocket.dll
' and register it on your machine.
' Read http://www.vahland.com/pub/asocket.htm for more info

' Constants: Connection states
Const asCONN_DISCONNECTED=1
Const asCONN_LISTENING=2
Const asCONN_CONNECTED=3

' Constants: Error codes
Const asERR_SUCCESS=0

' Constants: Protocols
Const asPROTOCOL_RAW=1
Const asPROTOCOL_TELNET=2

' Create a socket instance
WScript.Echo "Be sure to have the FREEWARE ASocket.dll registered on your system"
WScript.Echo "Check out the code header about how to obtain the component."
Set asObj = CreateObject("ActivXperts.Socket")
asObj.Protocol = asPROTOCOL_RAW

' Write some information to console
WScript.Echo "ActivSocket " & asObj.Version & " demo."

' Try to listen for a connection on port 1500 on this machine
asObj.StartListening 1500
WScript.Echo "StartListening: result = " & asObj.LastError

If asObj.LastError = asERR_SUCCESS Then

    ' Wait for a connection on port 1500 on this machine now...
    WScript.Echo "Waiting for a connection..."

    Do while asObj.ConnectionState = asCONN_LISTENING
        asObj.Sleep 1000
    Loop

    If asObj.ConnectionState = asCONN_CONNECTED Then

        ' YES, connection established.
        WScript.Echo "Connection established" & vbCrLf

        str = ""
        Do While asObj.ConnectionState = asCONN_CONNECTED and str <> "Quit"

            If asObj.HasData Then
                str = asObj.ReceiveString
                WScript.Echo "ReceiveString: " & str
            End If
            asObj.Sleep 100
        Loop

        ' And finally, disconnect
        asObj.Disconnect
    End If
End If



' CLIENT.VBS


' First start server.vbs on server, then start client.vbs on client

' You need the FREEWARE ASocket.dll to run the sample.
' Download it from http://www.vahland.com/pub/asocket.dll
' and register it on your machine.
' Read http://www.vahland.com/pub/asocket.htm for more info

' Constants: Connection states
Const asCONN_DISCONNECTED=1
Const asCONN_LISTENING=2
Const asCONN_CONNECTED=3

' Constants: Error codes
Const asERR_SUCCESS=0

' Constants: Protocols
Const asPROTOCOL_RAW=1
Const asPROTOCOL_TELNET=2

' Create a socket instance
WScript.Echo "Be sure to have the FREEWARE ASocket.dll registered on your system"
WScript.Echo "Check out the code header about how to obtain the component."
Set asObj = CreateObject("ActivXperts.Socket")
asObj.Protocol = asPROTOCOL_RAW

' Write some information to console
WScript.Echo "ActivSocket " & asObj.Version & " demo."

' Make a connection to port 1500 on remote server
asObj.Connect "127.0.0.1", 1500
WScript.Echo "Connect: result = " & asObj.LastError
If asObj.LastError = 11001 Then
    WScript.Echo "Error 11001: Specify a valid hostname-parameter in the Connect-method."
End If
If asObj.LastError = asERR_SUCCESS Then

    ' YES, we established a connection

    WScript.Echo "Connection established" & vbCrLf

    ' Send some strings now

    str = "This is just a message"
    asObj.SendString str, False
    WScript.Echo "SendString '" & str & "': result = " & asObj.LastError
    asObj.Sleep 3000

    str = "And this is another message"
    asObj.SendString str, False
    WScript.Echo "SendString '" & str & "': result = " & asObj.LastError
    asObj.Sleep 3000

    str = "Quit"
    asObj.SendString str, False
    WScript.Echo "SendString '" & str & "': result = " & asObj.LastError
    WScript.Echo "SendString: result = " & asObj.LastError
    asObj.Sleep 3000

    ' And finally, disconnect
    asObj.Disconnect
End If


Download this snippet    Add to My Saved Code

Winsock VBScript sample - connect to a remote vbscript socket program, exchange data and disconnect Comments

No comments have been posted about Winsock VBScript sample - connect to a remote vbscript socket program, exchange data and disconnect. Why not be the first to post a comment about Winsock VBScript sample - connect to a remote vbscript socket program, exchange data and disconnect.

Post your comment

Subject:
Message:
0/1000 characters