Why does the Close call in a thread behave differently in C # and Java?

Consider the following scenario: the servlet was written in Java, and as soon as you connect to the servlet, it will start writing to OutputStream, say 10 million bytes, one byte at a time.

You have a client program that reads the servlet response stream and reads 100 bytes and causes a close. Now, if your client program is in Java, the threads immediately close and the server stops sending content, but if the client program is in C #, the close call takes a lot of time, because it seems to be waiting for the completion of writing 10 million bytes.

So, I have two questions,

  • Why does C # behave differently?
  • What can I do to close a call on a thread C # closes the thread immediately and prevents the server from continuing to send data?

Any pointers would be appreciated :-)

+4
source share
2 answers

So, I finally figured it out a couple of weeks ago and tested it with Microsoft. The difference between C # and Java is that in Java close call also closes the request / connection, and in C # this does not happen unless you execute it using xxxRequest.Abort (). Hope this helps someone else too.

0
source

I guess here are some sockets. I would suspect that the Java implementation simply closes the client socket, which may cause an error on the server, while the C # version is a bit more server friendly and waits for it to confirm the close request. Since the server is busy shutting down data, it does not receive, or at least does not process, a shutdown request until it completes the sending.

Think of it this way: someone in front of you is trying to sell you something and will not be interrupted. You can slam the door on the face that closes them immediately, or you can wait until they finish the conversation, and then ask them to leave.

To solve this problem, perhaps you can create a worker thread that opens the thread, waits for 100 bytes, and then closes. In the meantime, your program can do whatever it takes while the thread eventually shuts down.

+3
source

All Articles