Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

.net - How To Signal End Of Data Without Closing NetworkStream in C#

I have a client application that serializes a object and sends it to a server application. The server should deserialize the object, make changes to it, then serialize it and send it back.

Server Code:

TcpClient client = server.AcceptTcpClient();
using(NetworkStream stream = client.GetStream())
{
    using(StreamReader streamReader = new StreamReader(stream))
    {
       string xmlData = streamReader.ReadToEnd();
    }
}

The ReadToEnd doesn't return unless the client closes the stream. But if the client closes the stream, I can't send a response.

Is there a better way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can signal "end of data" by closing only your half of the duplex TCP connection. This is accomplished with Socket.Disconnect.

See how it works with this example, which I kept similar to yours. The client sends the data and then calls Disconnect; this allows ReadToEnd to return while still keeping the server's half of the connection open. The server then sends a response and also disconnects, after which both parties can Close their end of the connection to tear it down.

static void Main(string[] args)
{
    Action clientCode = () =>
        {
            var buffer = new byte[100];
            var clientSocket = new Socket(AddressFamily.InterNetwork,
                                          SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(IPAddress.Loopback, 6690);
            clientSocket.Send(buffer);
            clientSocket.Disconnect(false);
            Console.WriteLine("Client: message sent and socket disconnected.");
            while (true) {
                var bytesRead = clientSocket.Receive(buffer);
                if (bytesRead == 0) {
                    break;
                }

                Console.WriteLine("Client: read " + bytesRead + " bytes.");
            }

            clientSocket.Dispose();
        };

    var server = new TcpListener(IPAddress.Loopback, 6690);
    var thread = new Thread(new ThreadStart(clientCode));
    server.Start();
    thread.Start();
    var client = server.AcceptTcpClient();

    using(NetworkStream stream = client.GetStream()) {
        using(StreamReader streamReader = new StreamReader(stream))
        {
            var data = streamReader.ReadToEnd();
            Console.WriteLine("Server: read " + data.Length + " bytes.");

            // Since we 're here we know that the client has disconnected.
            // Send the response before StreamReader is disposed, because
            // that will cause the socket itself to be closed as well!
            Thread.Sleep(TimeSpan.FromSeconds(1));
            Console.WriteLine("Server: sending response.");
            stream.Write(new byte[10], 0, 10);
            Console.WriteLine("Server: closing socket.");
        }
    }

    server.Stop();
    Console.WriteLine("Server: waiting for client thread to complete.");
    thread.Join();


    return;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...