I'd made a windows service remote server program that will able to communicate to a Windows CE device(as remote client) listening for a broadcast request, save a request to file, examine it and send back the requested file content being trace. I've implemented it in (c#) asynchronous transfer mode. It is working properly, but just 2 days ago I've encountered this socket exception:
System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
this is my code it points to the exception
public void AcceptCallback(IAsyncResult result)
{
ConnectionInfo connection = new ConnectionInfo();
try
{
long connection_number;
//finish accept
Socket s;
s = (Socket)result.AsyncState;
connection.socket = s.EndAccept(result);
connection.socket.Blocking = false;
connection.Buffer = new byte[10000]; //1kb buffer minimum
orionIP = ((IPEndPoint)connection.socket.RemoteEndPoint).Address.ToString();
lock (connections) connections.Add(connection);
IPHostEntry hostName = Dns.GetHostEntry(orionIP);
machineName = hostName.HostName;
lock (connections) connections.Add(connection);
//count client connected
connection_number = Interlocked.Increment(ref connection_count);
//start receive -> error here
connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length,
SocketFlags.None, new AsyncCallback(ReceiveCallBack), connection);
//start new accept
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
}//end try
catch (SocketException ex)
{ //later log files for error events - > ex.SocketErrorCode
string path = "eventlogs.txt";
//create error log files to write error here
File.WriteAllText(path, ex.ToString());
CloseConnection(connection);
}
catch (Exception exp)
{
string path = "eventlogs.txt";
//create error log files to write error here
File.WriteAllText(path, exp.ToString());
CloseConnection(connection);
// log error file later ("Socket Exception:" + exp.ToString());
}
}
I have no major revision made to it when the exception occurred. I followed all the instruction along with others encountering the same problem such as turn off the firewall, maxusePort
, check network connections and devices if properly configured, buffer size. I also check the race condition and WSAECONNABORTED
issues i might dealing with.
Can anyone relates to this problem give me some additional solutions? Any additional knowledge will be a great help .
Receive callback
private void ReceiveCallBack(IAsyncResult result)
{
//06.10 added thread for writing in the file
//added part review ()
//getemp file
string pathwrite = CreateTmpFile();//"streamdata.txt";
ConnectionInfo connection = (ConnectionInfo)result.AsyncState;
try
{
int bytesRead = connection.socket.EndReceive(result);
if (bytesRead > 0)
{
lock (serverlock)
{
connection.sb.Append(Encoding.UTF8.GetString(connection.Buffer, 0, bytesRead));
}
lock (connections)
{
foreach (ConnectionInfo conn in connections)
{
if (connection != conn)
{
conn.socket.Send(connection.Buffer, bytesRead, SocketFlags.None);
}
}
}
connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None,new AsyncCallback(ReceiveCallBack), connection);
}
else
{
string strContent = connection.sb.ToString();
File.WriteAllText(pathwrite, strContent);
AccessData();
CloseConnection(connection);
}
}//end try
catch(SocketException ex){
string path = "eventlogs2.txt";
//create error log files to write error here
File.WriteAllText(path, ex.ToString());
// CloseConnection(connection);
}
catch(Exception ex)
{
string path = "eventlogs2.txt";
//create error log files to write error here
File.WriteAllText(path, ex.ToString());
// CloseConnection(connection);
}
}
you do not accept new connections if there is a network error with the currently accepted connection. Move the BeginAccept to a separate try/catch.
public void AcceptCallback(IAsyncResult result)
{
ConnectionInfo connection = new ConnectionInfo();
try
{
long connection_number;
Socket s;
s = (Socket)result.AsyncState;
connection.socket = s.EndAccept(result);
connection.socket.Blocking = false;
connection.Buffer = new byte[10000]; //1kb buffer minimum
orionIP = ((IPEndPoint)connection.socket.RemoteEndPoint).Address.ToString();
lock (connections) connections.Add(connection);
IPHostEntry hostName = Dns.GetHostEntry(orionIP);
machineName = hostName.HostName;
lock (connections) connections.Add(connection);
connection_number = Interlocked.Increment(ref connection_count);
connection.socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length,
SocketFlags.None, new AsyncCallback(ReceiveCallBack), connection);
}
catch (SocketException ex)
{
string path = "eventlogs.txt";
File.WriteAllText(path, ex.ToString());
CloseConnection(connection);
}
catch (Exception exp)
{
string path = "eventlogs.txt";
File.WriteAllText(path, exp.ToString());
CloseConnection(connection);
}
try
{
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
}
catch (Exception exception)
{
//failed to accept. shut down service.
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.