VBGamer |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Re: How to pass multiplayer data though winsock? Mattman (8 replies, 0 views) (2001-Jun-27) ok, you have the sending pretty much correct...you use a delimeter (in this case you're using Chr$(127)...though I prefer Chr$(0)). However, you do not want to use the Split routine to break apart the data. Let's say you send the following data:
"Mattman: Hey, guys!" & Chr$(127) & "YellowSheep: What's up??"
Now the way you're currently receiving data is that you're expecting that you're expecting it to arrive all at once, just as you sent it. If that were true, I could combine 25 megs worth of characters, send it, and expect it all to arrive at once, which as we all know doesn't happen...yet. ;) Here are some possible ways the data will be received:
1. "Mattman: Hey, guys!" & Chr$(127) & "YellowSheep: What's up??"
2. A chunk of "Mattman: Hey, g", followed by a chunk of "uys!" & Chr$(127) & "YellowSheep: What's up?"
3. A chunk of "Mattman: Hey, guys!" & Chr$(127) & "Ye", followed by a chunk of "llowShee", followed by a chunk of "p: What's up??"
Unfortunately, splitting the data with the Split command is impractical, as it may not all have arrived yet. (Data can also be chunked together when you don't want it to, so watch for that too.) Here's a sample of how to handle the incoming data instead:
<PRE>
' ----------------------------------------------------
' This must be either a PUBLICLY or STATICALLY declared string!!!
Public sCurrentData As String ' Or Static sCurrentData As String
' ----------------------------------------------------
' This code must go within the DataArrival routine.
Dim sNewData As String
Dim sSplitData As String
Dim nDelimeter As Long
' Get the new data.
sckWinsock.GetData sNewData, vbString
sCurrentData = sCurrentData & sNewData
' Locate the first delimeter mark.
nDelimeter = InStr(sCurrentData, Chr$(127))
' Loop while there is a delimeter.
While nDelimeter <> 0
' Parse the code while there is a Chr$(127) delimeter mark.
sSplitData = Left$(sCurrentData, nDelimeter - 1
' Remove the portion of the string that we parsed.
sCurrentData = Right$(sCurrentData, Len(sCurrentData) - nDelimeter)
' Used the parsed segment in some function.
Call Some_Function_That_Uses(sSplitData)
' Find the next location of Chr$(127), the delimeter
nDelimeter = InStr(sCurrentData, Chr$(127))
Wend
' ----------------------------------------------------
</PRE>
Note that I haven't tried this code, and I just wrote it from previous experiences. I have tried to optimise it for as much speed as possible, as VB is not the fastest when it comes to strings. Good luck!
|