views:

45

answers:

1

Any suggestions on testing a TCP client listener. What I'm looking for is actual network stats like "payload size", "time-out values", "actual payload bytes received from server". public static void tcpConnect() { int port = 12345;

        IPEndPoint ipep = new IPEndPoint(
                IPAddress.Parse("xx.xx.xx.xx"), port);
        Socket server = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
        IPGlobalProperties _ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
        TcpConnectionInformation[] _tcpConnInfoArray = _ipGlobalProperties.GetActiveTcpConnections();

       // bool isAvaiable = true;

        try
        {
            server.Connect(ipep);                   
        }   
        catch (SocketException e)
        {
            Console.WriteLine("Unable to connect to server");
            Console.WriteLine(e.ToString());
            return;
        }
        NetworkStream ns = new NetworkStream(server);
        if (ns.CanRead)
        {
            foreach (TcpConnectionInformation tcpConnInfo in _tcpConnInfoArray)
            {
                if (tcpConnInfo.LocalEndPoint.Port == port)
                {
                    //isAvaiable = false;
                    Console.WriteLine("Error: Can't Read from Port");
                    break;
                }

                Console.Write("Local endpoint: {0} ", tcpConnInfo.LocalEndPoint.Address);
                Console.Write("Remote endpoint: {0} ", tcpConnInfo.RemoteEndPoint.Address);
                Console.Write("{0}", tcpConnInfo.State);
            }
        }
        else 
        {
            Console.WriteLine("Error: Can't Read from Socket");
            ns.Close();
            return;
        }
        while (true)
        {

           string input = Console.ReadLine();
            if (input == "exit")
                break;
            if (ns.CanWrite)
            {
                Console.WriteLine("NS Can write");
                ns.Flush();

            }
        }
        Console.WriteLine("Disconnection from server");
        ns.Close();
        server.Shutdown(SocketShutdown.Both);
        server.Close();


      }       

  }     
+1  A: 

So if I read correctly the real aim is to monitor the connection to see if it is still alive? We all know TCP is a state-full protocol. This means the "socket" must remain open. So you client will have a socket which is open. The TcpClient is just a nice wrapper that give you the ability to read and write to the socket. From the MS API page we can see there is an Active property. As the documentation reads, this is not updated if the remote disconnects... this is too bad and perhaps the problem you have. However they give the advise that if you want to watch the socket you will see if the remote disconnects. You can do this using the Connected property.

I know that not going to be the level of control you want, who wants to just see the TCP Client?? So get right at the Socket!!! once you have the socket you can have full control and viability over the connection. This should make you happy!!

CrazyDart
Thanks for the information...this helps a whole lot. What I'm trying to do is read and write to that socket once I have established a good connection.
Chad Sellers