Listening to more than one network stream

S

simonrigby_uk

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
W

William Stacey [MVP]

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.
 
W

William Stacey [MVP]

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]
- (e-mail address removed)

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

William Stacey said:
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]
- (e-mail address removed)

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
 
W

William Stacey [MVP]

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 said:
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]
- (e-mail address removed)

William Stacey said:
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]
- (e-mail address removed)

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
 
H

Helge Jensen

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.
 
S

simonrigby_uk

Thanks all,

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

Cheers

Simon
 

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

Top