Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2005

Tip: Looking for answers? Try searching our database.

Networkstream.BeginWrite exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adi A - 10 Jul 2005 13:58 GMT
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?
David Browne - 10 Jul 2005 18:43 GMT
> Hello
>
[quoted text clipped - 88 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.