Results 1 to 9 of 9

Thread: Best way to tell server what kind of message the client is sending?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Best way to tell server what kind of message the client is sending?

    I have a client server setup. When I send messages to the client from server or other way around I am using a kind of identifier on what type of message it is.

    It is a chat server setup right now mainly for learning purposes. When a client sends a chat message to the server it looks like this:

    m|message here

    The server does a string split with "|" to find out what type of message and the data for the type of message.

    Now the problem is "|" can be easily replicated by the client just by typing a message including that character causing the sent data to be incorrect when read by the server. I have made the client so it will remove all "|" in a user's message, but I don't want to do that.

    My main question is what is the optimal way to send data to client/server that allows you to easily grab what type of data is being sent and the actual data for it. Rephrased a bit - say I have three different chat types in the client/server: whisper, say and yell. When the client chooses one and sends a message to the server how can I easily detect what type of chat type was sent and the actual sent message?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Best way to tell server what kind of message the client is sending?

    I would prefix all messages with a fixed length tag, say 4 bytes. That way there would be no need for the "|" character, because you'd know that if you read the first 4 bytes, you'll be able to know what type of message you'll be receiving.
    I also noted from your other thread that you are intending to use the "f|" and "|" as delimiters for binary data. I would actually not recommend that. The chances are extremely large that the binary data in itself will contain any of these characters.
    What I would do instead, would be to write the length of the file first to the stream, preferably as an unsigned 64 bit integer. That way the receiving end will know exactly how much binary data it can expect, rendering the use of delimiters unnecessary.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Fanatic Member coolcurrent4u's Avatar
    Join Date
    Apr 2008
    Location
    *****
    Posts
    993

    Re: Best way to tell server what kind of message the client is sending?

    another way is to use a user defind type with fixed lenght string like this

    type Message
    sType as string * 3
    sValue as string * 256
    end type

    you can use it like this

    dim udtMSG as Message

    udt.sType="SHT" ' i use this command for shutdown
    udt.sValue=vbnullstring ' if you are not appending any information

    another example


    udt.sType="LGN" ' i use this command for login
    udt.sValue=sUser & "~~" & sPass ' two variables that hold username and password

    this method is very efficient and i have been using for some time now

    plse tell m if it helps
    Programming is all about good logic. Spend more time here


    (Generate pronounceable password) (Generate random number c#) (Filter array with another array)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: Best way to tell server what kind of message the client is sending?

    Thanks for the help.

    Another question that kinda follows the thread.

    Should I be sending the data using StreamWriter or NetworkStream?

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Best way to tell server what kind of message the client is sending?

    The StreamWriter and the NetworkStream are two completely different things.
    The StreamWriter is a class that is designed to write data to a stream, more specifically, text data.
    The NetworkStream is what it sounds like, a class that encapsulates a network connection and abstracts it as a stream.

    It all depends on what you're doing, but seeing as you are dealing with alot of binary data, I wouldn't use the StreamWriter. You could write directly using the NetworkStreams methods but in my opinion you should really use the BinaryWriter. I don't see any reason why you shouldn't use it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: Best way to tell server what kind of message the client is sending?

    Thanks again.

    I probably should have included this in the original post but I wasn't thinking of it at the time.

    What if a client sends a message with multiple parameters. For example the client sends a private message to another client. Which means I would need to send the identifier for the private message command, the client who the private message is directed to, and finally the actual message being sent.

    The private message command will be handled as normal(first 4 bytes), but what about the client username? It could be of any length and the message also. How would I extract them as two separate datas?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Best way to tell server what kind of message the client is sending?

    If you use the BinaryWriter, it'll prefix each string with its length, solving the problem completely.
    Why is this you say?
    Because if you read the message on the receiving end using the BinaryReader, you'd just call reader.ReadString() twice to read the username and the message, respectively. (Obviously you'd first have to read the first 4 bytes denoting the message type!).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: Best way to tell server what kind of message the client is sending?

    I'm a bit confused on how to use BinaryReader. This is what I have so far.

    This is the client send message function:
    vb Code:
    1. Public Sub SendServer(ByVal command As String, ByVal data As String)
    2.         Dim bw As IO.BinaryWriter
    3.         Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(command & data)
    4.         Try
    5.             bw = New IO.BinaryWriter(client.GetStream)
    6.             bw.Write(b)
    7.             bw.Flush()
    8.         Catch ex As Exception
    9.             MessageBox.Show(ex.ToString)
    10.         End Try
    11.     End Sub

    Server read:
    vb Code:
    1. Private Sub doRead()
    2.         Const BYTES_TO_READ As Integer = 255
    3.         Dim readBuffer(BYTES_TO_READ) As Byte
    4.         Dim bCommand(3) As Byte
    5.         Dim bytesRead As Integer
    6.         Dim bw As New BinaryReader(mClient.GetStream)
    7.         Dim sBuilder As New System.Text.StringBuilder
    8.         Do Until StopClient
    9.             Try
    10.                 bytesRead = bw.Read(readBuffer, 0, BYTES_TO_READ)
    11.             Catch ex As Exception
    12.                 Form1.removeClient(Me)
    13.                 Exit Sub
    14.             End Try
    15.             If (bytesRead > 0) Then
    16.                 Dim bData(UBound(readBuffer) - 4) As Byte
    17.                 For i As Integer = 0 To UBound(readBuffer)
    18.                     If i < 4 Then
    19.                         bCommand(i) = readBuffer(i)
    20.                     Else
    21.                         bData(i - 4) = readBuffer(i)
    22.                     End If
    23.                 Next
    24.                 Dim message As String = System.Text.Encoding.UTF8.GetString(bCommand)
    25.                 RaiseEvent dataReceived(Me, message, bData)
    26.             End If
    27.         Loop
    28.     End Sub

    It works for when sending single piece of data such as the connect and assign username. I sent CONNUsername. It grabs the first 4 which is CONN and the rest would be the username. But as you were describing in the last post I am unsure on how to do that.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Best way to tell server what kind of message the client is sending?

    Don't use the Read method, which will read a block of bytes just as they are.
    Since the start of the message will always be a 4 byte value, first you'd call the ReadInt32 method:
    Code:
    Dim commandType As Int32 = bw.ReadInt32()
    Now you can evaluate commandType to determine what type of message you are receiving, even before reading the rest of the message.

    Now lets take these chat messages as an example, lets say you evaluate commandType and determine that the incoming message is indeed a chat message, you'd then just call bw.ReadString once to get the username of the recipient, and then call bw.ReadString again to get the actual message. Voila!
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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