I am trying to send image via TcpClient and BufferedStream. The problem is that i cant send the data without disconnecting the TcpClient.

Send data:

while ((bytesread = bufferedinput.Read(buffer, 0, sizeBuffer)) > 0)
            {
                //Sending data
                bufferedoutput.Write(buffer, 0, bytesread);
            }

Receive data:

while ((bytesread = bufferedinput.Read(buffer, 0, buffsize)) > 0)
                {
                    bufferedoutput.Write(buffer, 0, bytesread);
                }

where :

NetworkStream serverStream = client.GetStream();

and BufferedStream bufferedoutput = new BufferedStream(serverStream);

The problem is that i must cast bufferedoutput.Close(); in the client side to get the data received by the server, but this disconnects my client, which is a problem. Any suggestions?

有帮助吗?

解决方案

It is evident that by closing NetworkStream object associated with a TcpClient object, disconnects the client. Since TCP is a stream based protocol, you can do only one of two thing, either 1.] use networkstream.readtimeout to disconnect if no data is available till the readtimeout, OR 2.] devise a protocol to tell the receiver about the length of data, sender is willing to send, and read only that length of bytes.

其他提示

Have you tried just bufferedoutput.Flush() instead?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top