Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → C#/.NET/Windows problem.
C#/.NET/Windows problem.
2007-02-28, 4:39 PM #1
Ok. I have a .NET (in C#) service that needs to make connection to a projector through TCP. It serves as a back end to a GUI. Reason why it is this way is because I need the GUI to show up just under the login prompt. The idea is that anyone can use the projector w/o having the need to log in to the machine (say to play a DVD). The service is set to start automatically.

The problem:
I keep getting System.IO.IOException: System unable to write data to the transport connection: an existing connection was forcibly closed by the remote host. Now here's the kicker. If I login to the machine and kill the service and then turn it back on, everything runs hunky dory. I can turn the damn thing on/off and what not. And it works for every single subsequent login/logout until the next reboot.

Here's the code that throws the exception.
Code:
//m_ns is a NetworkStream object
try
{
    //Write to the port but have to convert string -> byte[]
    if (m_ns.CanWrite)
    {
        m_ns.Write(Encoding.ASCII.GetBytes(cmd), 0, cmd.Length);
    }
    else
    {
        LogFile.WriteLog("!ERROR! OnscreenService: Unable to write to the socket");
        return false;
    }
    return true;
}
catch (SocketException se)  //Exceptions for sockets
{
    //There is code here but I don't think it is relevant to this issue.
}
catch (System.IO.IOException ioe)  //Exceptions for NetworkStream
{
    LogFile.WriteLog(string.Format("!ERROR! Onscreenservice (System.IO.IOException): {0} {1}",
        ioe.Source, ioe.Message, ioe.StackTrace));
    return false;
}

This baffles me since .CanWrite should be returning false if the connection was closed.

Edit: The service throws the exception not the GUI.
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-28, 4:54 PM #2
Are you sure the service is running in the first place when the PC first turns on?
2007-02-28, 5:00 PM #3
Yes. I should probably make it clear that the SERVICE is throwing the exception and not the GUI part.
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-28, 5:03 PM #4
you forgot a semicolon
free(jin);
tofu sucks
2007-02-28, 5:05 PM #5
landfish.Dispose();
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-28, 5:22 PM #6
:(
free(jin);
tofu sucks
2007-02-28, 6:29 PM #7
Originally posted by JediGandalf:
landfish.Dispose();


syntax error:
line 1: class "landfish" is undeclared.
fail

*Bandit Codes*

Code:
class landfish
{
   public:

   landfish(); 
   landfish( const char * landfishIsAwesome ); 
};
"DON'T TASE ME BRO!" lol
2007-02-28, 7:20 PM #8
I don't think the problem is in that code.

First of all, .CanWrite is useless the way you are using it. It can only be used (as I understand it) to tell whether a particular SOCKET CLASS is capable of writing. For example, a stream of a HTTP download would have .CanWrite return false. Correct me if I'm wrong but I think that for your stream .CanWrite always returns true regardless of whether the connection is open or not. I think that could be what is happening. The usefulness of .CanWrite is only if you have a function which accepts generic Sockets (which is always fun because you can then proceed to pull data from a number of different sources).

Without knowing how or when you open and close the socket or how you want to manage it, I would look for an end of file method or some other method that will tell you if the socket has been closed, and if it has been reopen the socket and do the handshaking or whatnot and continue.

2007-02-28, 7:23 PM #9
either what mzzt said or you forgot a semicolon :p
free(jin);
tofu sucks
2007-02-28, 7:29 PM #10
Connect function
Code:
private bool DoConnect(IPAddress ip)
{
    string port = _cmdConfig.FindConfigOption("netport");
    if (port.Length == 0)
    {
        LogFile.WriteLog("!ERROR!: Missing netport in projector config");
        return false;
    }

    try
    {
        m_tcp = new TcpClient(AddressFamily.InterNetwork);
        m_tcp.Connect(new IPEndPoint(ip, int.Parse(port)));  //Connect IP/port
        m_ns = m_tcp.GetStream();

        if (_theMake.ToLower() == "epson")  //Epson projector?
            SendEpsonPreamble();

        m_ns.BeginRead(m_recvBuf, 0, m_recvBuf.Length, m_recvCB, null);  //Begin asynchronous read
        base._isConnected = true;  //Set that we are connected.
        return true;
    }
    catch (TimeoutException te)
    {
        LogFile.WriteLog(string.Format("!ERROR!: {0}", te.Message));
        return false;
    }
    catch (SocketException se)
    {
        LogFile.WriteLog(string.Format("!ERROR! {0}: Socket error ({1}) - {2}",
            se.Source, se.ErrorCode, se.Message));
        return false;
    }
}
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2007-02-28, 7:38 PM #11
I don't see anything wrong in what you've posted. :(

2007-02-28, 8:04 PM #12
From the NetworkStream.Write remarks @ MSDN docs . . .

Quote:
If you attempt to write to a NetworkStream that is not writable, you will get an IOException. If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException...

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code, and refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error.


Have you done any of this yet? This might not get you very far but it is information available with your error beyond a stack trace.

↑ Up to the top!