0

I have a thread which check is user online

var user = (User)userObj;
if (!IsUserOnline(user)) return;
if (!TryToConnect(user)) return;
var TCP = user.CmdTCP;
Stream stm = TCP.GetStream();

This completes sucessfully everytime, but as I

stm.Write(GetBytes("chk"), 0, 3);

and then

var bb = new byte[1];
try {
stm.Read(bb, 0, 1);
} catch (Exception e) { /*error*/ }
if (bb[0] == CHR_SUCCESS) {
//etc etc
}

Stream Read fails with no warning when cheking if user is online second time. No exception, code doesn't go further. The thread just stops, program works.

Whole thread:

        var user = (User)userObj;
        if (!IsUserOnline(user)) return;
        if (!TryToConnect(user)) return;
        var TCP = user.CmdTCP;
        Stream stm = TCP.GetStream();
        try {
            stm.Write(GB("chk"), 0, 3);
            toLog("refresh::chk sent ;)");
        } catch (Exception e) {
            #region unable to send packet, online
            toLog("\trefresh::" + user.name + " is online (Connected, unable to send packet) " + e.Message);
            setUserStatus(user, userStatus.Online);
            return;
            #endregion
        }
        var bb = new byte[1];
        try {
            stm.Read(bb, 0, 1); //<<<<<<<<< here thread just ends, no toLog("..");, no exception
            toLog("refresh::got " + (char)bb[0]);
        } catch (Exception e) {
            #region unable to get packet, online
            toLog("\trefresh::" + user.name + " is online (Connected, unable to get packet) " + e.Message);
            setUserStatus(user, userStatus.Online);
            return;
            #endregion
        }
        if (bb[0] == CHR_SUCCESS) {
            try {
                stm.Write(GB(toStr(CHR_SSUCCESS)), 0, 1);
                toLog("refresh::CHR_SSUCCESS sent ;)");
            } catch (Exception e) {
                #region unable to send packet 2, online
                toLog("\trefresh::" user.name + " is online (Connected, unable to send packet #2) " + e.Message);
                setUserStatus(user, userStatus.Online);
                return;
                #endregion
            }
            toLog("\trefresh::" + user.name + " is online");
            setUserStatus(user, userStatus.Online);
        } else {
            for (int x = 0; x < 5; x++) {
                toLog("\t\trefresh::got wrong answer");
                System.Media.SystemSounds.Beep.Play();
                Thread.Sleep(500);
            }
            setUserStatus(user, userStatus.Online);
        }
John Smith
  • 496
  • 1
  • 6
  • 20
  • Does "The thread just stops" mean "hangs", or "interrupts"? – ndry Apr 27 '13 at 08:06
  • The thread just exits. – John Smith Apr 27 '13 at 08:07
  • The code in the question is just torn out of context, isn't it? Can you post the whole code the thread runs in one big monoblock? (if it is not too big, of course) – ndry Apr 27 '13 at 08:23
  • If it neither logs nor throws exception, maybe it just hangs? I'm not sure, but looks like Read() blocks? because no data was available. Look here http://stackoverflow.com/questions/6958255/what-are-some-reasons-networkstream-read-would-hang-block – ndry Apr 30 '13 at 20:48

1 Answers1

0

Seems like your Read() blocks, because no data is available to be read. Look here for more information: What are some reasons NetworkStream.Read would hang/block?

If I am right, the reason is the server doesn't send data. Try debugging the server, not client.

Community
  • 1
  • 1
ndry
  • 181
  • 2
  • 13