Results 1 to 10 of 10

Thread: Winsock Question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    29

    Winsock Question

    Hey,
    I need help sending a string with winsock and have winsock recieve the string as a string.
    Example:
    Public Sub Hello()
    dim hello as string
    hello = "HI!"
    winsock1.senddata (hello)
    End Sub

    But when winsock1 receives this, it receives "HI!"
    How do I make it so it recieves the string hello with the contents still inside the string?
    Thanks,
    CrazyMonkey

  2. #2
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Winsock Question

    You can't send the actual named variable, if that's what you mean. But all you have to do to send a string is this:
    Winsock1.SendData "This is some text haha 1 2 3"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    29

    Re: Winsock Question

    um...
    I'll give you an example of what I need.
    I'm making a multiplayer game in vb6 and I want it so when a player moves, winsock sends
    image3.top as 3top
    image3.left as 3left

    and when winsock receives it, it makes image3.top = 3top
    and image3.left = 3left

    Thanks,
    CrazyMoneky

  4. #4
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Winsock Question

    OK, you could do something like this:
    Code:
    Winsock1.SendData "3top " & Image3.Top
    Then in the receiving side:
    Code:
    Winsock1.GetData mystring
    Select Case Split(mystring, " ")(0)
    Case "3top"
        Image3.Top = Split(mystring," ")(1)
    Case 'other
    
    End Select
    Now, this is just a simple way to get started, if you have a large number of things to update, you will want a better way to handle it. You can probably send the actual code such as "Image3.Top = " & Image3.Top then parse it on the other end and run CallByName on it. But this is more involved and you should be fine with the method I gave above for now.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    29

    Re: Winsock Question

    Can you please explain the code that you gave me.
    So I understand it.
    Thanks,
    CrazyMonkey

  6. #6
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Winsock Question

    Of course. What this does is grab the first portion of the string which ends at the first space. That's what Split is doing. So you check that and it finds "3top" so then we know that we'll need to change Image3.Top so we do that using the second portion of the string, which is the Image3.Top value from the sender. Just to illustrate the Split function, if you have the following data:
    Code:
    str = "This is a string."
    Then you do this:
    Code:
    Dim s() As String 'this creates a dynamic array, which simply means a group of variables that can change at runtime
    s = Split(str, " ") 'now the data is split up into different variables, using the space character as the split point
    So now the s() array looks like this:
    Code:
    s(0) = "This"
    s(1) = "is"
    s(2) = "a"
    s(3) = "string."
    Hope this helps.

    Edit: The way I use Split in my winsock example above is just a shortcut version of it. Instead of putting it in the s() array I am using it directly, so I write Split(somevar, " ") and add the number of which piece I want in parentheses on the end: Split(somevar, " ")(0) or Split(somevar, " ")(1) etc.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    29

    Re: Winsock Question

    Thanks =)

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    29

    Re: Winsock Question

    Sorry for the double post.
    Hey thanks for the help.
    Do I have to give credits to you in my game if I use what I learned from you?

  9. #9
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: Winsock Question

    I appreciate you asking but no, not at all. If I had written a large part then I may like credit but just some snippets, feel free to use how you want!

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    29

    Re: Winsock Question

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width