2

I am rewriting the client side of an existing C++ application to be C#. The server application will remain written in C++. The communication is working properly all the way until the end, where the Server closes the socket. The architecture is as follows and uses Async send and receive: Client: Send Data to Server Server: Receive Data from Client Server: Send data (files) to Client Server: Close Socket Client: Receive Data from Server

In the old C++ version of the program, the data is already on the socket and even though one side of the socket connection closes, the data is still available. From my undertanding in C#, once half of the connection closes, a reset connection packet is sent and the C# gets an exception, which prevents me from receiving the rest of the data. (I am only receiving 1 file instead of 2 since the socket is closed by the server after it sends the information).

I can post my code, but it really is just a simple Async Send and Receive. If i step through the code and pause right before the socket close function of the server, everything works as expected. The actual Exception error is "An existing connection was forcibly closed by the remote host."

  • So you know perfectly what is happening there. I can see no question here. No questionmarks in your text. You just let the server wait for client to acknowledge that he received the message. – Al Kepp Jan 26 '19 at 00:21
  • I guess my question is I don't understand why I am getting the error. In the C++ to C++ version of this application, the socket is closed by the server, and the client is still able to get the data from the socket without any errors. In C#, it seems like it shoudl not be any different, but it does give you an error that I cannot seem to handle. I am also not supposed to modify any of the server code. – Jeremy Vaughn Jan 26 '19 at 00:24
  • I would need to try it myself to be sure, but it looks like this is an edge case, maybe we can call it a bug in your server program. So from the limited information we have, I would say a bug should be fixed. I understand that they probably defined it as "do not change", but did they know there is a bug? – Al Kepp Jan 26 '19 at 00:31
  • I do not think it is a bug since the previous cilent/server applications work perfectly. I think it is more of understanding how C# handles halfway connections. I tried to follow the thread linked below with same error, but to no avail. Of course the problem they have is slightly different, but results in the same error. https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host – Jeremy Vaughn Jan 26 '19 at 00:37
  • Please understant that the pure fact that it worked in C++ doesn't prove that there is no bug. But what is more important: You should always acknowledge that you received the whole message at the opposite end before you close the connection. Fire-and-forget as you do is not a reliable way of network communication. – Al Kepp Jan 26 '19 at 00:41
  • Even though the server sends the data and then closes, wouldn't the client still be able to see the data on the socket? https://stackoverflow.com/questions/11889791/receiving-data-from-already-closed-socket – Jeremy Vaughn Feb 04 '19 at 23:15

0 Answers0