473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Listening to more than one network stream

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
7 3111
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
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
> 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
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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
I have a Socket open to a target system. I get the network stream from the open socket and then create a stream reader and stream writer on this stream. The stream reader and writers are operating...
2
by: Kueishiong Tu | last post by:
I have a network stream which I got from HttpWebResponse and does not support seeking, How do I find the length of the network stream? HttpWebResponse* response = dynamic_cast<HttpWebResponse*>...
6
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
5
by: cs | last post by:
I need to get the position and also seek in a network stream, is there an easy way to do that? (I am converting some java code so thats the easy path).
8
by: Scott | last post by:
Hi guys, If I try to call read(), readline(), readtoend() and there is nothing to read (from a never ending loop for example) the program seems to continue but it exits the loop for no apparent...
3
by: Alex Clark | last post by:
Hi All, I'm having some problems reading a network stream. I'm writing a lightweight POP client to handle a very specific task, but I keep unexpectedly reaching the end of the datastream when...
3
by: kjell | last post by:
Hi, I'm trying to write a program that reads data from a network stream. I would like the program to read all available data in the buffer and then process the data. I do not want the program...
3
by: Al G | last post by:
Does anyone know what a -1 means as a Network stream read timeout? Console.WriteLine("Timeout= {0}", stream.ReadTimeout) returns a -1 Thanks
0
by: cva cangaran | last post by:
server side coding TcpClient dupClient = listener.AcceptTcpClient(); NetworkStream ns = dupClient.GetStream(); StreamReader sr = new StreamReader(ns); StreamWriter sw = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.