Solved

winsock, senddata & getdata with multiple variables

Posted on 2004-09-14
4
788 Views
Last Modified: 2013-11-13
ok I have the following problem...

I wanna send 2 strings to client from my server-program, but I don't know how to do it.
If it is possible with real code (not solutions like adding all the data to 1 string and then opening it
up in client-program)

I just wonder why it isn't as easy as

winsock.senddata variable1,variable2
winsock.getdata variable1,variable2
0
Comment
Question by:jozne
  • 2
4 Comments
 

Author Comment

by:jozne
Comment Utility
oh and btw. You don't have to write FULL code, I already have ips ports etc etc in my code. I just wanna know
if there is a way to send 2 or more variables by winsock, or do I have to get used to use only 1 string
and get all the data from it (meaning: 1 string containing 5 names and then the client will insert it from 1 string to 5 strings)
0
 
LVL 32

Expert Comment

by:Erick37
Comment Utility
If you can deal with fixed length strings, here is a technique which may work for you:

HOWTO: Send and Receive UDT's Using the Winsock Control
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q152058
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
Comment Utility
Hi jozne,

    winsock.senddata variable1,variable2

would become

    winsock.senddata variable1
    winsock.senddata variable2

or
 
    tempVariable = variable1 & variable2
    winsock.senddata tempVariable

But the problem with either method is that the winsock control (or anything else using the TCP protocol) simply streams data across the connection.  There is no notion or indication of where one message sent ends and another begins...it is all just one long continous stream of data.  In fact, the following two lines of code:

    winsock.senddata "cat"
    winsock.senddata "dog"

Could result in only ONE data_arrival event on the other side with "catdog" instead of two different arrivals with "cat" and then "dog".  Similarly, the following line:

    winsock.senddata "catdog"

Could result in TWO data_arrival events with "ca" in one and "tdog" in the other.  This is just the way the TCP stream works and there is nothing you can do about it.

The solution is to an EOMM (End of Message Marker) that you can look for in the data stream.  If your data stream consists of simple text, then you could use something like this:

    winsock.senddata "cat" & Chr(0)
    winsock.senddata "dog" & Chr(0)

or

    tempVariable = "cat" & Chr(0) & "dog" & Chr(0)
    winsock.senddata tempVariable

Now it doesn't matter if your message gets broken up or concatenated with another one before it reaches its destination.  You will be able to correctly determine where one message ends and another begins by searching the message stream for the EOMM and extracting the complete messages out.

Because your messages will not always arrive intact, you cannot use this type code in your DataArrival event:

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Dim inputString As String
           
        Winsock1.GetData inputString, vbString
        If inputString = "Login" Then

        End If
    End Sub

This is because you may have only received "Lo" in this event and and the rest, "gin" may arrive in the next event.  Or, you mave have received more than one message at a time.  Since the inputString  variable is declared locally in the DataArrival event, any data from the previous arrival is lost.  The way to handle this type of problem is to use a global buffer to hold your DataArrival stream.  Then, each time you have an arrival you can check if an EOMM has arrived and extract the next complete message.  Here is an example of a protocol that uses Chr(1) as an EOMM and Chr(0) as a message parameter delimiter.  So you could send "Login" & Chr(0) & "Mike" & Chr(1) and then process it on the other side like this:

    Private inputBuffer As String

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Dim inputString As String
           
        ' add msg to buffer and process it
        Winsock1.GetData inputString, vbString
        inputBuffer = inputBuffer & inputString
        processInputs
    End Sub

    Private Sub processInputs()
        Dim markerPos As Integer
        Dim curInput As String
        Dim values As Variant
   
        ' see if we have a msg in our buffer
        markerPos = InStr(inputBuffer, Chr(1))
        Do While markerPos > 0 ' if we have a msg then process it
            curInput = Left(inputBuffer, markerPos - 1) ' get msg from buffer
            inputBuffer = Mid(inputBuffer, markerPos + 1) ' remove msg from buffer
       
            values = Split(curInput, Chr(0)) ' split msg into command and its values
       
            Select Case values(0) ' what command was it?
                Case "Login"
                    MsgBox values(1) ' This should be "Mike"
                                   
                Case Else
                    MsgBox curInput, vbCritical, "Unknown Command"
            End Select
       
            markerPos = InStr(inputBuffer, Chr$(1)) ' any more msgs in buffer?
        Loop
    End Sub

Hope this helps you understand how to create your own message protocol in your application.

Regards,

Idle_Mind
0
 

Author Comment

by:jozne
Comment Utility
thank you idle_mind, for giving me such detailed information about winsock. First time, someone really explains their answer
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now