Results 1 to 4 of 4

Thread: set data types in winsock on receive

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    2

    set data types in winsock on receive

    I want to have in my winsock control, it take and send the name of a player for a game, and a value, so a string and an integer. how do i make it in the data arrival to make a part that can receive a name, string, and a part to receive a score, an integer. also, this means i would need the senddata part to, to send a name and then an score.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: set data types in winsock on receive

    Send the data as a UDT. The Winsock control does not understand UDTs, so in your code you will have to move it to/from a byte array in order to send/receive the data. You can use the CopyMemory to move the UDT to/from the byte array.

    For example:
    Code:
    Public Type dclPlayerRec
                PlayerValue As Integer
                PlayerName As String * 20
           End Type
    
    Public Player   As dclPlayerRec       ' the IPC structure
    Public bTBuff() As Byte               ' buffer for sends/receives
    
    Public Declare Sub CopyMemory Lib "KERNEL32" _
                       Alias "RtlMoveMemory" (hpvDest As Any, _
                                              hpvSource As Any, _
                                              ByVal cbCopy As Long)
    Note that the string must be fixed length, otherwise PlayerName will simply contain the BSTR (address) of the string.

    To copy the UDT to/from a byte array, code the API calls are:
    for the send routine
    Code:
       ReDim bTBuff(Len(Player) - 1)
       CopyMemory bTBuff(0), Player, Len(Player)
       tcpClient.SendData bTBuff
    for the receive routine
    Code:
       ReDim bTBuff(bytesTotal - 1)
       tcpServer(Index).GetData bTBuff, vbArray + vbByte, bytesTotal
       CopyMemory Player, bTBuff(0), Len(Player)
    Several other things to consider.

    If the string is very large, you may have to write the DataArrival code to handle receiving the UDT in pieces. Let me know if you need help with that.

    If both client and server are written in VB you should not have any problems with the Integer value. If one side or the other is written in a different language or is running on a non-Windows platform, you may have to convert the Integer to/from network byte order (big-endian) before sending it and after receiving it. There are API calls for converting to/from network byte order.

    For Integers:
    ntohs - network to host short
    htons - host to network short

    For Longs:
    ntohl - network to host long
    htonl - host to network long

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Posts
    2

    Re: set data types in winsock on receive

    well, im pretty sure i do not need to do that much, because their are pletny of programs i have with source that use only winsock, and for example, a chat program i have which sends a name of the chatter witht he connection, and then whatever you type to send

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: set data types in winsock on receive

    I think sending as a UDT like ccoder said is the best way, but the easiest way would be to do something like:

    vb Code:
    1. Winsock.SendData "INFO/" & strPlayerName & "/" & CStr(intScore) & vbCrLf

    Then on the data arrival:
    vb Code:
    1. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    2.     Dim strData As String, strPackets() As String
    3.     Dim lonLoop As Long, strInfo() As String
    4.    
    5.     Winsock1.GetData strData, vbString, bytesTotal
    6.    
    7.     'Split up the "packets".
    8.     'A packet looks like this in this example:
    9.     'INFO/PlayerName/Score
    10.    
    11.     'Where INFO tells the program what kind of data is in the packet.
    12.     'Each individual packet is separated by a vbCrLf (new line).
    13.     'We will split the data into packets by the vbCrLf
    14.     'Then we will process each packet individually.
    15.    
    16.     'Find vbCrLf in data.
    17.     If InStr(1, strData, vbCrLf) > 0 Then
    18.         'Split up each packet.
    19.         strPackets() = Split(strData, vbCrLf)
    20.        
    21.         'Loop through all packets.
    22.         For lonLoop = 0 To UBound(strPackets())
    23.             'Make sure packet isn't empty.
    24.             If Len(strPackets(lonLoop)) > 0 Then
    25.                 'strPackets(lonLoop) looks like this in this example:
    26.                 'INFO/PlayerName/Score
    27.                
    28.                 'Check what kind of packet it is:
    29.                 Select Case Left$(strPackets(lonLoop), 4)
    30.                    
    31.                     'It is an info packet.
    32.                     Case "INFO"
    33.                         'Split up the packet into different chunks.
    34.                         'So we can separate the playername from the score.
    35.                         strInfo() = Split(strPackets(lonLoop), "/")
    36.                        
    37.                         'Now:
    38.                         'strInfo(0) = INFO
    39.                         'strInfo(1) = PlayerName
    40.                         'strInfo(2) = Score
    41.                        
    42.                         MsgBox strInfo(1) & " has a score of " & strInfo(2), vbInformation
    43.                    
    44.                     'Add other packet types here if u want:
    45.                     'Case "blah"
    46.                         '
    47.                 End Select
    48.            
    49.             End If
    50.        
    51.         Next lonLoop
    52.     End If
    53.    
    54.     Erase strPackets()
    55.     Erase strInfo()
    56. End Sub

    Not really the best way but it's an example.

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