Networkstream.BeginWrite exception

A

Adi A

Hello

I have a multi-tier application which uses tcp/ip for communication between
tier.
Sometimes during the startup of the server I get the following exception
(the first line was added by my application):

Exception in method HandeOnError: System.Exception: Error sending
string --->
System.ArgumentNullException: Value cannot be null.
Parameter name: buffer
at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
at AdsTCPServer.TAdsConnection.Send(String Data)
--- End of inner exception stack trace ---
at AdsTCPServer.TAdsConnection.Send(String Data)

As you'll be able to see in the following code, I check every parameter the
BeginWrite method receives (in the "catch" clause), and according to the
"Error sending string" header, all of the parameters passed the check:

public void Send(string Data)
{
if (IsOpen())
{
try
{
if (m_oSocket != null)
{
Data+= m_strWriteTerminator;
m_arrWriteBuffer = Encoding.ASCII.GetBytes (Data);

NetworkStream netstream = m_oSocket.GetStream();
try
{
if (netstream != null)
{
lock(netstream)
{
if (netstream.CanWrite)
{
netstream.BeginWrite(m_arrWriteBuffer,0, Data.Length,
m_SendCallback,netstream);
}
}
}
}
catch (Exception E)
{
Exception myException;
if (m_SendCallback == null)
myException = new Exception("Error sending string: m_SendCallback is
null", E);
if (m_arrWriteBuffer == null)
myException = new Exception("Error sending string: m_arrWriteBuffer is
null", E);
else
if (m_arrWriteBuffer.Length == 0)
myException = new Exception("Error sending string: m_arrWriteBuffer
is 0 length", E);
if (Data == null)
myException = new Exception("Error sending string: Data is null", E);
if (netstream == null)
myException = new Exception("Error sending string: netstream is null",
E);
if (Data == "")
myException = new Exception("Error sending string: Data is empty", E);
else
myException = new Exception("Error sending string", E);
throw myException;
}
netstream = null;
m_arrWriteBuffer = null;
}
}
catch (Exception E)
{
if (IsSocketDown(E.ToString()))
DoOnDisconnect();
else
DoOnError(E);
}
GC.Collect();
}
}


Any idea what causes this exception?
 
D

David Browne

Adi A said:
Hello

I have a multi-tier application which uses tcp/ip for communication
between
tier.
Sometimes during the startup of the server I get the following exception
(the first line was added by my application):

Exception in method HandeOnError: System.Exception: Error sending
string --->
System.ArgumentNullException: Value cannot be null.
Parameter name: buffer
at System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32
offset, Int32 size, AsyncCallback callback, Object state)
at AdsTCPServer.TAdsConnection.Send(String Data)
--- End of inner exception stack trace ---
at AdsTCPServer.TAdsConnection.Send(String Data)

As you'll be able to see in the following code, I check every parameter
the
BeginWrite method receives (in the "catch" clause), and according to the
"Error sending string" header, all of the parameters passed the check:

public void Send(string Data)
{
if (IsOpen())
{
try
{
if (m_oSocket != null)
{
Data+= m_strWriteTerminator;
m_arrWriteBuffer = Encoding.ASCII.GetBytes (Data);

NetworkStream netstream = m_oSocket.GetStream();
try
{
if (netstream != null)
{
lock(netstream)
{
if (netstream.CanWrite)
{
netstream.BeginWrite(m_arrWriteBuffer,0, Data.Length,
m_SendCallback,netstream);
}
}
}
}
catch (Exception E)
{
Exception myException;
if (m_SendCallback == null)
myException = new Exception("Error sending string: m_SendCallback is
null", E);
if (m_arrWriteBuffer == null)
myException = new Exception("Error sending string: m_arrWriteBuffer
is
null", E);
else
if (m_arrWriteBuffer.Length == 0)
myException = new Exception("Error sending string: m_arrWriteBuffer
is 0 length", E);
if (Data == null)
myException = new Exception("Error sending string: Data is null", E);
if (netstream == null)
myException = new Exception("Error sending string: netstream is
null",
E);
if (Data == "")
myException = new Exception("Error sending string: Data is empty",
E);
else
myException = new Exception("Error sending string", E);
throw myException;
}
netstream = null;
m_arrWriteBuffer = null;
}
}
catch (Exception E)
{
if (IsSocketDown(E.ToString()))
DoOnDisconnect();
else
DoOnError(E);
}
GC.Collect();
}
}


Any idea what causes this exception?

Your catch block is completely broken. As it is program flow will go to
multiple exception assignments. Just throw instead of assigning the new
exception to a local variable and then throwing at the end.

catch (Exception E)
{
if (m_SendCallback == null)
throw new Exception("Error sending string: m_SendCallback is null", E);
if (m_arrWriteBuffer == null)
throw new Exception("Error sending string: m_arrWriteBuffer is null",
E);
if (m_arrWriteBuffer.Length == 0)
throw new Exception("Error sending string: m_arrWriteBuffer is 0
length", E);
if (Data == null)
throw new Exception("Error sending string: Data is null", E);
if (netstream == null)
throw new Exception("Error sending string: netstream is null", E);
if (Data == "")
throw new Exception("Error sending string: Data is empty", E);
throw new Exception("Error sending string", E);
}



David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top