Results 1 to 13 of 13

Thread: [RESOLVED] how do you call a sub using winsock

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Resolved [RESOLVED] how do you call a sub using winsock

    when i type ng and send it .ng shows up in view.txt box..
    but it doesnt call the sub test1 .
    thanks

    Code:
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim msg As String
    Winsock1.GetData msg
    view.Text = view.Text & "Server: " & msg & vbCrLf
    
    '// Code for the game
        Select Case Left(msg, 2)
        
        Case "NG"
            'Call sub_reset_game
             Call sub_test1
        Case "PX"
            'Call sub_place_XorO(msg)
        
        Case "PO"
            'Call sub_place_XorO(msg)
            
        Case "OG"
            'Call sub_WhoAmI(msg)
            
        Case "TR"
            'Call sub_MyTurnOnNot(msg)
        End Select
    
    End Sub

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: how do you call a sub using winsock

    Try: Select Case UCase(Left(msg, 2))

    You said you sent it .ng or did you mean ng with not dot
    If there's a dot, you have to adjust for that
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: how do you call a sub using winsock

    i uploaded both client and server
    Attached Files Attached Files

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: how do you call a sub using winsock

    thanks lavolpe its working now'

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] how do you call a sub using winsock

    A little debugging goes a long way. If you've just put this line: MsgBox Left(msg, 2) before select case you would have noticed the problem immediately.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] how do you call a sub using winsock

    Quote Originally Posted by baja_yu View Post
    If you've just put this line: MsgBox Left(msg, 2) before select case you would have noticed the problem immediately.
    Actually,if you'd just looked at the contents of the view TextBox you'd have seen the problem.

    Some observations:

    If the data is going to be input by the user you can't necessarily rely on them entering the 'command' in Upper Case. Under those circumstances I'd use (EDIT: which I just noticed has been suggested earlier)
    Code:
    Select Case UCase(Left$(msg, 2))
    then, whatever case the user uses it will match correctly.

    Your code will probably work OK when you're running it on your own Machine. However, if you intend to use it over a local network or the Internet you'll have to introduce some buffering in the DataArrival events. This will require you to also introduce an Application Protocol (basically, the 'rules' that the Client and Server use to communicate with each other). Sending and Receiving data with Winsock (TCP/IP) is not the same as, say, reading and writing files where you can read data a record at a time.

    With TCP/IP, data will arrive in 'bits and pieces' - one SendData from one end does not necessarily result in one DataArrival at the other end, there may be more than one before the complete message is received. So, the question is, 'How do I know when the complete message has been received?", that's where the Application Protocol comes in; a simple example would be to have a 'rule' that says 'every message sent by either end must always end with a Carriage Return'. Then, in your DataArrival events you buffer the data received until you get a Carriage Return, you then know that the complete message has arrived and you can process it.

    I posted a very simple 'chat' example here: http://www.vbforums.com/showthread.p...hlight=winsock you may wish to take a look at the DataArrival events to see one method of implementing buffering. Although at the moment you're not sending and receiving much data, I suspect as you develop the applications, you will be in the future, and, as I said before, it will probably work 'perfectly' on your Machine but when you try to use it over the Internet (or any other Network) you'll start to get unexpected errors and 'missing data'. If you implement buffering and an Application Protocol now it will save you considerable debugging time (not to mention frustration) in the future.

    Finally, in the code attached in your post earlier you call sub_test which doesn't exist. I suspect you mean to call test.

    Good luck.
    Last edited by Doogle; Jan 3rd, 2011 at 12:17 AM. Reason: Typos

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: [RESOLVED] how do you call a sub using winsock

    thanks ,if this want work good over the internet ,ive got more researching to

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: [RESOLVED] how do you call a sub using winsock

    hey doogle
    do i put this Select Case UCase(right(strData, 2))
    in after loop?
    and if i look for ng on the right is that the right way?
    thanks

    Code:
    Private Sub wsClient_DataArrival(ByVal bytesTotal As Long)
    '
    ' The Server has sent a message to the Client
    ' Unblock it and display in the TextBox
    ' The message can come in 'bits and pieces' so we have
    ' to buffer it until we get an end-of-record character
    ' in this case it's a vbCr
    '
    Static strBuffer As String
    Dim boComplete As Boolean
    Dim strData As String
    Dim intPos As Integer
    wsClient.GetData strData
    strBuffer = strBuffer & strData
    Do
        intPos = InStr(strBuffer, vbCr)
        If intPos > 0 Then
            txtClient.Text = txtClient.Text & Mid(strBuffer, 1, intPos - 1)
            txtClient.Text = txtClient.Text & vbCrLf
            '
            ' Is there anything else in the buffer
            '
            If intPos + 1 >= Len(strBuffer) Then
                '
                ' No we've processed all the data
                ' Flush the buffer and signal to exit the loop
                '
                strBuffer = ""
                boComplete = True
            Else
                '
                ' Something left in the Buffer
                ' Move it to the front and go round the
                ' loop again
                '
                strBuffer = Mid(strBuffer, intPos + 1)
            End If
        Else
            boComplete = True
        End If
    Loop Until boComplete = True
    
    '// Code for the game
        Select Case UCase(right(strData, 2))
    
        
        Case "NG"
        
            'Call sub_reset_game
             Call test1
        Case "PX"
            'Call sub_place_XorO(strData)
        
        Case "PO"
            'Call sub_place_XorO(msg)
            
        Case "OG"
            'Call sub_WhoAmI(msg)
            
        Case "TR"
            'Call sub_MyTurnOnNot(msg)
        End Select
    
    
    
    
    End Sub

  9. #9
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] how do you call a sub using winsock

    The best way to find out is to set for yourself. Put a test string in strData and see what UCase(right(strData, 2)) returns.

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] how do you call a sub using winsock

    In case you're still stuck......
    Code:
    Option Explicit
    
    Private Sub wsClient_DataArrival(ByVal bytesTotal As Long)
    '
    ' The Server has sent a message to the Client
    ' Unblock it and display in the TextBox
    ' The message can come in 'bits and pieces' so we have
    ' to buffer it until we get an end-of-record character
    ' in this case it's a vbCr
    '
    Static strBuffer As String
    Dim boComplete As Boolean
    Dim strData As String
    Dim intPos As Integer
    wsClient.GetData strData
    strBuffer = strBuffer & strData
    Do
        intPos = InStr(strBuffer, vbCr)
        If intPos > 0 Then
            strData = Mid(strBuffer, 1, intPos - 1)
            '// Code for the game
            Select Case UCase(Left$(strData, 2))
                Case "NG"
        
                'Call sub_reset_game
                Call test1
                Case "PX"
                'Call sub_place_XorO(strData)
        
                Case "PO"
                'Call sub_place_XorO(msg)
            
                Case "OG"
                'Call sub_WhoAmI(msg)
            
                Case "TR"
                'Call sub_MyTurnOnNot(msg)
            End Select
            '
            ' Is there anything else in the buffer
            '
            If intPos + 1 >= Len(strBuffer) Then
                '
                ' No we've processed all the data
                ' Flush the buffer and signal to exit the loop
                '
                strBuffer = ""
                boComplete = True
            Else
                '
                ' Something left in the Buffer
                ' Move it to the front and go round the
                ' loop again
                '
                strBuffer = Mid(strBuffer, intPos + 1)
            End If
        Else
            boComplete = True
        End If
    Loop Until boComplete = True
    End Sub
    If you study the code you'll see that the message sent is extracted out of strBuffer and into strData, via the Mid$ statement.

    The Select is looking at the first two characters sent; (Left$(strData,2)) so, if the message was ng<cr> subroutine test1 will be called.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: [RESOLVED] how do you call a sub using winsock

    good morning and i had put it after loop, does it matter about that?
    thanks

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] how do you call a sub using winsock

    Hi,

    Yes it does matter. If you do it after the loop, strData may not contain the whole message. It's best to put your code where I have put it in my last post.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: [RESOLVED] how do you call a sub using winsock

    thanks will do

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