[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
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
1 Attachment(s)
Re: how do you call a sub using winsock
i uploaded both client and server
Re: how do you call a sub using winsock
thanks lavolpe its working now'
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.
Re: [RESOLVED] how do you call a sub using winsock
Quote:
Originally Posted by
baja_yu
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.
Re: [RESOLVED] how do you call a sub using winsock
thanks ,if this want work good over the internet ,ive got more researching to
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
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.
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.
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
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.
Re: [RESOLVED] how do you call a sub using winsock