|
-
Sep 2nd, 2009, 11:53 PM
#1
Thread Starter
Lively Member
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?
-
Sep 3rd, 2009, 01:49 AM
#2
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.
-
Sep 3rd, 2009, 05:41 AM
#3
Fanatic Member
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
-
Sep 3rd, 2009, 11:10 AM
#4
Thread Starter
Lively Member
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?
-
Sep 3rd, 2009, 11:15 AM
#5
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.
-
Sep 3rd, 2009, 01:02 PM
#6
Thread Starter
Lively Member
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?
-
Sep 3rd, 2009, 01:13 PM
#7
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!).
-
Sep 3rd, 2009, 01:55 PM
#8
Thread Starter
Lively Member
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:
Public Sub SendServer(ByVal command As String, ByVal data As String)
Dim bw As IO.BinaryWriter
Dim b As Byte() = System.Text.Encoding.UTF8.GetBytes(command & data)
Try
bw = New IO.BinaryWriter(client.GetStream)
bw.Write(b)
bw.Flush()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Server read:
vb Code:
Private Sub doRead()
Const BYTES_TO_READ As Integer = 255
Dim readBuffer(BYTES_TO_READ) As Byte
Dim bCommand(3) As Byte
Dim bytesRead As Integer
Dim bw As New BinaryReader(mClient.GetStream)
Dim sBuilder As New System.Text.StringBuilder
Do Until StopClient
Try
bytesRead = bw.Read(readBuffer, 0, BYTES_TO_READ)
Catch ex As Exception
Form1.removeClient(Me)
Exit Sub
End Try
If (bytesRead > 0) Then
Dim bData(UBound(readBuffer) - 4) As Byte
For i As Integer = 0 To UBound(readBuffer)
If i < 4 Then
bCommand(i) = readBuffer(i)
Else
bData(i - 4) = readBuffer(i)
End If
Next
Dim message As String = System.Text.Encoding.UTF8.GetString(bCommand)
RaiseEvent dataReceived(Me, message, bData)
End If
Loop
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.
-
Sep 3rd, 2009, 02:12 PM
#9
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|