TCP question

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

I need to transfer different types of data from my tcp client to my tcp
server - string, bynary, datasets.
Ho to know what exactly data I receive?
 
Hi Nikolay,

All TCP data is transmitted as a series of bytes. It's your
responsibility to determine a proper higher-level protocol for these
bytes so you know what you're looking at.

Take the MSN protocol for example. Sometimes you receive a command that
contains binary data. The first part of the command is a Command String
(a straight Byte-To-ASCII conversion) which gives you the important
information about the command (most notably the type of command and the
length of the incoming data). You download the amount of data the
command tells you it has sent, and then judge what to do with that data
based on the command you received.

I personally would do something similar in your server and client. I
would decide on a protocol to be applied on top of TCP to designate what
sort of data you are transmitting.

Regards,
-Adam.
 
I thought of way to send some kind of headers, but I am very new to
programming and I don't know how.
I wonder, because data is transferred as Bytes, should the headers be binary
or string.
the simplest thing that I can figure is to use an integer as header and
based on the integer to decide what kind of data is transmitted. Just can't
figure out how to attach the header to the sent data and how to separate it
at the receiver.
 
| I wonder, because data is transferred as Bytes, should the headers be
binary
| or string.

uhhhh...humph. ;^)

try looking at the smtp or nntp protocols. that will help you get started.
to address your above statement fairly...it is strictly arbitrary which
approach you take.
 
Nikolay,
In addition to the other comments, the following article demonstrates how to
use a BinaryReader & BinaryWriter do send & receive a "message" over a
NetworkStream.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09182003.asp

The example is in C#, however it should be easily converted to VB.NET, post
if you need help.

I would define a "message" that included a header & detail. The "header"
would include information such as the type of message & possible length of
message.

The "type of message" could be an Integer, an Enum, a String, or a Double,
or a few other things; just about any serializable type; just as long as all
"messages" use the same "type". I would probably use an Enum, as it
encapsulates the types nicely.

For example using an Enum:

Public Enum MessageType As Integer
[String]
[Integer]
datasets
End Enum


Dim writer As BinaryWriter
writer.Write(MessageType.String)
writer.Write("This is the string")
writer.Write(MessageType.Integer)
writer.Write(100)
writer.Write(MessageType.datasets)
' code to write a DataSet to the Stream

Dim reader As BinaryReader
Dim type As MessageType = CType(reader.ReadInt32(), MessageType)
Select Case type
Case MessageType.String
Dim s As String = reader.ReadString()
Case MessageType.Integer
Dim i As Integer = reader.ReadInt32
Case MessageType.datasets
' code to read the DataSet from the stream
End Select

For example using A String:

Dim writer As BinaryWriter
writer.Write("String")
writer.Write("This is the string")
writer.Write("Integer")
writer.Write(100)
writer.Write("datasets")
' code to write a DataSet to the Stream

Dim reader As BinaryReader
Dim type As String = reader.ReadString()
Select Case type
Case "String"
Dim s As String = reader.ReadString()
Case "Integer"
Dim i As Integer = reader.ReadInt32
Case "datasets"
' code to read the DataSet from the stream
End Select


Hope this helps
Jay
 
Jay that was very helpful, thank you!
I've red many answer given by you and all of them were great.
You are very useful for the community!



Jay B. Harlow said:
Nikolay,
In addition to the other comments, the following article demonstrates how
to
use a BinaryReader & BinaryWriter do send & receive a "message" over a
NetworkStream.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09182003.asp

The example is in C#, however it should be easily converted to VB.NET,
post
if you need help.

I would define a "message" that included a header & detail. The "header"
would include information such as the type of message & possible length of
message.

The "type of message" could be an Integer, an Enum, a String, or a Double,
or a few other things; just about any serializable type; just as long as
all "messages" use the same "type". I would probably use an Enum, as it
encapsulates the types nicely.

For example using an Enum:

Public Enum MessageType As Integer
[String]
[Integer]
datasets
End Enum


Dim writer As BinaryWriter
writer.Write(MessageType.String)
writer.Write("This is the string")
writer.Write(MessageType.Integer)
writer.Write(100)
writer.Write(MessageType.datasets)
' code to write a DataSet to the Stream

Dim reader As BinaryReader
Dim type As MessageType = CType(reader.ReadInt32(), MessageType)
Select Case type
Case MessageType.String
Dim s As String = reader.ReadString()
Case MessageType.Integer
Dim i As Integer = reader.ReadInt32
Case MessageType.datasets
' code to read the DataSet from the stream
End Select

For example using A String:

Dim writer As BinaryWriter
writer.Write("String")
writer.Write("This is the string")
writer.Write("Integer")
writer.Write(100)
writer.Write("datasets")
' code to write a DataSet to the Stream

Dim reader As BinaryReader
Dim type As String = reader.ReadString()
Select Case type
Case "String"
Dim s As String = reader.ReadString()
Case "Integer"
Dim i As Integer = reader.ReadInt32
Case "datasets"
' code to read the DataSet from the stream
End Select


Hope this helps
Jay

Nikolay Petrov said:
I need to transfer different types of data from my tcp client to my tcp
server - string, bynary, datasets.
Ho to know what exactly data I receive?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top