Solved

winsock, senddata & getdata with multiple variables

Posted on 2004-09-14
4
803 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
ID: 12056680
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
ID: 12056772
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
ID: 12057335
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
ID: 12082041
thank you idle_mind, for giving me such detailed information about winsock. First time, someone really explains their answer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Question about INSTR  - need to extract data MS ACCESS query 6 54
Getting warning: You are about to delete 1 row(s) 9 43
Added a column screws up code 5 49
vbModal 12 34
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

911 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

24 Experts available now in Live!

Get 1:1 Help Now