Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - an established connection was aborted by the software in your host machine Error

I am building a Mock Server using TCPListener.

When I tried to debug my code by running the client, I was able to read the request on the server. But on the client side, an exception is being thrown. Here is the exception I got from the trace:

System.ServiceModel.CommunicationException: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:05:00'. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine

Here is the code of I implemented on the server:

TcpClient client = listener.AcceptTcpClient();
NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
var dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);

byte[] sendBytes = null;
try
{
    var output = "POST HTTP/1.1 200 OK\r\n";
    output += "Server: Apache-Coyote/1.1\r\n";
    output += "Content-Type: application/soap+xml\r\n";
    output += "Content-Length: 632\r\n";
    output += "Date: Thu, 06 Aug 2015 02:06:39 GMT\r\n\r\n";
    output +=
        "<S:Envelope xmlns:S=\"http://www.w3.org/2003/05/soap-envelope\"><S:Header></S:Header><S:Body></S:Body></S:Envelope>";
    sendBytes = Encoding.ASCII.GetBytes(output);
    nwStream.Write(sendBytes, 0, sendBytes.Length);
    nwStream.Flush();
}
catch (SocketException se)
{
    throw;
}

client.Close();

Edit

I also tried to disable my firewall but the exception still occurs.

like image 981
Ray Avatar asked Aug 11 '15 00:08

Ray


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Setting the correct Content-Length fixed it.

If Content-Length is greater than the actual length, the client-side will continue to wait for the remaining parts of the response. But since the server is already done with sending the response, it will end the process.

like image 67
Ray Avatar answered Nov 06 '22 06:11

Ray