Results 1 to 11 of 11

Thread: [RESOLVED] Help with VB6 Network coding! Doesn't go in correct order!

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    [RESOLVED] Help with VB6 Network coding! Doesn't go in correct order!

    Ok I've been using this coding for awhile, the deeper I got into it I realized that it doesn't do everything in order.

    VB Code:
    1. Private Sub WStcpClient_DataArrival(ByVal bytesTotal As Long)
    2.     Static sBuffer As String
    3.     Dim Data As String, DataArray() As String, ComArray() As String
    4.     Dim i As Integer, j As Integer, ComI As Integer
    5.     'Get the data from WinSock, Seperate Packets and add each packet (A Command) to the Command Array
    6.     WStcpClient.GetData Data
    7.     Data = sBuffer & Data
    8.     ComArray = Split(Data, packetDelimiter)
    9.     'Save the stuff to carry over to the next event
    10.     sBuffer = ComArray(UBound(ComArray))
    11.     'Loop Through All Commands (Packets) Recieved
    12.     'The last entry is incomplete or "" so ignore it
    13.     For ComI = 0 To UBound(ComArray) - 1
    14.     'Error Handle, If No Data then go to next
    15.         If ComArray(ComI) <> "" Then
    16.             'Stop Loop Hogging resources
    17.             DoEvents
    18.             MsgBox ComArray(ComI) 'VIEW INPUT DATA FOR DEBUG
    19.             'Now Split the current command (Packet) into the Data Array (Array Of Variables unique to the command code
    20.             ' (The command code is in place Zero of the array and is a two letter code telling the client what command to
    21.             ' process, the remaining places are variables needed to do the command )
    22.             DataArray = Split(ComArray(ComI), packetDelimiter)
    23.             '####Data in Data Array Is Proccesed here####
    24.             Call ProcessData(ComArray(), ComI)
    25.         End If
    26.     Next ComI
    27. End Sub

    Generally it will process it after wards. but lets say it recieves three things to display in a textbox all at once, but sent out are these:

    "1"
    "2"
    "3"

    when you put the debugger on you see it in order, but when it finishes processing in the textbox/etc you will see it in the order backwards more or less, generally what would be displayed is

    "3"
    "2"
    "1"

    This only happens when the coding is sent quicker. Can anyone tell why? I found this out when the NPC was attacking before entering the room, generally when I put the debugger on, you'd see the enter room message being first, then the attacks, but since it was all together, it ended up attacking first then you'd see him enter the room.

    What it seems like more or less it processes the last thing it gets first.. Not sure what to do.
    Last edited by Valleriani; Jan 9th, 2007 at 01:28 AM.

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    Also if someone has sample coding that allows correct sending of data,
    I can't understand why it displays the last coding it recieves first before the rest (GEnerally if they all got there at the same time)

  3. #3
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    perhaps showing us how you're sending the data may reflect on the issue at hand.
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    VB Code:
    1. Public Sub Ostats(ByVal Index As Integer)
    2.     If frmServer.WStcpServer(Index).State <> sckClosed Then
    3.         MsgSend = "~g" & "Welcome to Future Galaxy! There are ~w" & usersonline & "~g users connected and a total of ~w" & userstotal & "~g connections today."
    4.         blnRetVal = WinsockSend(frmServer.WStcpServer(Index), MsgSend)
    5.     End If
    6. End Sub
    7.  
    8. 'Load character room for the first time
    9. Public Sub CharacterWelcome(ByVal Index As Integer)
    10.     Dim i As Integer
    11.    
    12.     If Not rsuserdata.EOF Then rsuserdata.MoveFirst ' make sure your on the first one
    13.     rsuserdata.FindFirst "Character='" & users(Index) & "'" 'Find the username
    14.     userroom(Index) = roomidnumber(userroom(Index)) 'Room Number
    15.     Call Ostats(Index)
    16.    
    17.     'If joining the game say this
    18.     For i = frmServer.WStcpServer.LBound + 1 To frmServer.WStcpServer.UBound
    19.     If frmServer.WStcpServer(i).State <> sckClosed Then
    20.         If userroom(i) = userroom(Index) Then 'In the room
    21.             If Not users(i) = users(Index) Then 'User typed
    22.                 MsgSend = "~g" & users(Index) & " has just entered the game!"
    23.                 blnRetVal = WinsockSend(frmServer.WStcpServer(i), MsgSend)
    24.             End If
    25.         End If
    26.     End If
    27.     Next
    28.    
    29.     Call CheckRoom(Index)
    30.     Call CharacterRoomA(Index)
    31. End Sub

    Generally the CharacterWelcome goes first. It will call ostats though. Generally CaracaterRoomA is where it sends more data, it sends this after getting some quick data:
    VB Code:
    1. If firstchar = 1 Then
    2.             If frmServer.WStcpServer(Index).State <> sckClosed Then
    3.                 MsgSend = "¿csdl¶" & Sidelist
    4.                 blnRetVal = WinsockSend(frmServer.WStcpServer(Index), MsgSend)
    5.                 MsgSend = "~w<~g" & roomtitle(userroom(Index)) & "~w> " & roomdescription(userroom(Index)) & " " & Charsinroom & " is here with you. There is a " & LCase(itemsinroom) & " here."
    6.                 blnRetVal = WinsockSend(frmServer.WStcpServer(Index), MsgSend)
    7.             End If

    What happens there is it sends the room data right after the Ostats to the player that joins.

    Oh, and WinsockSend is how it sends it:

    VB Code:
    1. Public Function WinsockSend(pSock As Winsock, ByVal Data As String) As Boolean
    2.   ' send the data on the passed winsock
    3.   If pSock.State = sckConnected Then
    4.     ' send the data and return true
    5.     pSock.SendData Data & packetDelimiter
    6.     DoEvents
    7.     WinsockSend = True
    8.   Else
    9.     ' return false because we're not connected
    10.     WinsockSend = False
    11.   End If
    12. End Function

    The packetdelim is 'packetDelimiter = Chr$(0)'

    ---

    This is what it looks like in a text box (SAMPLE) on connect:

    Welcome to Future Galaxy! There are 1 users connected and a total of 1 connections today.
    <Dark Room> You are in a dark room. You can see faint light coming from a door to the south.

    But sometimes it will do this, this is for any data, but this is a sample. Again this would be on connect.

    <Dark Room> You are in a dark room. You can see faint light coming from a door to the south.
    Welcome to Future Galaxy! There are 1 users connected and a total of 1 connections today.

    As you see it will basicly jump around depending if it was sent to quickly. I don't lose any data, but it seems like its placing the last data recieved on first if two data are send together quickly.

    P.S. Ignore the ~w, ~g, etc. Those are for color sending.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    This happens with any coding, but thats a sample. Generally whats happening is if two coding is sent out at the same time, the last one is displayed first. It should display the FIRST one always and go down the list.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    Bringing this out the grave again because I still cannot figure it out!

    Does anyone have any network coding that I could take a look at that handles multiple clients?

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    This might help.

    It isn't perfect but it does address a lot of the common mistakes people make in TCP Winsock control programs. In particular it addresses "the packet fallacy" common to a lot of TCP programs. Remember, a TCP application does NOT have some sort of "packet" interface to the wire, it's a stream. That's the whole point of TCP! You are reading and writing streams, NOT packets.

    This sort of "leet speak" leads a lot of programmers astray.

    See item 20 in The Lame List.

    What you want to send and receive are called frames or messages, not packets.
    Attached Files Attached Files

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    Thank you very much, read up on that site :P

    Thanks for the example as well. I was taught packets, thats why I say it. But glad you corrected me. Seems better that way.. :P

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    Quote Originally Posted by Valleriani
    Thank you very much, read up on that site :P

    Thanks for the example as well. I was taught packets, thats why I say it. But glad you corrected me. Seems better that way.. :P
    Did this fix your issue or do you still have questions/problems?

  10. #10
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    In the first post sBuffer was a single Static String that was serving for all connections. This flaw was made much worse by the use of DoEvents() in the DataArrival event handler of course, but was a problem nonetheless.

    The sample code in the ZIP archive I posted corrects this without changing the "style" of the program's buffer handling too much. It uses a String array of buffers, one per connection.

    Do people realize what DoEvents() really does? I wonder. If so they'd be loathe to ever use it in Winsock control programs. A lot of programmers are already in a trance trying to cope with concurrency without adding this Joker to the deck. I suspect its presence is often a desperation measure when nothing else seems to work.

    There are places for DoEvents(), but this just isn't one of them. Here it allows DataArrival for Connection 3 to be interrupted by DataArrival for Connection 7 (or whatever), and on and on, jamming everyone's input into the same sBuffer, etc. It can also lead to a stack overflow if you have several connections or a lot of data arriving. DoEvents() isn't a pause (or much of one). It simply calls the Form's Windows Message Loop once and then gives up its processor slice, like doing a Sleep(0) call, and then returns the oprn Forms count (which most people discard, calling it as if it were a Sub instead of a Function).


    If studying the sample didn't help enough perhaps this explanation will shed a little more light.

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    62

    Re: Help with VB6 Network coding! Doesn't go in correct order!

    Yes hack think its all good now.

    Thank you for the sample and the explanation. I'll definatly be fixing the coding soon and wiping the DoEvents clean out.

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