Solved

winsock, senddata & getdata with multiple variables

Posted on 2004-09-14
4
845 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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.

717 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