By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,134 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,134 IT Pros & Developers. It's quick & easy.

Listening to more than one network stream

P: n/a
Hi all,

Sorry if this is the incorrect group but I couldn't see anything
directly relevant.

Can someone confirm for me what happens when two network streams are
sent to an application at the same time.

My scenario is a small server application that listens on a particular
TCP port. I am sending streams of data to it via a client app. Inside
these streams I am using a \n character to delimit certain data. My
question is. Are streams coordinated in some way. For example, If I
flush a StreamWriter to send a stream to the server at the same time
that another client does this, can I guarantee hat the StreamReader
wont receive data that is mixed together? In other words does the
protocol line up all the data in one stream first and then attach the
next stream to the end of the first one? I hope this makes sense.
Please let me know if any further info is required.

Actually one further question. Does a stream have a terminator on the
end. In other words does the Stream Reader know that one stream has
finished before the next one begins.

Sorry if this is a bit vague, but I'm not very hot on the details of
the protocol or the Frameworks support for it.

Many thanks in advance.

Simon

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Simon,

The answer to both of your questions is no. First, you will have to
coordinate the sending of data across a socket. If you have an app sending
information on two separate threads over the same socket, then you have to
coordinate what is sent on your own.

As for sending a terminator, there is nothing that a socket sends to say
"I am done". You should end a terminator on your own.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<si***********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi all,

Sorry if this is the incorrect group but I couldn't see anything
directly relevant.

Can someone confirm for me what happens when two network streams are
sent to an application at the same time.

My scenario is a small server application that listens on a particular
TCP port. I am sending streams of data to it via a client app. Inside
these streams I am using a \n character to delimit certain data. My
question is. Are streams coordinated in some way. For example, If I
flush a StreamWriter to send a stream to the server at the same time
that another client does this, can I guarantee hat the StreamReader
wont receive data that is mixed together? In other words does the
protocol line up all the data in one stream first and then attach the
next stream to the end of the first one? I hope this makes sense.
Please let me know if any further info is required.

Actually one further question. Does a stream have a terminator on the
end. In other words does the Stream Reader know that one stream has
finished before the next one begins.

Sorry if this is a bit vague, but I'm not very hot on the details of
the protocol or the Frameworks support for it.

Many thanks in advance.

Simon

Nov 17 '05 #2

P: n/a
On your server each client will connect and each client will be represented
by a unique socket. You can read and write via the socket directly or wrap
that in a NetworkStream stream. A network stream is different from most
streams and you can't seek or get or set position on it as you don't know
what the end it. End of Stream is set by the client by shutdown send on its
socket. Your server will be do a shutdown send on the client socket when it
is done sending. That way both sockets shutdown cleanly. A shutdown.send
will tell the server stream that is EOS and any further Reads will return 0
bytes read. You are free to process the stream (byte 0 till EOS) anyway you
want using Len bytes as delims or some char delim which things like a
StreamReader will use to delim lines.

If your doing blocking reads on the Network stream, you will get all reads
in order. If you doing async reads, all reads are also in order, however
the notifications of said read (i.e. the callbacks) can come in any order so
this can get complex and difficult to diagnose.

--
William Stacey [MVP]

<si***********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi all,

Sorry if this is the incorrect group but I couldn't see anything
directly relevant.

Can someone confirm for me what happens when two network streams are
sent to an application at the same time.

My scenario is a small server application that listens on a particular
TCP port. I am sending streams of data to it via a client app. Inside
these streams I am using a \n character to delimit certain data. My
question is. Are streams coordinated in some way. For example, If I
flush a StreamWriter to send a stream to the server at the same time
that another client does this, can I guarantee hat the StreamReader
wont receive data that is mixed together? In other words does the
protocol line up all the data in one stream first and then attach the
next stream to the end of the first one? I hope this makes sense.
Please let me know if any further info is required.

Actually one further question. Does a stream have a terminator on the
end. In other words does the Stream Reader know that one stream has
finished before the next one begins.

Sorry if this is a bit vague, but I'm not very hot on the details of
the protocol or the Frameworks support for it.

Many thanks in advance.

Simon

Nov 17 '05 #3

P: n/a
> As for sending a terminator, there is nothing that a socket sends to
say "I am done". You should end a terminator on your own.
Hi Nicholas. Naturally a socket.Shutdown(Send) is an explicit "I am done".

- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<si***********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi all,

Sorry if this is the incorrect group but I couldn't see anything
directly relevant.

Can someone confirm for me what happens when two network streams are
sent to an application at the same time.

My scenario is a small server application that listens on a particular
TCP port. I am sending streams of data to it via a client app. Inside
these streams I am using a \n character to delimit certain data. My
question is. Are streams coordinated in some way. For example, If I
flush a StreamWriter to send a stream to the server at the same time
that another client does this, can I guarantee hat the StreamReader
wont receive data that is mixed together? In other words does the
protocol line up all the data in one stream first and then attach the
next stream to the end of the first one? I hope this makes sense.
Please let me know if any further info is required.

Actually one further question. Does a stream have a terminator on the
end. In other words does the Stream Reader know that one stream has
finished before the next one begins.

Sorry if this is a bit vague, but I'm not very hot on the details of
the protocol or the Frameworks support for it.

Many thanks in advance.

Simon


Nov 17 '05 #4

P: n/a
William,

Yes, it indicates that the socket that is doing the sending is not
allowed to send anything else. However, that's not what the OP wanted. He
wanted to know if a byte sequence was sent which would tell the server "hey,
the client is done" which the Shutdown method does not do. You have to send
that sequence yourself.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:um**************@TK2MSFTNGP15.phx.gbl...
As for sending a terminator, there is nothing that a socket sends to
say "I am done". You should end a terminator on your own.


Hi Nicholas. Naturally a socket.Shutdown(Send) is an explicit "I am
done".

- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<si***********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi all,

Sorry if this is the incorrect group but I couldn't see anything
directly relevant.

Can someone confirm for me what happens when two network streams are
sent to an application at the same time.

My scenario is a small server application that listens on a particular
TCP port. I am sending streams of data to it via a client app. Inside
these streams I am using a \n character to delimit certain data. My
question is. Are streams coordinated in some way. For example, If I
flush a StreamWriter to send a stream to the server at the same time
that another client does this, can I guarantee hat the StreamReader
wont receive data that is mixed together? In other words does the
protocol line up all the data in one stream first and then attach the
next stream to the end of the first one? I hope this makes sense.
Please let me know if any further info is required.

Actually one further question. Does a stream have a terminator on the
end. In other words does the Stream Reader know that one stream has
finished before the next one begins.

Sorry if this is a bit vague, but I'm not very hot on the details of
the protocol or the Frameworks support for it.

Many thanks in advance.

Simon



Nov 17 '05 #5

P: n/a
Shutdown "send" does tell the server that the client stream is closed for
sends. As any further reads at the server will return 0, which is EOS. A
blocking read on the server will always return at least 1 byte, unless send
is closed by client and a read will only then return 0. So if you read 0,
you know the client has closed the stream explicitly for sends. If you want
to know that only one message is done out of multiple messages in a row,
then you need a delim or packetize all messages by length pre-pending them.

--
William Stacey [MVP]

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eE**************@TK2MSFTNGP14.phx.gbl...
William,

Yes, it indicates that the socket that is doing the sending is not
allowed to send anything else. However, that's not what the OP wanted.
He wanted to know if a byte sequence was sent which would tell the server
"hey, the client is done" which the Shutdown method does not do. You have
to send that sequence yourself.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:um**************@TK2MSFTNGP15.phx.gbl...
As for sending a terminator, there is nothing that a socket sends to
say "I am done". You should end a terminator on your own.


Hi Nicholas. Naturally a socket.Shutdown(Send) is an explicit "I am
done".

- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<si***********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi all,

Sorry if this is the incorrect group but I couldn't see anything
directly relevant.

Can someone confirm for me what happens when two network streams are
sent to an application at the same time.

My scenario is a small server application that listens on a particular
TCP port. I am sending streams of data to it via a client app. Inside
these streams I am using a \n character to delimit certain data. My
question is. Are streams coordinated in some way. For example, If I
flush a StreamWriter to send a stream to the server at the same time
that another client does this, can I guarantee hat the StreamReader
wont receive data that is mixed together? In other words does the
protocol line up all the data in one stream first and then attach the
next stream to the end of the first one? I hope this makes sense.
Please let me know if any further info is required.

Actually one further question. Does a stream have a terminator on the
end. In other words does the Stream Reader know that one stream has
finished before the next one begins.

Sorry if this is a bit vague, but I'm not very hot on the details of
the protocol or the Frameworks support for it.

Many thanks in advance.

Simon



Nov 17 '05 #6

P: n/a


si***********@hotmail.com wrote:
Can someone confirm for me what happens when two network streams are
sent to an application at the same time.
TCP is a connection-oriented protocol and the input from connections
will not be intermixed.

The server waits for a connection, and then does a .Accept for each new
connection. This creates a new socket for each client and the input from
each client is sent to the separate sockets, and thus to separate
NetworkStreams and therefore reading each NetworkStream by a separate
StreamReader will "just work".

If you wish to process multiple connections concurrently (which you
probably always will ;), you can either start a new thread to service
each client-connection or use async IO. I would recommend using threads,
since async IO is *amazingly* hard to get right.
Actually one further question. Does a stream have a terminator on the
end. In other words does the Stream Reader know that one stream has
finished before the next one begins.


As the other posts say, Read will return 0 when the other end closes the
connection.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #7

P: n/a
Thanks all,

Helge, thanks for that. I was already heading down the thread path, so
you have headed off a future question.

Cheers

Simon

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.