0

I am trying to connect to printer through my code and then applying security settings on the printer. Before applying the security feature it checks for password in the following code module. The code hangs after the line

stream.Flush()

if (responseStr.IndexOf(PrinterSecurityConstants.NewPrinterPass) > 0)
{
    LogUtility.Info(PrinterSecurityConstants.ValidateUserNamePassword);

    data = System.Text.Encoding.ASCII.GetBytes(
        ConfigurationManager.AppSettings[PrinterSecurityConstants.CurrentPass]
        .ToLower() + CommonConstant.Carriagereturn);
    stream.Write(data, 0, data.Length);
    stream.Flush();
    bytes = stream.Read(dataResponse, 0, dataResponse.Length);
    responseStr = System.Text.Encoding.ASCII.GetString(dataResponse, 0, bytes);
}

What I need to do so that this code works.

Vidit
  • 1
  • 1
  • 3
  • can you put a break point on stream.write and tell us what the value of data is? – Benjamin Danger Johnson Oct 09 '12 at 20:52
  • What type is `stream`? What is `dataResponse`? – Dave Zych Oct 09 '12 at 20:58
  • it has 9 random numbers and after i execute the line and then again try to add watch, it says the expression is evaluated and has no value.And the code hangs after executing the line bytes = stream.Read(dataResponse, 0, dataResponse.Length); – Vidit Oct 09 '12 at 21:01
  • stream is defined as NetworkStream stream = null and dataresponse is Byte[] dataResponse = new Byte[1024]; – Vidit Oct 09 '12 at 21:02
  • Regarding NetworkStream, Microsoft says "because NetworkStream is not buffered, it (Flush) has no affect on network streams." http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.flush.aspx – hatchet - done with SOverflow Oct 09 '12 at 21:19
  • yup, it hangs after excuting the next line and not at the stream.Flush(); – Vidit Oct 09 '12 at 21:27
  • 1
    very similar to http://stackoverflow.com/questions/6958255/what-are-some-reasons-networkstream-read-would-hang-block and http://stackoverflow.com/questions/1365012/c-sharp-networkstream-read – hatchet - done with SOverflow Oct 09 '12 at 21:56

1 Answers1

0

Stream.Read() blocks until there are bytes available for reading. If you do not want to do this you need to change the read mode.

SolidRegardless
  • 427
  • 3
  • 17
  • 4
    Generally, most streams support the Stream.DataAvailable() method which checks if there is data to be read from the Stream, if there is not, you can either block anyway until there is, by Read(), or loop and do something else before testing again with DataAvailable(). Usually, blocking is not a problem, because the Read is on a different Thread to the main program thread. – SolidRegardless Oct 10 '12 at 07:12
  • @SolidRegardless I expected the Read() to return 0 if there was nothing in the stream based on the following Microsoft documentation (see Remarks section): https://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.read%28v=vs.100%29.aspx "If no data is available for reading, the Read method returns 0." I guess this is only after the first successful read. Anyway, your post was helpful in understanding that -- using the DataAvailable flag helped me out. – VineAndBranches Feb 22 '16 at 18:44