Click here to Skip to main content
15,886,056 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
an existing connection was forcibly closed by the remote host

This happens with a socket connection between client and server. The connection is alive and well, and heaps of data is being transferred, but it then becomes disconnected out of nowhere.


Latest....

Server Program
===============

class Program
{

public static void StartServer()
{

byte[] bytes = new byte[1024];

try
{
try
{
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 1, 105 });
//ipadd = ds.Tables[0].Rows[i]["IP"].ToString();
//IPAddress ipAddress = new IPAddress(Encoding.ASCII.GetBytes(ipadd ));
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);


Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);



sender.Connect(remoteEP);

Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString());


byte[] msg = Encoding.ASCII.GetBytes("date");


int bytesSent = sender.Send(msg);


int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test {0}= {1}", ipAddress.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytesRec));



}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}

//}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

static void Main(string[] args)
{

StartServer();
Console.Read();
}

Client Program
==============
class Program
{
public static string data = null;

public static void StartListening()
{

byte[] bytes = new Byte[1024];


IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);


Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);


try
{
listener.Bind(localEndPoint);
listener.Listen(10);


while (true)
{

Console.WriteLine("Waiting for a connection...");

Socket handler = listener.Accept();
data = null;

while (true)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);

break;
}

if (data == "date")
{

System.Diagnostics.Process process1;
process1 = new System.Diagnostics.Process();
process1.EnableRaisingEvents = false;

*******************************
System.Diagnostics.Process.Start("Notepad.exe");//, strCmdLine);
*******************************
process1.Close();
byte[] msg = Encoding.ASCII.GetBytes("operation completed");

handler.Send(msg);
}
handler.Shutdown(SocketShutdown.Both);
handler.Close();

}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}


}

static void Main(string[] args)
{
StartListening();
}

This Program Execute without error...

i change the code notepad.exe to visualstudio setupproject.exe that returns network socket exception
Posted
Comments
Richard MacCutchan 7-Dec-13 4:27am    
Please format you code properly (use the "code" link above the edit box, and use indents), and also explain where the error occurs.

1 solution

Similar question with answer
An existing connection was forcibly closed by the remote host[^]

Like that OP, you did the same mistake. You forgot to close the socket connection. Use Socket.Close Method[^] to close the connection properly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900