From VB to VBA (1 Viewer)

Nsane

New member
Local time
Today, 23:14
Joined
Jun 8, 2015
Messages
4
Hello Guys

I have been given the source code to a little program that i would like to implement into a .adp project. It is a little program that connects to another program on a server: sends and recieves som information. The code is writtin is MS Visual Studio.
I'm very much a beginner but i try the best i can :D

The original code:
Code:
Function Connect(ByVal server As [String], ByVal port As Int32, ByVal message As [String]) As Boolean
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer 
' connected to the same address as specified by the server, port
' combination.
           

Dim oWatch As New Stopwatch
oWatch.Reset()
oWatch.Start()

Dim client As New TcpClient(server, port)
client.ReceiveTimeout = 10000

' Translate the passed message into ASCII and store it as a Byte array.
'Dim sendData As [Byte]() = System.Text.Encoding.UTF7.GetBytes(message)
            
Dim sendData As [Byte]() = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(message)

' Buffer for reading data
Dim bytes(1024) As Byte
' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty

' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()


' Send the message to the connected TcpServer. 
stream.Write(sendData, 0, sendData.Length)


' Receive the TcpServer.response.
' Buffer to store the response bytes.
'data = New [Byte](256) {}

' Read the first batch of the TcpServer response bytes.
            Dim i As Int32
            i = stream.Read(bytes, 0, bytes.Length)
            While (i <> 0)
            responseData = System.Text.Encoding.GetEncoding("iso-8859-                       1").GetString(bytes, 0, i)
                tbSvarText.Text = tbSvarText.Text & responseData
                Console.WriteLine("Received: {0}", responseData)
                i = stream.Read(bytes, 0, bytes.Length)
            End While
            ' Close everything.
            stream.Close()
            client.Close()
Now this is what i have come up with so far:
Code:
 ' Create a TcpClient.
    ' Note, for this client to work you need to have a TcpServer
    ' connected to the same address as specified by the server, port
    ' combination.
    Private WithEvents Winsock1 As Winsock
    
    Sub Init()
        Set Winsock1 = CreateObject("MSWinsock.Winsock")
        Winsock1.RemoteHost = "Localhost"
        Winsock1.RemotePort = "701"
        Winsock1.Connect
    
    ' Translate the passed message into ASCII and store it as a Byte array.
    Dim sendIndhold() As Byte
    Dim i As Integer
    
    
    sendIndhold = StrConv(message, vbFromUnicode)
    For i = 0 To UBound(message)                      
    Next
    
    
    ' Buffer for reading data
    Dim bytes(1024) As Byte
    
    ' String to store the response ASCII representation.
    Dim responseData As String
    responseData = Empty
    
    ' Get a client stream for reading and writing.
    Dim modtaget As Winsock
    modtaget = Winsock.GetData    'Really in doubt here.. Networkstreamer?
    
    ' Send the message to the connected TcpServer. 
    Winsock1.sendData (sendIndhold)

    ' Receive the TcpServer.response.
    ' Read the first batch of the TcpServer response bytes.
    Dim k As Double
    Winsock1.GetData (bytes)
    
    responseData =
This is how far i've gotten.

Can anybody help me correct it and maybe get the rest ?
Any help is wonderful. :rolleyes:
 
Last edited by a moderator:

CJ_London

Super Moderator
Staff member
Local time
Today, 22:14
Joined
Feb 19, 2013
Messages
16,610
please use code tags to preserve the text formatting (i.e. indenting), your code is very difficult to read as presented.

Also, to help, please clarify what problem you are incurring. Provide the line the code fails on and any error message
 

vbaInet

AWF VIP
Local time
Today, 22:14
Joined
Jan 22, 2010
Messages
26,374
Thanks Uncle Gizmo for the edit!
please use code tags to preserve the text formatting (i.e. indenting), your code is very difficult to read as presented.
Nsane, is new here so welcome to AWF! :)

Here's a link on how to use code tags:
http://www.access-programmers.co.uk/forums/showthread.php?t=240420

Re your conversion, here are some pointers:
1. The StopWatch class - Timer() should suffice
2. Encoding - ADODB Stream object
3. Do it in a class - the default constructor doesn't accept parameters so create your own. Destroy the object in the destructor i.e. Terminate() sub.
 

Nsane

New member
Local time
Today, 23:14
Joined
Jun 8, 2015
Messages
4
Hey guys!

Yes, thank you for the edit!

1. Cool i might use timer() or skip the timeout
2. On Encoding. Is the "ADODB Stream object" instead of the Networkstreamer or the part where is it encoded to ASCII
3. I'm sorry. What do you mean ?

What would you guys use instead of the networkstreamer ?
Line: "Dim stream As NetworkStream = client.GetStream()"

Thank you !
 

vbaInet

AWF VIP
Local time
Today, 22:14
Joined
Jan 22, 2010
Messages
26,374
1. Timer() function or the Sleep API - not that important anyway
2. The Stream object in ADODB allows you to specify the encoding, i.e. iso-8859-1. There's no alternative for the NetworkStream class in VBA. You can perhaps find a dll that does all the serialization for you because VBA doesn't support asynchronous events. However, your Winsock library should contain all the relevant methods for sending data across the network via tcp. Add a reference to the Winsock library and have a look.
3. Don't worry about it, just carry on with how you're working.
 

Users who are viewing this thread

Top Bottom