Menu

#1 Problem with Disconnecting.

v1.0_(example)
open
nobody
5
2005-06-14
2005-06-14
Anonymous
No

Additional information: Error reading from socket.

this is the disassembly of where it fails.

00000164 E8 8F 4E 00 76 call 76004FF8
00000169 90 nop

Discussion

  • Nobody/Anonymous

    Logged In: NO

    same here... get:
    Object reference not set to an instance of an object.

    Unhandled Exception: System.ObjectDisposedException: Cannot
    access a disposed ob
    ject named "System.Net.Sockets.Socket".
    Object name: "System.Net.Sockets.Socket".
    at System.Net.Sockets.Socket.EndSend(IAsyncResult
    asyncResult)
    at De.Mud.Telnet.TelnetWrapper.SendCallback(IAsyncResult ar)
    at
    System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback(UInt32
    err
    orCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
    at De.Mud.Telnet
    .TelnetWrapper.Disconnect()
    at ConsoleApplication1.Class1.Main(String[] args) in
    c:\projects\test\telnet\
    consoleapplication1\class1.cs:line 34

     
  • Nobody/Anonymous

    Logged In: NO

    I'm having the same issue. An unhandled exception.

     
  • SeeRockCity

    SeeRockCity - 2006-02-13

    Logged In: YES
    user_id=1450656

    Me too. I think that this happens when there is still
    data coming back from the telnet host when the disconnect
    () call is made.

    After debuggin for a while, I noticed that the Disconnect
    method ran just fine but the next block of code to run was
    in the method ReceiveCallback which (of course) is trying
    to access the socket that you just closed by calling
    disconnect.

    I'm not sure how you would solve this. How can you tell
    when the telnet server is done sending its data if all
    these calls are happening Asynchronously?

    I ended up just commenting out the exception in the
    RecieveCallback method (inside the catch block) and
    writing a log event there instead.

     
  • Nobody/Anonymous

    Logged In: NO

    Guys, the correct way is not to throw an exception in
    void "OnRecievedData"! You just validate the connection
    and shut it down!

    Ex.:

    catch(Exception ex)
    {
    if (Connected != null)
    Disconnect();
    }

    This should be enought!

     
  • Becky

    Becky - 2006-09-07

    Logged In: YES
    user_id=1592788

    I am having the same problem. It would work better if
    there were a way to "stop receiving". You can start it,
    but you can't stop it!

     
  • Nigel Benns

    Nigel Benns - 2007-01-02

    Logged In: YES
    user_id=216782
    Originator: NO

    I get this problem when I do a "Console.Readline" after the application disconnects improperly.
    This little app just spits out garbage when I try to connect to a server (no terminal emulation yet?)
    I also don't get any of my Console.WriteLines in the catch statements (does this redirect standard io?)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using De.Mud.Telnet;

    namespace telnetapp
    {
    class Program
    {
    static void Main(string[] args)
    {
    TelnetWrapper tw = new TelnetWrapper();
    tw.Hostname = "130.170.255.49";
    tw.Port = 23;
    tw.TerminalType = "xterm";

    tw.DataAvailable += new DataAvailableEventHandler(tw_DataAvailable);
    tw.Disconnected += new DisconnectedEventHandler(tw_Disconnected);

    tw.Connect();
    if (tw.Connected)
    {
    try
    {
    tw.Receive();
    }
    catch { Console.WriteLine("bad"); }
    };

    tw.Disconnect();

    Console.ReadLine();

    }

    static void tw_Disconnected(object sender, EventArgs e)
    {
    Console.WriteLine("Disconnected");
    }

    static void tw_DataAvailable(object sender, Net.Graphite.Telnet.DataAvailableEventArgs e)
    {
    TelnetWrapper tw = sender as TelnetWrapper;

    try
    {
    Console.WriteLine(e.Data);
    }
    catch
    {
    if (!tw.Connected)
    tw.Disconnect();

    }
    }

    }
    }

     
  • Nobody/Anonymous

    Logged In: NO

    Solution to this problem is as follows:

    --Change--

    public bool Connected
    {
    get
    {
    return socket.Connected;
    }
    }

    --To--

    public bool IsConnected
    {
    get;
    private set;
    }

    --Add to TelnetWrapper--
    public event ConnectedEventHandler Connected;

    --Add to delegate list--
    public delegate void ConnectedEventHandler(object sender, ConnectedEventArgs e);

    --Add ConnectedEventArgs.cs--
    using System;

    namespace Net.Graphite.Telnet
    {
    public class ConnectedEventArgs : EventArgs
    {
    public bool Connected { get; set; }

    public ConnectedEventArgs(bool connected)
    {
    Connected = connected;
    }
    }
    }

    --Change--
    private void ConnectCallback(IAsyncResult ar)
    {
    Socket client = null;

    try
    {
    // Retrieve the socket from the state object.
    client = (Socket)ar.AsyncState;

    // Complete the connection.
    client.EndConnect(ar);

    // Signal that the connection has been made.
    connectDone.Set();
    }
    catch (Exception e)
    {
    Disconnect();
    throw(new ApplicationException("Unable to connect to " +
    client.RemoteEndPoint.ToString(), e));
    }
    }

    --To--

    private void ConnectCallback(IAsyncResult ar)
    {
    Socket client = null;

    try
    {
    // Retrieve the socket from the state object.
    client = (Socket)ar.AsyncState;

    // Complete the connection.
    client.EndConnect(ar);

    // Signal that the connection has been made.
    IsConnected = true;
    connectDone.Set();
    Connected(this, new ConnectedEventArgs(true));
    }
    catch
    {
    Disconnect();
    IsConnected = false;
    connectDone.Set();
    Connected(this, new ConnectedEventArgs(false));
    }
    }

    --Change--
    public void Connect(string host, int port)
    {
    try
    {
    // Establish the remote endpoint for the socket.
    IPHostEntry ipHostInfo = Dns.Resolve(host);
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

    // Create a TCP/IP socket.
    socket = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);

    // Connect to the remote endpoint.
    socket.BeginConnect(remoteEP,
    new AsyncCallback(ConnectCallback), socket);
    connectDone.WaitOne();

    Reset();
    }
    catch
    {
    Disconnect();
    throw;
    }
    }

    --To--

    public void Connect(string host, int port)
    {
    try
    {
    // Establish the remote endpoint for the socket.
    IPHostEntry ipHostInfo = Dns.Resolve(host);
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

    // Create a TCP/IP socket.
    socket = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);

    // Connect to the remote endpoint.
    socket.BeginConnect(remoteEP,
    new AsyncCallback(ConnectCallback), socket);
    connectDone.WaitOne();

    if (IsConnected)
    Reset();
    else
    Disconnect();
    }
    catch
    {
    Disconnect();
    throw;
    }
    }

    --Surround the code within these functions with if (IsConnected) { /* original code */ } --
    public string Send(string cmd)
    protected override void Write(byte[] b)
    private void Receive(Socket client)
    private void ReceiveCallback(IAsyncResult ar)
    private void Send(Socket client, byte[] byteData)
    private void SendCallback(IAsyncResult ar)

     
  • Nobody/Anonymous

    Logged In: NO

    On the note of my code below, the test code is as follows now:

    public TestRun(string host, int port)
    {
    t = new TelnetWrapper();

    t.Disconnected += new DisconnectedEventHandler(this.OnDisconnect);
    t.DataAvailable += new DataAvailableEventHandler(this.OnDataAvailable);
    t.Connected += new ConnectedEventHandler(this.OnConnected);

    t.TerminalType = "NETWORK-VIRTUAL-TERMINAL";
    t.Hostname = host;
    t.Port = port;
    Console.WriteLine("Connecting ...");
    t.Connect();
    }

    void OnConnected(object sender, ConnectedEventArgs e)
    {
    if (e.Connected)
    {
    // Do work here
    }
    else
    {
    // display error
    }
    }

    -- TRScheel --

     
  • Nobody/Anonymous

    Logged In: NO

    What is this? A "solution" with empty or abstract IsConnected/Connected get/set methods?

     
  • Nobody/Anonymous

    Logged In: NO

    To the previous comment: I got told that Visual Studio 2008 uses these empty methods to create properties automatically. Older VS versions need the property stored in separate variables, which are set and returned via parameters in the get/set methods.

    Anyway, it didn't work for me, I couldn't create a Telnet connection with this anymore, and I have no time to work on it much longer.

     
  • Nobody/Anonymous

    I had the following NullReferenceException exception when calling the .Disconnect() method on a TelnetWrapper object.

    System.NullReferenceException: <snip>
    bij De.Mud.Telnet.TelnetWrapper.Disconnect() in <snip>\Telnet\TelnetWrapper.cs:regel 214
    bij De.Mud.Telnet.TelnetWrapper.Dispose(Boolean disposing) in <snip>\Telnet\TelnetWrapper.cs:regel 379
    bij De.Mud.Telnet.TelnetWrapper.Dispose() in <snip>\Telnet\TelnetWrapper.cs:regel 373
    bij De.Mud.Telnet.TelnetWrapper.Close() in <snip>\Telnet\TelnetWrapper.cs:regel 367
    bij TranService.Device.Disconnect() in <snip>\Device.cs:regel 68
    bij Service.CloseConnection(String deviceAlias) in <snip>\Service.cs:regel 51

    The TelnetWrapper.Disconnect() works on the DisconnectedEventHandler which wasn't set in my case, hence the exception. Get past this exception by either configuring an event listener, or by adding a simple check in the TelnetWrapper.cs file, Disconnect() method:
    if (Disconnected != null)
    {
    Disconnected(this, new System.EventArgs());
    }

     

Log in to post a comment.

MongoDB Logo MongoDB