Does socket has 2 streams ? What is "(a)synchronous"? Will BeginReceive() creates too many threads s

R

Ryan Liu

TcpClient has a method called GetworkStream GetStream();
So in other words, there is only one stream associate with it for input and
output, right?

So while it is receiving, it can not send, and vise visa, right? So will it
be a problem both server and client can initiative a sending action?

TcpClient only supports synchronous operation. What does "Synchronous" mean?
Means while it is reading or waiting for data to arrive from the stream, it
will block current thread? Does it also mean it will block another thread
in the same process from sending data though this stream? Or just an
exception while other thread sends data.

But of cause it can not block the computer on the another side of stream,
what happen another side client computer call Steam's read method?

Socket does not has such method like GetStream(), but it supports
asynchronous operation. Does this means it has 2 streams for input and
output?

And what does "asynchronous" mean? If I have a server keep receiving
message from clients, if I use BeginReceive() method, is that means in the
endless loop, it will create a thread each time when it executes
BeginReceive(). Creating so many threads will it hurt performance of the
server?

And is that true, I need an endless loop to receive all messages from one
client, and if each message is long, just to receive a single message
(message has the header to tell the total length), it might need get data
from multiple BeginReceive()? And data get from one BeginReceive() might
has first part belong to previous message and the rest for next message?

On server side, I think to minimize the resource usage, it is better just
keep one thread for each client. This thread will receive data from client,
and when one complete message received, it process this message in the same
thread. Is this reasonable?

But if multiple BeginReceive() get one message, each BeginReceive() creates
a thread, then this is really different from my intention to reduce the
resource usage.

Sever programming is a difficult task for me. I've asked too many questions.
Really appreciate for your help!

Ryan
 
W

William Stacey [MVP]

| So in other words, there is only one stream associate with it for input
and
| output, right?

right.

| So while it is receiving, it can not send, and vise visa, right? So will
it
| be a problem both server and client can initiative a sending action?

You can read and write a ~same time. You could, for example, have 1 thread
reading and 1 thread writing. You don't want multiple readers or writers on
the same socket however.

| Socket does not has such method like GetStream(), but it supports
| asynchronous operation.

NetworkStream has BeginRead and BeginWrite also.

| Does this means it has 2 streams for input and
| output?

No stream at all. However you can send and receive on diff threads as same
time. Internally, it may lock (have not seen the code) any shared data
structures so it may not allow "actual" simultaneous reads and writes, but
close enouph.

| And what does "asynchronous" mean? If I have a server keep receiving
| message from clients, if I use BeginReceive() method, is that means in the
| endless loop, it will create a thread each time when it executes
| BeginReceive(). Creating so many threads will it hurt performance of the
| server?

The callback is run on a thread pool thread and those are shared and max
threads can only grow so high.
 
R

Ryan Liu

Hi William,

Thanks a lot for your prompt and very clear reply.

One last question, I have defined Message data structure, and it has method
to convert a Message to byte[] and parse byte[] back to Message.

If client send the whole Message's byte[] in one BeginSend(), will the
server guaranteed to receive all bytes if the receiving buffer is big
enough, so I don't have to concatenate the bytes received on the server.

The message has the first 4 bytes indicates start of message , followed by 4
bytes to define total length, can these information be helpful in
BeginReceive()?

I know these questions are too detailed and specific, really greatful if you
can help.

Thanks a lot!
Ryan
 
W

William Stacey [MVP]

For udp, you will get the whole message. For tcp, it is a stream, so you
can only expect to get 1 or more bytes (or 0 if client shutdown send side).
There is no notion of message boundaries other then what you define in your
protocol. So in this case, you read 4 bytes and get the int len. Then read
util you get all bytes in the message. After you get the len, you can
create one byte[] of that size and use that as your buffer for the whole
message, so you don't have to append smaller byte[]'s together.

--
William Stacey [MVP]

| Hi William,
|
| Thanks a lot for your prompt and very clear reply.
|
| One last question, I have defined Message data structure, and it has
method
| to convert a Message to byte[] and parse byte[] back to Message.
|
| If client send the whole Message's byte[] in one BeginSend(), will the
| server guaranteed to receive all bytes if the receiving buffer is big
| enough, so I don't have to concatenate the bytes received on the server.
|
| The message has the first 4 bytes indicates start of message , followed by
4
| bytes to define total length, can these information be helpful in
| BeginReceive()?
|
| I know these questions are too detailed and specific, really greatful if
you
| can help.
|
| Thanks a lot!
| Ryan
|
| "William Stacey [MVP]" <[email protected]> дÈëÓʼþ
| | > | So in other words, there is only one stream associate with it for
input
| > and
| > | output, right?
| >
| > right.
| >
| > | So while it is receiving, it can not send, and vise visa, right? So
will
| > it
| > | be a problem both server and client can initiative a sending action?
| >
| > You can read and write a ~same time. You could, for example, have 1
| thread
| > reading and 1 thread writing. You don't want multiple readers or
writers
| on
| > the same socket however.
| >
| > | Socket does not has such method like GetStream(), but it supports
| > | asynchronous operation.
| >
| > NetworkStream has BeginRead and BeginWrite also.
| >
| > | Does this means it has 2 streams for input and
| > | output?
| >
| > No stream at all. However you can send and receive on diff threads as
| same
| > time. Internally, it may lock (have not seen the code) any shared data
| > structures so it may not allow "actual" simultaneous reads and writes,
but
| > close enouph.
| >
| > | And what does "asynchronous" mean? If I have a server keep receiving
| > | message from clients, if I use BeginReceive() method, is that means in
| the
| > | endless loop, it will create a thread each time when it executes
| > | BeginReceive(). Creating so many threads will it hurt performance of
the
| > | server?
| >
| > The callback is run on a thread pool thread and those are shared and max
| > threads can only grow so high.
| >
| >
|
|
 
N

Nick Hounsome

Ryan Liu said:
Hi William,

Thanks a lot for your prompt and very clear reply.

One last question, I have defined Message data structure, and it has
method
to convert a Message to byte[] and parse byte[] back to Message.

If client send the whole Message's byte[] in one BeginSend(), will the
server guaranteed to receive all bytes if the receiving buffer is big
enough, so I don't have to concatenate the bytes received on the server.

No it isn't gauranteed and you should not rely on it. TCP is a stream
oriented protocol not a message oriented protocol.
The message has the first 4 bytes indicates start of message , followed by
4
bytes to define total length, can these information be helpful in
BeginReceive()?

Yes but you can't even gaurantee the 4 bytes or the length will come
together in all cases - it is possible (assuming your protocol doesn't
handshake every message) for the sender to send 2 messages one after the
other such that it is buffered at the sender and then split in the network
resulting in a partial read of the start of message or the length (It's very
unlikely but you should really handle it).
 

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