Tech Off Thread

5 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

TcpClient Not Connected From Array Lists vs 2005

Back to Forum: Tech Off
  • User profile image
    Shark_M

    hello guys
    There is this problem that i am not sure why it is happening.
    The problem is that I have this server, in C# that when it accepts clients, i add the TcpClient socket to the ArrayList at a specified index.

    Now I say something like this
    ArrayAllClients.Add (Handler)

    where TcpClient handler = listener.AcceptTcpClient();

    at the time handler connected = true. all seems fine.

    now when i try to send to multiple clients, I have this method called SendToGroupOfClients, and it takes indexes.

    so i do this

    TcpClient TempClient = new TcpClient();
            TempClient = (TcpClient)SynchronousSocketListener.ArrayAllClients[index];

    after checking
                            NetworkStream stream = TempClient.GetStream();
                            if (stream.CanWrite == true)
                            {
                                stream.Write(sendBytes, 0, sendBytes.Length);
                            }
    this bit is in a try catch block, and it always goes to the catch, because it says cannot write to non-connected sockets. However the client is connected, and its in the arraylist for all clients

    This Code Worked ok in C# 7.1(2003), but not 8.0 (2005)

    To This day i am not sure why this is happening or what i can do to correct this problem?

    any suggestions would help me
    thanks

  • User profile image
    Shark_M

    why would this work ok in 2003? and not in 2005 builds of Visual Studio C#?

  • User profile image
    Shark_M

    the problem boils down to this
    the TCPclient in the array list is not connected. Why would it not be connected?

    Tcpclient.client.connected = false;

    however the client is logging on as i am debugging.
    this problem is only in C# 2.0 not in the 2003 build Sad

  • User profile image
    ScanIAm

    Shark_M wrote:
    hello guys
    There is this problem that i am not sure why it is happening.
    The problem is that I have this server, in C# that when it accepts clients, i add the TcpClient socket to the ArrayList at a specified index.

    Now I say something like this
    ArrayAllClients.Add (Handler)

    where TcpClient handler = listener.AcceptTcpClient();

    at the time handler connected = true. all seems fine.

    now when i try to send to multiple clients, I have this method called SendToGroupOfClients, and it takes indexes.

    so i do this

    TcpClient TempClient = new TcpClient();
            TempClient = (TcpClient)SynchronousSocketListener.ArrayAllClients[index];

    after checking
                            NetworkStream stream = TempClient.GetStream();
                            if (stream.CanWrite == true)
                            {
                                stream.Write(sendBytes, 0, sendBytes.Length);
                            }
    this bit is in a try catch block, and it always goes to the catch, because it says cannot write to non-connected sockets. However the client is connected, and its in the arraylist for all clients

    This Code Worked ok in C# 7.1(2003), but not 8.0 (2005)

    To This day i am not sure why this is happening or what i can do to correct this problem?

    any suggestions would help me
    thanks


    I don't know why this would be different for different versions of the software, but I do know that TcpClient was essentially useless when I tried to write a sockets app for file transfers.  It limits what you are able to do and it doesn't allow you to manipulate the socket directly.  You might want to look at the KeepAlive settings on the socket to see if it has timed out.

    If I were you, though, I'd rewrite it using sockets directly.  You'll get more control over the data and you'll learn more about how sockets work at the same time.

  • User profile image
    Shark_M

     solved the problem. Its a simple thing. Apparently C# 2.0 , need a live object (Client Thread that actually holds live socket connection for you to be able to pass to array list). when you pass something to the ArrayList that is alive instance , like live connected socket, it only takes a copy of it, and not a copy of its Handle or session ID for the socket. i.e it makes a dead copy of the tcpclient.

    Using Delegates and Events i managed to do the trick  to invoke the holder thread for the actual tcpclient networkstream

    it works great


    thanks again!

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.